[{"data":1,"prerenderedAt":159},["Reactive",2],{"/guides/python/httplib2/define-a-time-limit":3,"related:lMh6puQxIi":77},{"_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":72,"_id":73,"_source":74,"_file":75,"_extension":76},"/guides/python/httplib2/define-a-time-limit","httplib2",false,"","Define a time limit","Learn how to set a conversion time limit at PDFShift. This will allow you to define a maximum time for the conversion process to complete, and if the process takes longer than the defined time, the conversion will be aborted. This guides explains you how to achieve it using Python and the httplib2 library.","Python","timeout","pdf",[14],"waiting-for-a-custom-element-to-be-ready",{"type":16,"children":17,"toc":69},"root",[18,26,31,52,64],{"type":19,"tag":20,"props":21,"children":22},"element","p",{},[23],{"type":24,"value":25},"text","In this guide, we'll show you how you can set a conversion time limit at PDFShift. This will allow you to define a maximum time for the conversion process to complete, and if the process takes longer than the defined time, the conversion will be aborted.",{"type":19,"tag":20,"props":27,"children":28},{},[29],{"type":24,"value":30},"In some cases, this can be useful if you want the request to not be too long and then handle failure on your end depending on the result.",{"type":19,"tag":20,"props":32,"children":33},{},[34,36,42,44,50],{"type":24,"value":35},"To do so, we use the ",{"type":19,"tag":37,"props":38,"children":40},"code",{"className":39},[],[41],{"type":24,"value":11},{"type":24,"value":43}," property, which is the maximum time in ",{"type":19,"tag":45,"props":46,"children":47},"strong",{},[48],{"type":24,"value":49},"seconds",{"type":24,"value":51}," that the request is allowed to take. If the request takes longer than the defined time, the conversion will be aborted.",{"type":19,"tag":53,"props":54,"children":59},"pre",{"className":55,"code":57,"language":58,"meta":7},[56],"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.example.com',\n    'timeout': 10\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",[60],{"type":19,"tag":37,"props":61,"children":62},{"__ignoreMap":7},[63],{"type":24,"value":57},{"type":19,"tag":20,"props":65,"children":66},{},[67],{"type":24,"value":68},"In the above example, the conversion could fail if the conversion takes longer than 10 seconds.",{"title":7,"searchDepth":70,"depth":70,"links":71},2,[],"markdown","content:guides:python:httplib2:define-a-time-limit.md","content","guides/python/httplib2/define-a-time-limit.md","md",[78,82,86,90,94,98,102,106,107,111,115,119,123,127,131,135,139,143,147,151,155],{"_path":79,"title":80,"language":10,"library":5,"_id":81},"/guides/python/httplib2/accessing-secured-pages","Accessing secured pages","content:guides:python:httplib2:accessing-secured-pages.md",{"_path":83,"title":84,"language":10,"library":5,"_id":85},"/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":87,"title":88,"language":10,"library":5,"_id":89},"/guides/python/httplib2/adding-a-text-watermark","Adding a text watermark","content:guides:python:httplib2:adding-a-text-watermark.md",{"_path":91,"title":92,"language":10,"library":5,"_id":93},"/guides/python/httplib2/adding-an-image-watermark","Adding an image watermark","content:guides:python:httplib2:adding-an-image-watermark.md",{"_path":95,"title":96,"language":10,"library":5,"_id":97},"/guides/python/httplib2/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:python:httplib2:avoid-conversion-on-error.md",{"_path":99,"title":100,"language":10,"library":5,"_id":101},"/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":103,"title":104,"language":10,"library":5,"_id":105},"/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":4,"title":8,"language":10,"library":5,"_id":73},{"_path":108,"title":109,"language":10,"library":5,"_id":110},"/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":112,"title":113,"language":10,"library":5,"_id":114},"/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":116,"title":117,"language":10,"library":5,"_id":118},"/guides/python/httplib2/loading-css-from-a-string","Loading CSS from a string","content:guides:python:httplib2:loading-css-from-a-string.md",{"_path":120,"title":121,"language":10,"library":5,"_id":122},"/guides/python/httplib2/loading-css-from-a-url","Loading CSS from a URL","content:guides:python:httplib2:loading-css-from-a-url.md",{"_path":124,"title":125,"language":10,"library":5,"_id":126},"/guides/python/httplib2/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:python:httplib2:loading-javascript-from-a-string.md",{"_path":128,"title":129,"language":10,"library":5,"_id":130},"/guides/python/httplib2/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:python:httplib2:loading-javascript-from-a-url.md",{"_path":132,"title":133,"language":10,"library":5,"_id":134},"/guides/python/httplib2/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:python:httplib2:protecting-the-generated-pdf.md",{"_path":136,"title":137,"language":10,"library":5,"_id":138},"/guides/python/httplib2/receive-a-webhook-event","Receive a webhook event","content:guides:python:httplib2:receive-a-webhook-event.md",{"_path":140,"title":141,"language":10,"library":5,"_id":142},"/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":144,"title":145,"language":10,"library":5,"_id":146},"/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":148,"title":149,"language":10,"library":5,"_id":150},"/guides/python/httplib2/send-custom-http-headers","Send custom HTTP headers","content:guides:python:httplib2:send-custom-http-headers.md",{"_path":152,"title":153,"language":10,"library":5,"_id":154},"/guides/python/httplib2/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:python:httplib2:using-cookies.md",{"_path":156,"title":157,"language":10,"library":5,"_id":158},"/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",1785426833395]