[{"data":1,"prerenderedAt":149},["Reactive",2],{"/guides/node/got/loading-javascript-from-a-string":3,"related:MzFj2smDjO":67},{"_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":62,"_id":63,"_source":64,"_file":65,"_extension":66},"/guides/node/got/loading-javascript-from-a-string","got",false,"","Loading Javascript from a string","Follow this guide to learn how to add custom javascript to your page to customize the output of your generated PDF. This allows you to modify elements on the page, add or remove content, etc. This guide uses Node and the Got library and relies on the PDFShift's API.","Node","Got","javascript","pdf",[15,16,17,18],"adding-a-custom-header-or-footer","loading-css-from-a-string","loading-css-from-a-url","loading-javascript-from-a-url",{"type":20,"children":21,"toc":59},"root",[22,30,35,47],{"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 javascript code as a given string when converting an HTML document to PDF using PDFShift's API. This allows you to add custom JS 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 JS 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":41},"pre",{"className":38,"code":40,"language":12,"meta":7},[39],"language-javascript","const got = require('got');\nconst fs = require('fs');\n\n// You can get an API key at https://pdfshift.io\napiKey = 'sk_xxxxxxxxxxxx'\n\nparams = {\n    source: 'https://www.example.com',\n    javascript: 'document.querySelector(\"h1\").style.color = \"red\";'\n}\n\nconst response = await got.post('https://api.pdfshift.io/v3/convert/pdf', {\n    headers: { 'X-API-Key': apiKey },\n    json: params,\n    responseType: 'buffer'\n});\n\n// Write the buffer as result.pdf\nfs.writeFileSync('result.pdf', await response.buffer());\n\nconsole.log('The PDF document was generated and saved to result.pdf');\n",[42],{"type":23,"tag":43,"props":44,"children":45},"code",{"__ignoreMap":7},[46],{"type":28,"value":40},{"type":23,"tag":24,"props":48,"children":49},{},[50,52,57],{"type":28,"value":51},"The ",{"type":23,"tag":43,"props":53,"children":55},{"className":54},[],[56],{"type":28,"value":12},{"type":28,"value":58}," parameter accepts either a string or a URL. It will be used as the javascript for the page when converting it to PDF. This allows you to set a custom JS value to adjust the rendering of your page specifically when exporting the document to PDF.",{"title":7,"searchDepth":60,"depth":60,"links":61},2,[],"markdown","content:guides:node:got:loading-javascript-from-a-string.md","content","guides/node/got/loading-javascript-from-a-string.md","md",[68,72,76,80,84,88,92,96,100,104,108,112,116,117,121,125,129,133,137,141,145],{"_path":69,"title":70,"language":10,"library":11,"_id":71},"/guides/node/got/accessing-secured-pages","Accessing secured pages","content:guides:node:got:accessing-secured-pages.md",{"_path":73,"title":74,"language":10,"library":11,"_id":75},"/guides/node/got/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:node:got:adding-a-custom-header-or-footer.md",{"_path":77,"title":78,"language":10,"library":11,"_id":79},"/guides/node/got/adding-a-text-watermark","Adding a text watermark","content:guides:node:got:adding-a-text-watermark.md",{"_path":81,"title":82,"language":10,"library":11,"_id":83},"/guides/node/got/adding-an-image-watermark","Adding an image watermark","content:guides:node:got:adding-an-image-watermark.md",{"_path":85,"title":86,"language":10,"library":11,"_id":87},"/guides/node/got/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:node:got:avoid-conversion-on-error.md",{"_path":89,"title":90,"language":10,"library":11,"_id":91},"/guides/node/got/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:node:got:convert-html-to-pdf-from-a-url.md",{"_path":93,"title":94,"language":10,"library":11,"_id":95},"/guides/node/got/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:node:got:convert-html-to-pdf-from-raw-html.md",{"_path":97,"title":98,"language":10,"library":11,"_id":99},"/guides/node/got/define-a-time-limit","Define a time limit","content:guides:node:got:define-a-time-limit.md",{"_path":101,"title":102,"language":10,"library":11,"_id":103},"/guides/node/got/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:node:got:exporting-only-a-specific-set-of-pages.md",{"_path":105,"title":106,"language":10,"library":11,"_id":107},"/guides/node/got/generate-a-document-with-full-height","Generate a document with full height","content:guides:node:got:generate-a-document-with-full-height.md",{"_path":109,"title":110,"language":10,"library":11,"_id":111},"/guides/node/got/loading-css-from-a-string","Loading CSS from a string","content:guides:node:got:loading-css-from-a-string.md",{"_path":113,"title":114,"language":10,"library":11,"_id":115},"/guides/node/got/loading-css-from-a-url","Loading CSS from a URL","content:guides:node:got:loading-css-from-a-url.md",{"_path":4,"title":8,"language":10,"library":11,"_id":63},{"_path":118,"title":119,"language":10,"library":11,"_id":120},"/guides/node/got/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:node:got:loading-javascript-from-a-url.md",{"_path":122,"title":123,"language":10,"library":11,"_id":124},"/guides/node/got/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:node:got:protecting-the-generated-pdf.md",{"_path":126,"title":127,"language":10,"library":11,"_id":128},"/guides/node/got/receive-a-webhook-event","Receive a webhook event","content:guides:node:got:receive-a-webhook-event.md",{"_path":130,"title":131,"language":10,"library":11,"_id":132},"/guides/node/got/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:node:got:save-your-pdf-online-and-get-back-a-url.md",{"_path":134,"title":135,"language":10,"library":11,"_id":136},"/guides/node/got/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:node:got:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":138,"title":139,"language":10,"library":11,"_id":140},"/guides/node/got/send-custom-http-headers","Send custom HTTP headers","content:guides:node:got:send-custom-http-headers.md",{"_path":142,"title":143,"language":10,"library":11,"_id":144},"/guides/node/got/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:node:got:using-cookies.md",{"_path":146,"title":147,"language":10,"library":11,"_id":148},"/guides/node/got/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:node:got:waiting-for-a-custom-element-to-be-ready.md",1785426829971]