[{"data":1,"prerenderedAt":135},["Reactive",2],{"/guides/python/urllib3/convert-html-to-pdf-from-a-url":3,"related:LXsQlXPlR6":53},{"_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":16,"_type":48,"_id":49,"_source":50,"_file":51,"_extension":52},"/guides/python/urllib3/convert-html-to-pdf-from-a-url","urllib3",false,"","Convert an HTML document to PDF from a URL","Learn how to easily convert HTML documents to PDF from a URL using Python and the urllib3 library. This how-to guide offers clear Python code examples that show developers how to implement this using the PDFShift API.","Python","source","pdf",[14,15],"convert-html-to-pdf-from-raw-html","avoid-conversion-on-error",{"type":17,"children":18,"toc":45},"root",[19,27,40],{"type":20,"tag":21,"props":22,"children":23},"element","p",{},[24],{"type":25,"value":26},"text","In this guide, we'll show you how to convert HTML documents to PDFs using PDFShift's API. This is a straightforward process that can be accomplished with just a few lines of Python code.",{"type":20,"tag":28,"props":29,"children":34},"pre",{"className":30,"code":32,"language":33,"meta":7},[31],"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}\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",[35],{"type":20,"tag":36,"props":37,"children":38},"code",{"__ignoreMap":7},[39],{"type":25,"value":32},{"type":20,"tag":21,"props":41,"children":42},{},[43],{"type":25,"value":44},"With this Python script, you can effortlessly convert HTML documents from a URL to PDF files using PDFShift's API. Happy coding!",{"title":7,"searchDepth":46,"depth":46,"links":47},2,[],"markdown","content:guides:python:urllib3:convert-html-to-pdf-from-a-url.md","content","guides/python/urllib3/convert-html-to-pdf-from-a-url.md","md",[54,58,62,66,70,74,75,79,83,87,91,95,99,103,107,111,115,119,123,127,131],{"_path":55,"title":56,"language":10,"library":5,"_id":57},"/guides/python/urllib3/accessing-secured-pages","Accessing secured pages","content:guides:python:urllib3:accessing-secured-pages.md",{"_path":59,"title":60,"language":10,"library":5,"_id":61},"/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":63,"title":64,"language":10,"library":5,"_id":65},"/guides/python/urllib3/adding-a-text-watermark","Adding a text watermark","content:guides:python:urllib3:adding-a-text-watermark.md",{"_path":67,"title":68,"language":10,"library":5,"_id":69},"/guides/python/urllib3/adding-an-image-watermark","Adding an image watermark","content:guides:python:urllib3:adding-an-image-watermark.md",{"_path":71,"title":72,"language":10,"library":5,"_id":73},"/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":4,"title":8,"language":10,"library":5,"_id":49},{"_path":76,"title":77,"language":10,"library":5,"_id":78},"/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":80,"title":81,"language":10,"library":5,"_id":82},"/guides/python/urllib3/define-a-time-limit","Define a time limit","content:guides:python:urllib3:define-a-time-limit.md",{"_path":84,"title":85,"language":10,"library":5,"_id":86},"/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":88,"title":89,"language":10,"library":5,"_id":90},"/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":92,"title":93,"language":10,"library":5,"_id":94},"/guides/python/urllib3/loading-css-from-a-string","Loading CSS from a string","content:guides:python:urllib3:loading-css-from-a-string.md",{"_path":96,"title":97,"language":10,"library":5,"_id":98},"/guides/python/urllib3/loading-css-from-a-url","Loading CSS from a URL","content:guides:python:urllib3:loading-css-from-a-url.md",{"_path":100,"title":101,"language":10,"library":5,"_id":102},"/guides/python/urllib3/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:python:urllib3:loading-javascript-from-a-string.md",{"_path":104,"title":105,"language":10,"library":5,"_id":106},"/guides/python/urllib3/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:python:urllib3:loading-javascript-from-a-url.md",{"_path":108,"title":109,"language":10,"library":5,"_id":110},"/guides/python/urllib3/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:python:urllib3:protecting-the-generated-pdf.md",{"_path":112,"title":113,"language":10,"library":5,"_id":114},"/guides/python/urllib3/receive-a-webhook-event","Receive a webhook event","content:guides:python:urllib3:receive-a-webhook-event.md",{"_path":116,"title":117,"language":10,"library":5,"_id":118},"/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":120,"title":121,"language":10,"library":5,"_id":122},"/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":124,"title":125,"language":10,"library":5,"_id":126},"/guides/python/urllib3/send-custom-http-headers","Send custom HTTP headers","content:guides:python:urllib3:send-custom-http-headers.md",{"_path":128,"title":129,"language":10,"library":5,"_id":130},"/guides/python/urllib3/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:python:urllib3:using-cookies.md",{"_path":132,"title":133,"language":10,"library":5,"_id":134},"/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",1785426833901]