[{"data":1,"prerenderedAt":169},["Reactive",2],{"/guides/python/httplib2/avoid-conversion-on-error":3,"related:y1juaPZIJF":87},{"_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":15,"_type":82,"_id":83,"_source":84,"_file":85,"_extension":86},"/guides/python/httplib2/avoid-conversion-on-error","httplib2",false,"","Avoid the conversion on error when loading the document","Learn how to avoid the conversion on error when loading the document using Python and the httplib2 library and relies on the PDFShift's API.","Python","raise_for_status","pdf",[14],"convert-html-to-pdf-from-raw-html",{"type":16,"children":17,"toc":79},"root",[18,26,47,59],{"type":19,"tag":20,"props":21,"children":22},"element","p",{},[23],{"type":24,"value":25},"text","In this guide, we'll show you how to abort the conversion when loading the distant source does not return a 2XX response.",{"type":19,"tag":20,"props":27,"children":28},{},[29,31,37,39,45],{"type":24,"value":30},"When you're converting a document, you might want to avoid the conversion on error when loading the document. This can be done by setting the ",{"type":19,"tag":32,"props":33,"children":35},"code",{"className":34},[],[36],{"type":24,"value":11},{"type":24,"value":38}," parameter to ",{"type":19,"tag":32,"props":40,"children":42},{"className":41},[],[43],{"type":24,"value":44},"True",{"type":24,"value":46}," in the request.",{"type":19,"tag":48,"props":49,"children":54},"pre",{"className":50,"code":52,"language":53,"meta":7},[51],"language-python","import httplib2, json, base64\n\n# You can get an API key at https://pdfshift.io\napi_key = 'sk_xxxxxxxxxxxx'\n\nparams = {\n    'source': 'https://www.httpstat.us/404',\n    'raise_for_status': True\n}\n\nhttp = httplib2.Http()\nresponse, content = http.request(\n    'https://api.pdfshift.io/v3/convert/pdf',\n    method='POST',\n    body=json.dumps(params),\n    headers={\n        'Content-Type': 'application/json',\n        'X-API-Key': api_key\n    }\n)\n\nif response.status >= 400:\n    raise ValueError(f\"Request failed with status code {response.status}: {content.decode('utf-8')}\")\n\nwith open('result.pdf', 'wb') as f:\n    f.write(content)\n\nprint('The PDF document was generated and saved to result.pdf')\n","python",[55],{"type":19,"tag":32,"props":56,"children":57},{"__ignoreMap":7},[58],{"type":24,"value":52},{"type":19,"tag":20,"props":60,"children":61},{},[62,64,69,71,77],{"type":24,"value":63},"Passing ",{"type":19,"tag":32,"props":65,"children":67},{"className":66},[],[68],{"type":24,"value":11},{"type":24,"value":70}," to ",{"type":19,"tag":32,"props":72,"children":74},{"className":73},[],[75],{"type":24,"value":76},"true",{"type":24,"value":78}," will ensure that if PDFShift can not load your document, the conversion will fail with an error.",{"title":7,"searchDepth":80,"depth":80,"links":81},2,[],"markdown","content:guides:python:httplib2:avoid-conversion-on-error.md","content","guides/python/httplib2/avoid-conversion-on-error.md","md",[88,92,96,100,104,105,109,113,117,121,125,129,133,137,141,145,149,153,157,161,165],{"_path":89,"title":90,"language":10,"library":5,"_id":91},"/guides/python/httplib2/accessing-secured-pages","Accessing secured pages","content:guides:python:httplib2:accessing-secured-pages.md",{"_path":93,"title":94,"language":10,"library":5,"_id":95},"/guides/python/httplib2/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:python:httplib2:adding-a-custom-header-or-footer.md",{"_path":97,"title":98,"language":10,"library":5,"_id":99},"/guides/python/httplib2/adding-a-text-watermark","Adding a text watermark","content:guides:python:httplib2:adding-a-text-watermark.md",{"_path":101,"title":102,"language":10,"library":5,"_id":103},"/guides/python/httplib2/adding-an-image-watermark","Adding an image watermark","content:guides:python:httplib2:adding-an-image-watermark.md",{"_path":4,"title":8,"language":10,"library":5,"_id":83},{"_path":106,"title":107,"language":10,"library":5,"_id":108},"/guides/python/httplib2/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:python:httplib2:convert-html-to-pdf-from-a-url.md",{"_path":110,"title":111,"language":10,"library":5,"_id":112},"/guides/python/httplib2/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:python:httplib2:convert-html-to-pdf-from-raw-html.md",{"_path":114,"title":115,"language":10,"library":5,"_id":116},"/guides/python/httplib2/define-a-time-limit","Define a time limit","content:guides:python:httplib2:define-a-time-limit.md",{"_path":118,"title":119,"language":10,"library":5,"_id":120},"/guides/python/httplib2/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:python:httplib2:exporting-only-a-specific-set-of-pages.md",{"_path":122,"title":123,"language":10,"library":5,"_id":124},"/guides/python/httplib2/generate-a-document-with-full-height","Generate a document with full height","content:guides:python:httplib2:generate-a-document-with-full-height.md",{"_path":126,"title":127,"language":10,"library":5,"_id":128},"/guides/python/httplib2/loading-css-from-a-string","Loading CSS from a string","content:guides:python:httplib2:loading-css-from-a-string.md",{"_path":130,"title":131,"language":10,"library":5,"_id":132},"/guides/python/httplib2/loading-css-from-a-url","Loading CSS from a URL","content:guides:python:httplib2:loading-css-from-a-url.md",{"_path":134,"title":135,"language":10,"library":5,"_id":136},"/guides/python/httplib2/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:python:httplib2:loading-javascript-from-a-string.md",{"_path":138,"title":139,"language":10,"library":5,"_id":140},"/guides/python/httplib2/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:python:httplib2:loading-javascript-from-a-url.md",{"_path":142,"title":143,"language":10,"library":5,"_id":144},"/guides/python/httplib2/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:python:httplib2:protecting-the-generated-pdf.md",{"_path":146,"title":147,"language":10,"library":5,"_id":148},"/guides/python/httplib2/receive-a-webhook-event","Receive a webhook event","content:guides:python:httplib2:receive-a-webhook-event.md",{"_path":150,"title":151,"language":10,"library":5,"_id":152},"/guides/python/httplib2/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:python:httplib2:save-your-pdf-online-and-get-back-a-url.md",{"_path":154,"title":155,"language":10,"library":5,"_id":156},"/guides/python/httplib2/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:python:httplib2:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":158,"title":159,"language":10,"library":5,"_id":160},"/guides/python/httplib2/send-custom-http-headers","Send custom HTTP headers","content:guides:python:httplib2:send-custom-http-headers.md",{"_path":162,"title":163,"language":10,"library":5,"_id":164},"/guides/python/httplib2/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:python:httplib2:using-cookies.md",{"_path":166,"title":167,"language":10,"library":5,"_id":168},"/guides/python/httplib2/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:python:httplib2:waiting-for-a-custom-element-to-be-ready.md",1785426833379]