[{"data":1,"prerenderedAt":150},["Reactive",2],{"/guides/ruby/httpparty/loading-css-from-a-string":3,"related:AOC3pMEYVK":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/ruby/httpparty/loading-css-from-a-string","httpparty",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 Ruby code samples with the HttpParty library.","Ruby","HttpParty","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-ruby","require 'httparty'\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    'css' => 'body { background-color: #f00; }'\n}\n\nresponse = HTTParty.post(\n    \"https://api.pdfshift.io/v3/convert/pdf\",\n    body: params.to_json,\n    headers: {\n        'Content-Type' => 'application/json',\n        'X-API-Key' => api_key\n    }\n)\n\n# Check for successful response\nunless response.success?\n    raise \"Request failed with status code #{response.code}: #{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",[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:ruby:httpparty:loading-css-from-a-string.md","content","guides/ruby/httpparty/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/ruby/httpparty/accessing-secured-pages","Accessing secured pages","content:guides:ruby:httpparty:accessing-secured-pages.md",{"_path":74,"title":75,"language":10,"library":11,"_id":76},"/guides/ruby/httpparty/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:ruby:httpparty:adding-a-custom-header-or-footer.md",{"_path":78,"title":79,"language":10,"library":11,"_id":80},"/guides/ruby/httpparty/adding-a-text-watermark","Adding a text watermark","content:guides:ruby:httpparty:adding-a-text-watermark.md",{"_path":82,"title":83,"language":10,"library":11,"_id":84},"/guides/ruby/httpparty/adding-an-image-watermark","Adding an image watermark","content:guides:ruby:httpparty:adding-an-image-watermark.md",{"_path":86,"title":87,"language":10,"library":11,"_id":88},"/guides/ruby/httpparty/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:ruby:httpparty:avoid-conversion-on-error.md",{"_path":90,"title":91,"language":10,"library":11,"_id":92},"/guides/ruby/httpparty/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:ruby:httpparty:convert-html-to-pdf-from-a-url.md",{"_path":94,"title":95,"language":10,"library":11,"_id":96},"/guides/ruby/httpparty/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:ruby:httpparty:convert-html-to-pdf-from-raw-html.md",{"_path":98,"title":99,"language":10,"library":11,"_id":100},"/guides/ruby/httpparty/define-a-time-limit","Define a time limit","content:guides:ruby:httpparty:define-a-time-limit.md",{"_path":102,"title":103,"language":10,"library":11,"_id":104},"/guides/ruby/httpparty/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:ruby:httpparty:exporting-only-a-specific-set-of-pages.md",{"_path":106,"title":107,"language":10,"library":11,"_id":108},"/guides/ruby/httpparty/generate-a-document-with-full-height","Generate a document with full height","content:guides:ruby:httpparty: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/ruby/httpparty/loading-css-from-a-url","Loading CSS from a URL","content:guides:ruby:httpparty:loading-css-from-a-url.md",{"_path":115,"title":116,"language":10,"library":11,"_id":117},"/guides/ruby/httpparty/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:ruby:httpparty:loading-javascript-from-a-string.md",{"_path":119,"title":120,"language":10,"library":11,"_id":121},"/guides/ruby/httpparty/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:ruby:httpparty:loading-javascript-from-a-url.md",{"_path":123,"title":124,"language":10,"library":11,"_id":125},"/guides/ruby/httpparty/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:ruby:httpparty:protecting-the-generated-pdf.md",{"_path":127,"title":128,"language":10,"library":11,"_id":129},"/guides/ruby/httpparty/receive-a-webhook-event","Receive a webhook event","content:guides:ruby:httpparty:receive-a-webhook-event.md",{"_path":131,"title":132,"language":10,"library":11,"_id":133},"/guides/ruby/httpparty/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:ruby:httpparty:save-your-pdf-online-and-get-back-a-url.md",{"_path":135,"title":136,"language":10,"library":11,"_id":137},"/guides/ruby/httpparty/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:ruby:httpparty:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":139,"title":140,"language":10,"library":11,"_id":141},"/guides/ruby/httpparty/send-custom-http-headers","Send custom HTTP headers","content:guides:ruby:httpparty:send-custom-http-headers.md",{"_path":143,"title":144,"language":10,"library":11,"_id":145},"/guides/ruby/httpparty/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:ruby:httpparty:using-cookies.md",{"_path":147,"title":148,"language":10,"library":11,"_id":149},"/guides/ruby/httpparty/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:ruby:httpparty:waiting-for-a-custom-element-to-be-ready.md",1785426834994]