[{"data":1,"prerenderedAt":150},["Reactive",2],{"/guides/node/node-fetch/loading-css-from-a-string":3,"related:F7hAO4MGHW":68},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":11,"property":12,"output":13,"related":14,"default":6,"body":19,"_type":63,"_id":64,"_source":65,"_file":66,"_extension":67},"/guides/node/node-fetch/loading-css-from-a-string","node-fetch",false,"","Loading CSS from a string","Follow this guide to load custom CSS as a given string when converting an HTML document to PDF with PDFShift's API. This guide provides Node code samples with the NodeFetch library.","Node","NodeFetch","css","pdf",[15,16,17,18],"adding-a-custom-header-or-footer","loading-css-from-a-url","loading-javascript-from-a-string","loading-javascript-from-a-url",{"type":20,"children":21,"toc":60},"root",[22,30,35,48],{"type":23,"tag":24,"props":25,"children":26},"element","p",{},[27],{"type":28,"value":29},"text","In this guide, we'll show you how to load custom CSS as a given string when converting an HTML document to PDF using PDFShift's API. This allows you to add custom CSS to your page to customize the output of your generated PDF.",{"type":23,"tag":24,"props":31,"children":32},{},[33],{"type":28,"value":34},"It can be interesting to be able to set a custom CSS value to adjust the rendering of your page specifically when exporting the document to PDF.\nThis allows you to keep a standard look and feel to your users, but adjust some areas when sending that same document to PDFShift to get back a PDF.",{"type":23,"tag":36,"props":37,"children":42},"pre",{"className":38,"code":40,"language":41,"meta":7},[39],"language-javascript","const fetch = require('node-fetch');\nconst fs = require('fs');\n\n// You can get an API key at https://pdfshift.io\napi_key = 'sk_xxxxxxxxxxxx'\n\nparams = {\n    source: 'https://www.example.com',\n    css: 'body { background-color: #f00; }'\n}\n\nconst response = await fetch('https://api.pdfshift.io/v3/convert/pdf', {\n    method: 'post',\n    headers: {\n        'X-API-Key': api_key,\n        'Content-Type': 'application/json'\n    },\n    body: JSON.stringify(params)\n});\n\nif (!response.ok) {\n    throw new Error(`HTTP error! status: ${response.status}`);\n}\n\nfs.writeFileSync('result.pdf', response.buffer());\n\nconsole.log('The PDF document was generated and saved to result.pdf');\n","javascript",[43],{"type":23,"tag":44,"props":45,"children":46},"code",{"__ignoreMap":7},[47],{"type":28,"value":40},{"type":23,"tag":24,"props":49,"children":50},{},[51,53,58],{"type":28,"value":52},"The ",{"type":23,"tag":44,"props":54,"children":56},{"className":55},[],[57],{"type":28,"value":12},{"type":28,"value":59}," parameter accepts either a string or a URL. It will be used as the CSS for the page when converting it to PDF. This allows you to set a custom CSS value to adjust the rendering of your page specifically when exporting the document to PDF.",{"title":7,"searchDepth":61,"depth":61,"links":62},2,[],"markdown","content:guides:node:node-fetch:loading-css-from-a-string.md","content","guides/node/node-fetch/loading-css-from-a-string.md","md",[69,73,77,81,85,89,93,97,101,105,109,110,114,118,122,126,130,134,138,142,146],{"_path":70,"title":71,"language":10,"library":11,"_id":72},"/guides/node/node-fetch/accessing-secured-pages","Accessing secured pages","content:guides:node:node-fetch:accessing-secured-pages.md",{"_path":74,"title":75,"language":10,"library":11,"_id":76},"/guides/node/node-fetch/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:node:node-fetch:adding-a-custom-header-or-footer.md",{"_path":78,"title":79,"language":10,"library":11,"_id":80},"/guides/node/node-fetch/adding-a-text-watermark","Adding a text watermark","content:guides:node:node-fetch:adding-a-text-watermark.md",{"_path":82,"title":83,"language":10,"library":11,"_id":84},"/guides/node/node-fetch/adding-an-image-watermark","Adding an image watermark","content:guides:node:node-fetch:adding-an-image-watermark.md",{"_path":86,"title":87,"language":10,"library":11,"_id":88},"/guides/node/node-fetch/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:node:node-fetch:avoid-conversion-on-error.md",{"_path":90,"title":91,"language":10,"library":11,"_id":92},"/guides/node/node-fetch/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:node:node-fetch:convert-html-to-pdf-from-a-url.md",{"_path":94,"title":95,"language":10,"library":11,"_id":96},"/guides/node/node-fetch/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:node:node-fetch:convert-html-to-pdf-from-raw-html.md",{"_path":98,"title":99,"language":10,"library":11,"_id":100},"/guides/node/node-fetch/define-a-time-limit","Define a time limit","content:guides:node:node-fetch:define-a-time-limit.md",{"_path":102,"title":103,"language":10,"library":11,"_id":104},"/guides/node/node-fetch/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:node:node-fetch:exporting-only-a-specific-set-of-pages.md",{"_path":106,"title":107,"language":10,"library":11,"_id":108},"/guides/node/node-fetch/generate-a-document-with-full-height","Generate a document with full height","content:guides:node:node-fetch:generate-a-document-with-full-height.md",{"_path":4,"title":8,"language":10,"library":11,"_id":64},{"_path":111,"title":112,"language":10,"library":11,"_id":113},"/guides/node/node-fetch/loading-css-from-a-url","Loading CSS from a URL","content:guides:node:node-fetch:loading-css-from-a-url.md",{"_path":115,"title":116,"language":10,"library":11,"_id":117},"/guides/node/node-fetch/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:node:node-fetch:loading-javascript-from-a-string.md",{"_path":119,"title":120,"language":10,"library":11,"_id":121},"/guides/node/node-fetch/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:node:node-fetch:loading-javascript-from-a-url.md",{"_path":123,"title":124,"language":10,"library":11,"_id":125},"/guides/node/node-fetch/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:node:node-fetch:protecting-the-generated-pdf.md",{"_path":127,"title":128,"language":10,"library":11,"_id":129},"/guides/node/node-fetch/receive-a-webhook-event","Receive a webhook event","content:guides:node:node-fetch:receive-a-webhook-event.md",{"_path":131,"title":132,"language":10,"library":11,"_id":133},"/guides/node/node-fetch/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:node:node-fetch:save-your-pdf-online-and-get-back-a-url.md",{"_path":135,"title":136,"language":10,"library":11,"_id":137},"/guides/node/node-fetch/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:node:node-fetch:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":139,"title":140,"language":10,"library":11,"_id":141},"/guides/node/node-fetch/send-custom-http-headers","Send custom HTTP headers","content:guides:node:node-fetch:send-custom-http-headers.md",{"_path":143,"title":144,"language":10,"library":11,"_id":145},"/guides/node/node-fetch/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:node:node-fetch:using-cookies.md",{"_path":147,"title":148,"language":10,"library":11,"_id":149},"/guides/node/node-fetch/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:node:node-fetch:waiting-for-a-custom-element-to-be-ready.md",1785426830722]