[{"data":1,"prerenderedAt":155},["Reactive",2],{"/guides/ruby/faraday/loading-javascript-from-a-url":3,"related:MmJresC0zH":73},{"_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":68,"_id":69,"_source":70,"_file":71,"_extension":72},"/guides/ruby/faraday/loading-javascript-from-a-url","faraday",false,"","Loading Javascript from a URL","Follow this guide to add custom javascript to your page before converting it to PDF. Using a URL allows you to later be able to change the content of the javascript without having to update the query made to PDFShift. Learn how you can implement this using Ruby and the Faraday library to call the PDFShift's API.","Ruby","Faraday","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-string",{"type":20,"children":21,"toc":65},"root",[22,30,35,40,53],{"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 as a URL 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 javascript 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":24,"props":36,"children":37},{},[38],{"type":28,"value":39},"Moreover, adding a URL can be easier to do than passing the raw javascript content, as it allows you to later modify that javascript without having to modify the query made to PDFShift.",{"type":23,"tag":41,"props":42,"children":47},"pre",{"className":43,"code":45,"language":46,"meta":7},[44],"language-ruby","require 'faraday'\nrequire 'json'\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    'javascript' => 'https://www.example.com/custom.js'\n}\n\n# Create a Faraday connection\nconn = Faraday.new(url: \"https://api.pdfshift.io/v3/convert/pdf\") do |faraday|\n    faraday.request :url_encoded\n    faraday.adapter Faraday.default_adapter\nend\n\n# Make the POST request\nresponse = conn.post do |req|\n    req.headers['Content-Type'] = 'application/json'\n    req.headers['X-API-Key'] = api_key\n    req.body = params.to_json\nend\n\n# Check for successful response\nunless response.success?\n    raise \"Request failed with status code #{response.status}: #{response.body}\"\nend\n\n# write response to a file nammed \"result.pdf\"\nFile.open('result.pdf', 'wb') { |f| f.write(response.body) }\n\n# Print a success message\nputs 'The PDF document was generated and saved to result.pdf'\n","ruby",[48],{"type":23,"tag":49,"props":50,"children":51},"code",{"__ignoreMap":7},[52],{"type":28,"value":45},{"type":23,"tag":24,"props":54,"children":55},{},[56,58,63],{"type":28,"value":57},"The ",{"type":23,"tag":49,"props":59,"children":61},{"className":60},[],[62],{"type":28,"value":12},{"type":28,"value":64}," 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 javascript value to adjust the rendering of your page specifically when exporting the document to PDF.",{"title":7,"searchDepth":66,"depth":66,"links":67},2,[],"markdown","content:guides:ruby:faraday:loading-javascript-from-a-url.md","content","guides/ruby/faraday/loading-javascript-from-a-url.md","md",[74,78,82,86,90,94,98,102,106,110,114,118,122,126,127,131,135,139,143,147,151],{"_path":75,"title":76,"language":10,"library":11,"_id":77},"/guides/ruby/faraday/accessing-secured-pages","Accessing secured pages","content:guides:ruby:faraday:accessing-secured-pages.md",{"_path":79,"title":80,"language":10,"library":11,"_id":81},"/guides/ruby/faraday/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:ruby:faraday:adding-a-custom-header-or-footer.md",{"_path":83,"title":84,"language":10,"library":11,"_id":85},"/guides/ruby/faraday/adding-a-text-watermark","Adding a text watermark","content:guides:ruby:faraday:adding-a-text-watermark.md",{"_path":87,"title":88,"language":10,"library":11,"_id":89},"/guides/ruby/faraday/adding-an-image-watermark","Adding an image watermark","content:guides:ruby:faraday:adding-an-image-watermark.md",{"_path":91,"title":92,"language":10,"library":11,"_id":93},"/guides/ruby/faraday/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:ruby:faraday:avoid-conversion-on-error.md",{"_path":95,"title":96,"language":10,"library":11,"_id":97},"/guides/ruby/faraday/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:ruby:faraday:convert-html-to-pdf-from-a-url.md",{"_path":99,"title":100,"language":10,"library":11,"_id":101},"/guides/ruby/faraday/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:ruby:faraday:convert-html-to-pdf-from-raw-html.md",{"_path":103,"title":104,"language":10,"library":11,"_id":105},"/guides/ruby/faraday/define-a-time-limit","Define a time limit","content:guides:ruby:faraday:define-a-time-limit.md",{"_path":107,"title":108,"language":10,"library":11,"_id":109},"/guides/ruby/faraday/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:ruby:faraday:exporting-only-a-specific-set-of-pages.md",{"_path":111,"title":112,"language":10,"library":11,"_id":113},"/guides/ruby/faraday/generate-a-document-with-full-height","Generate a document with full height","content:guides:ruby:faraday:generate-a-document-with-full-height.md",{"_path":115,"title":116,"language":10,"library":11,"_id":117},"/guides/ruby/faraday/loading-css-from-a-string","Loading CSS from a string","content:guides:ruby:faraday:loading-css-from-a-string.md",{"_path":119,"title":120,"language":10,"library":11,"_id":121},"/guides/ruby/faraday/loading-css-from-a-url","Loading CSS from a URL","content:guides:ruby:faraday:loading-css-from-a-url.md",{"_path":123,"title":124,"language":10,"library":11,"_id":125},"/guides/ruby/faraday/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:ruby:faraday:loading-javascript-from-a-string.md",{"_path":4,"title":8,"language":10,"library":11,"_id":69},{"_path":128,"title":129,"language":10,"library":11,"_id":130},"/guides/ruby/faraday/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:ruby:faraday:protecting-the-generated-pdf.md",{"_path":132,"title":133,"language":10,"library":11,"_id":134},"/guides/ruby/faraday/receive-a-webhook-event","Receive a webhook event","content:guides:ruby:faraday:receive-a-webhook-event.md",{"_path":136,"title":137,"language":10,"library":11,"_id":138},"/guides/ruby/faraday/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:ruby:faraday:save-your-pdf-online-and-get-back-a-url.md",{"_path":140,"title":141,"language":10,"library":11,"_id":142},"/guides/ruby/faraday/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:ruby:faraday:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":144,"title":145,"language":10,"library":11,"_id":146},"/guides/ruby/faraday/send-custom-http-headers","Send custom HTTP headers","content:guides:ruby:faraday:send-custom-http-headers.md",{"_path":148,"title":149,"language":10,"library":11,"_id":150},"/guides/ruby/faraday/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:ruby:faraday:using-cookies.md",{"_path":152,"title":153,"language":10,"library":11,"_id":154},"/guides/ruby/faraday/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:ruby:faraday:waiting-for-a-custom-element-to-be-ready.md",1785426834491]