[{"data":1,"prerenderedAt":149},["Reactive",2],{"/guides/python/urllib3/loading-css-from-a-string":3,"related:D9ABrKJ2I8":67},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":5,"property":11,"output":12,"related":13,"default":6,"body":18,"_type":62,"_id":63,"_source":64,"_file":65,"_extension":66},"/guides/python/urllib3/loading-css-from-a-string","urllib3",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 Python code samples with the urllib3 library.","Python","css","pdf",[14,15,16,17],"adding-a-custom-header-or-footer","loading-css-from-a-url","loading-javascript-from-a-string","loading-javascript-from-a-url",{"type":19,"children":20,"toc":59},"root",[21,29,34,47],{"type":22,"tag":23,"props":24,"children":25},"element","p",{},[26],{"type":27,"value":28},"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":22,"tag":23,"props":30,"children":31},{},[32],{"type":27,"value":33},"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":22,"tag":35,"props":36,"children":41},"pre",{"className":37,"code":39,"language":40,"meta":7},[38],"language-python","import urllib3, json, base64\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\nhttp = urllib3.PoolManager()\nresponse = http.request(\n    'POST',\n    'https://api.pdfshift.io/v3/convert/pdf',\n    headers={\n        'Content-Type': 'application/json',\n        'X-API-Key': api_key\n    },\n    body=json.dumps(params).encode('utf-8')\n)\n\nif response.status != 200:\n    raise ValueError(f\"Request failed with status code {response.status}: {response.data.decode('utf-8')}\")\n\nwith open('result.pdf', 'wb') as f:\n    f.write(response.content)\n\nprint('The PDF document was generated and saved to result.pdf')\n","python",[42],{"type":22,"tag":43,"props":44,"children":45},"code",{"__ignoreMap":7},[46],{"type":27,"value":39},{"type":22,"tag":23,"props":48,"children":49},{},[50,52,57],{"type":27,"value":51},"The ",{"type":22,"tag":43,"props":53,"children":55},{"className":54},[],[56],{"type":27,"value":11},{"type":27,"value":58}," 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":60,"depth":60,"links":61},2,[],"markdown","content:guides:python:urllib3:loading-css-from-a-string.md","content","guides/python/urllib3/loading-css-from-a-string.md","md",[68,72,76,80,84,88,92,96,100,104,108,109,113,117,121,125,129,133,137,141,145],{"_path":69,"title":70,"language":10,"library":5,"_id":71},"/guides/python/urllib3/accessing-secured-pages","Accessing secured pages","content:guides:python:urllib3:accessing-secured-pages.md",{"_path":73,"title":74,"language":10,"library":5,"_id":75},"/guides/python/urllib3/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:python:urllib3:adding-a-custom-header-or-footer.md",{"_path":77,"title":78,"language":10,"library":5,"_id":79},"/guides/python/urllib3/adding-a-text-watermark","Adding a text watermark","content:guides:python:urllib3:adding-a-text-watermark.md",{"_path":81,"title":82,"language":10,"library":5,"_id":83},"/guides/python/urllib3/adding-an-image-watermark","Adding an image watermark","content:guides:python:urllib3:adding-an-image-watermark.md",{"_path":85,"title":86,"language":10,"library":5,"_id":87},"/guides/python/urllib3/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:python:urllib3:avoid-conversion-on-error.md",{"_path":89,"title":90,"language":10,"library":5,"_id":91},"/guides/python/urllib3/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:python:urllib3:convert-html-to-pdf-from-a-url.md",{"_path":93,"title":94,"language":10,"library":5,"_id":95},"/guides/python/urllib3/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:python:urllib3:convert-html-to-pdf-from-raw-html.md",{"_path":97,"title":98,"language":10,"library":5,"_id":99},"/guides/python/urllib3/define-a-time-limit","Define a time limit","content:guides:python:urllib3:define-a-time-limit.md",{"_path":101,"title":102,"language":10,"library":5,"_id":103},"/guides/python/urllib3/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:python:urllib3:exporting-only-a-specific-set-of-pages.md",{"_path":105,"title":106,"language":10,"library":5,"_id":107},"/guides/python/urllib3/generate-a-document-with-full-height","Generate a document with full height","content:guides:python:urllib3:generate-a-document-with-full-height.md",{"_path":4,"title":8,"language":10,"library":5,"_id":63},{"_path":110,"title":111,"language":10,"library":5,"_id":112},"/guides/python/urllib3/loading-css-from-a-url","Loading CSS from a URL","content:guides:python:urllib3:loading-css-from-a-url.md",{"_path":114,"title":115,"language":10,"library":5,"_id":116},"/guides/python/urllib3/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:python:urllib3:loading-javascript-from-a-string.md",{"_path":118,"title":119,"language":10,"library":5,"_id":120},"/guides/python/urllib3/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:python:urllib3:loading-javascript-from-a-url.md",{"_path":122,"title":123,"language":10,"library":5,"_id":124},"/guides/python/urllib3/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:python:urllib3:protecting-the-generated-pdf.md",{"_path":126,"title":127,"language":10,"library":5,"_id":128},"/guides/python/urllib3/receive-a-webhook-event","Receive a webhook event","content:guides:python:urllib3:receive-a-webhook-event.md",{"_path":130,"title":131,"language":10,"library":5,"_id":132},"/guides/python/urllib3/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:python:urllib3:save-your-pdf-online-and-get-back-a-url.md",{"_path":134,"title":135,"language":10,"library":5,"_id":136},"/guides/python/urllib3/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:python:urllib3:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":138,"title":139,"language":10,"library":5,"_id":140},"/guides/python/urllib3/send-custom-http-headers","Send custom HTTP headers","content:guides:python:urllib3:send-custom-http-headers.md",{"_path":142,"title":143,"language":10,"library":5,"_id":144},"/guides/python/urllib3/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:python:urllib3:using-cookies.md",{"_path":146,"title":147,"language":10,"library":5,"_id":148},"/guides/python/urllib3/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:python:urllib3:waiting-for-a-custom-element-to-be-ready.md",1785426833935]