[{"data":1,"prerenderedAt":199},["Reactive",2],{"/guides/python/urllib3/convert-html-to-pdf-from-raw-html":3,"related:kg1M7yAzh9":117},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":5,"output":11,"related":12,"default":6,"body":14,"_type":112,"_id":113,"_source":114,"_file":115,"_extension":116},"/guides/python/urllib3/convert-html-to-pdf-from-raw-html","urllib3",false,"","Convert an HTML document to PDF from raw HTML","Learn how to to convert raw HTML document to PDF using Python and the urllib3 library. Our guide will explain you how to quickly convert documents with PDFShift's API.","Python","pdf",[13],"convert-html-to-pdf-from-a-url",{"type":15,"children":16,"toc":109},"root",[17,25,45,67,80,85,97],{"type":18,"tag":19,"props":20,"children":21},"element","p",{},[22],{"type":23,"value":24},"text","In this guide, we'll show you how to convert an HTML document to PDF using Python and the urllib3 library.\nProviding the HTML document as raw as a lot of great advantages :",{"type":18,"tag":26,"props":27,"children":28},"ul",{},[29,35,40],{"type":18,"tag":30,"props":31,"children":32},"li",{},[33],{"type":23,"value":34},"It avoids PDFShift to make a network request to fetch the HTML document.",{"type":18,"tag":30,"props":36,"children":37},{},[38],{"type":23,"value":39},"It allows you to convert HTML documents that are not publicly accessible.",{"type":18,"tag":30,"props":41,"children":42},{},[43],{"type":23,"value":44},"It improves the speed of the conversion",{"type":18,"tag":19,"props":46,"children":47},{},[48,50,57,59,65],{"type":23,"value":49},"If, on top of that, you can provide the styles and javascript also inline in the document (",{"type":18,"tag":51,"props":52,"children":54},"code",{"className":53},[],[55],{"type":23,"value":56},"\u003Cstyle>",{"type":23,"value":58}," and ",{"type":18,"tag":51,"props":60,"children":62},{"className":61},[],[63],{"type":23,"value":64},"\u003Cscript>",{"type":23,"value":66}," tags), it will also drastically reduce the duration of the conversion.",{"type":18,"tag":19,"props":68,"children":69},{},[70,72,78],{"type":23,"value":71},"For converting a raw document, we use the same parameter as for the URL, which is the ",{"type":18,"tag":51,"props":73,"children":75},{"className":74},[],[76],{"type":23,"value":77},"source",{"type":23,"value":79}," parameter.\nAll you have to do is provide an HTML document.",{"type":18,"tag":19,"props":81,"children":82},{},[83],{"type":23,"value":84},"Here's an example:",{"type":18,"tag":86,"props":87,"children":92},"pre",{"className":88,"code":90,"language":91,"meta":7},[89],"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': '\u003Chtml>\u003Cbody>\u003Ch1>This will be a PDF document\u003C/h1>\u003Cp>This will generate a basic PDF to show how you can add raw HTML\u003C/body>\u003C/html>',\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",[93],{"type":18,"tag":51,"props":94,"children":95},{"__ignoreMap":7},[96],{"type":23,"value":90},{"type":18,"tag":19,"props":98,"children":99},{},[100,102,107],{"type":23,"value":101},"Providing the ",{"type":18,"tag":51,"props":103,"children":105},{"className":104},[],[106],{"type":23,"value":77},{"type":23,"value":108}," parameter as a raw HTML document is by far the best recommended method to convert HTML documents in PDFShift as it reduce the amount of network requests, loading time of each documents (image, css, javascript, etc) and thus improve the conversion time.",{"title":7,"searchDepth":110,"depth":110,"links":111},2,[],"markdown","content:guides:python:urllib3:convert-html-to-pdf-from-raw-html.md","content","guides/python/urllib3/convert-html-to-pdf-from-raw-html.md","md",[118,122,126,130,134,138,142,143,147,151,155,159,163,167,171,175,179,183,187,191,195],{"_path":119,"title":120,"language":10,"library":5,"_id":121},"/guides/python/urllib3/accessing-secured-pages","Accessing secured pages","content:guides:python:urllib3:accessing-secured-pages.md",{"_path":123,"title":124,"language":10,"library":5,"_id":125},"/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":127,"title":128,"language":10,"library":5,"_id":129},"/guides/python/urllib3/adding-a-text-watermark","Adding a text watermark","content:guides:python:urllib3:adding-a-text-watermark.md",{"_path":131,"title":132,"language":10,"library":5,"_id":133},"/guides/python/urllib3/adding-an-image-watermark","Adding an image watermark","content:guides:python:urllib3:adding-an-image-watermark.md",{"_path":135,"title":136,"language":10,"library":5,"_id":137},"/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":139,"title":140,"language":10,"library":5,"_id":141},"/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":4,"title":8,"language":10,"library":5,"_id":113},{"_path":144,"title":145,"language":10,"library":5,"_id":146},"/guides/python/urllib3/define-a-time-limit","Define a time limit","content:guides:python:urllib3:define-a-time-limit.md",{"_path":148,"title":149,"language":10,"library":5,"_id":150},"/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":152,"title":153,"language":10,"library":5,"_id":154},"/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":156,"title":157,"language":10,"library":5,"_id":158},"/guides/python/urllib3/loading-css-from-a-string","Loading CSS from a string","content:guides:python:urllib3:loading-css-from-a-string.md",{"_path":160,"title":161,"language":10,"library":5,"_id":162},"/guides/python/urllib3/loading-css-from-a-url","Loading CSS from a URL","content:guides:python:urllib3:loading-css-from-a-url.md",{"_path":164,"title":165,"language":10,"library":5,"_id":166},"/guides/python/urllib3/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:python:urllib3:loading-javascript-from-a-string.md",{"_path":168,"title":169,"language":10,"library":5,"_id":170},"/guides/python/urllib3/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:python:urllib3:loading-javascript-from-a-url.md",{"_path":172,"title":173,"language":10,"library":5,"_id":174},"/guides/python/urllib3/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:python:urllib3:protecting-the-generated-pdf.md",{"_path":176,"title":177,"language":10,"library":5,"_id":178},"/guides/python/urllib3/receive-a-webhook-event","Receive a webhook event","content:guides:python:urllib3:receive-a-webhook-event.md",{"_path":180,"title":181,"language":10,"library":5,"_id":182},"/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":184,"title":185,"language":10,"library":5,"_id":186},"/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":188,"title":189,"language":10,"library":5,"_id":190},"/guides/python/urllib3/send-custom-http-headers","Send custom HTTP headers","content:guides:python:urllib3:send-custom-http-headers.md",{"_path":192,"title":193,"language":10,"library":5,"_id":194},"/guides/python/urllib3/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:python:urllib3:using-cookies.md",{"_path":196,"title":197,"language":10,"library":5,"_id":198},"/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",1785426833912]