[{"data":1,"prerenderedAt":197},["Reactive",2],{"/guides/python/aiohttp/save-your-pdf-online-and-get-back-a-url":3,"related:bQkclqCH3L":115},{"_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":110,"_id":111,"_source":112,"_file":113,"_extension":114},"/guides/python/aiohttp/save-your-pdf-online-and-get-back-a-url","aiohttp",false,"","Save your PDF online and get back a URL","This guides shows you how can generate a PDF document from HTML and get back an URL instead of the raw PDF. This allows you to share the link directly to your users or redirect them to that page. Learn how you can achieve it using Python and the aiohttp library to call PDFShift's API.","Python","filename","pdf",[14,15],"save-your-pdf-to-your-amazon-s3-bucket","receive-a-webhook-event",{"type":17,"children":18,"toc":107},"root",[19,27,40,52,72,77,88,98],{"type":20,"tag":21,"props":22,"children":23},"element","p",{},[24],{"type":25,"value":26},"text","In this guide, we'll show you how you can generate a PDF document from HTML and get back an URL instead of the raw PDF. This allows you to share the link directly to your users or redirect them to that page.",{"type":20,"tag":21,"props":28,"children":29},{},[30,32,38],{"type":25,"value":31},"To do so, we are going to pass the ",{"type":20,"tag":33,"props":34,"children":36},"code",{"className":35},[],[37],{"type":25,"value":11},{"type":25,"value":39}," parameter to the request. It expects a string that will be the name of the file once it is saved on our servers.",{"type":20,"tag":41,"props":42,"children":47},"pre",{"className":43,"code":45,"language":46,"meta":7},[44],"language-python","import aiohttp, asyncio, json\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    'filename': 'example.pdf'\n}\n\nresponse = None\ntry:\n    async with aiohttp.ClientSession() as session:\n        async with session.post(\n            'https://api.pdfshift.io/v3/convert/pdf',\n            headers={'X-API-Key': api_key},\n            json=params\n        ) as response:\n            if response.status >= 400:\n                raise Exception('Invalid request: {}'.format(await response.text()))\n\n            print('The PDF document was generated and is available online via the url in the response')\nexcept asyncio.TimeoutError:\n    raise Exception('The request took too long to process')\nexcept aiohttp.ClientError as e:\n    raise Exception(f'An error occurred: {e}')\nexcept Exception as e:\n    # We highly recommend you to handle exceptions. Often, PDFShift will provide you with a clear explanation about what happened.\n    # Moreover, in case of error, no PDF are returned !\n    raise Exception(f'An error occurred: {e}')\n","python",[48],{"type":20,"tag":33,"props":49,"children":50},{"__ignoreMap":7},[51],{"type":25,"value":45},{"type":20,"tag":21,"props":53,"children":54},{},[55,57,62,64,70],{"type":25,"value":56},"In the above example, we used the ",{"type":20,"tag":33,"props":58,"children":60},{"className":59},[],[61],{"type":25,"value":11},{"type":25,"value":63}," parameter to specify the name of the file once it is saved on our servers. The response will convert the document ",{"type":20,"tag":65,"props":66,"children":67},"strong",{},[68],{"type":25,"value":69},"but return a JSON response",{"type":25,"value":71}," instead of the raw PDF.",{"type":20,"tag":21,"props":73,"children":74},{},[75],{"type":25,"value":76},"The body of the response will be similar to this:",{"type":20,"tag":41,"props":78,"children":83},{"className":79,"code":81,"language":82,"meta":7},[80],"language-json","{\n    \"success\": true,\n    \"url\": \"https://pdfshift.s3.amazonaws.com/d/2/2024-03/c9c2d3e536ff42d892f06fdda6bb1ff7/example.pdf\",\n    \"filesize\": 34980,\n    \"duration\": 1237,\n    \"response\": {\n        \"status-code\": 200,\n        \"content-length\": 0,\n        \"requests\": 0,\n        \"duration\": 1085.6739225387573\n    },\n    \"executed\": \"2024-03-06T10:05:51.516413\",\n    \"pdf_pages\": 1\n}\n","json",[84],{"type":20,"tag":33,"props":85,"children":86},{"__ignoreMap":7},[87],{"type":25,"value":81},{"type":20,"tag":21,"props":89,"children":90},{},[91,96],{"type":20,"tag":65,"props":92,"children":93},{},[94],{"type":25,"value":95},"NOTE",{"type":25,"value":97},": Using the filename does save your document on our S3 storage for 2 days. After two days, the document is automatically deleted.",{"type":20,"tag":21,"props":99,"children":100},{},[101,105],{"type":20,"tag":65,"props":102,"children":103},{},[104],{"type":25,"value":95},{"type":25,"value":106},": Since we store your documents on our server, this command is not recommended if you want to be HIPAA compliant.",{"title":7,"searchDepth":108,"depth":108,"links":109},2,[],"markdown","content:guides:python:aiohttp:save-your-pdf-online-and-get-back-a-url.md","content","guides/python/aiohttp/save-your-pdf-online-and-get-back-a-url.md","md",[116,120,124,128,132,136,140,144,148,152,156,160,164,168,172,176,180,181,185,189,193],{"_path":117,"title":118,"language":10,"library":5,"_id":119},"/guides/python/aiohttp/accessing-secured-pages","Accessing secured pages","content:guides:python:aiohttp:accessing-secured-pages.md",{"_path":121,"title":122,"language":10,"library":5,"_id":123},"/guides/python/aiohttp/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:python:aiohttp:adding-a-custom-header-or-footer.md",{"_path":125,"title":126,"language":10,"library":5,"_id":127},"/guides/python/aiohttp/adding-a-text-watermark","Adding a text watermark","content:guides:python:aiohttp:adding-a-text-watermark.md",{"_path":129,"title":130,"language":10,"library":5,"_id":131},"/guides/python/aiohttp/adding-an-image-watermark","Adding an image watermark","content:guides:python:aiohttp:adding-an-image-watermark.md",{"_path":133,"title":134,"language":10,"library":5,"_id":135},"/guides/python/aiohttp/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:python:aiohttp:avoid-conversion-on-error.md",{"_path":137,"title":138,"language":10,"library":5,"_id":139},"/guides/python/aiohttp/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:python:aiohttp:convert-html-to-pdf-from-a-url.md",{"_path":141,"title":142,"language":10,"library":5,"_id":143},"/guides/python/aiohttp/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:python:aiohttp:convert-html-to-pdf-from-raw-html.md",{"_path":145,"title":146,"language":10,"library":5,"_id":147},"/guides/python/aiohttp/define-a-time-limit","Define a time limit","content:guides:python:aiohttp:define-a-time-limit.md",{"_path":149,"title":150,"language":10,"library":5,"_id":151},"/guides/python/aiohttp/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:python:aiohttp:exporting-only-a-specific-set-of-pages.md",{"_path":153,"title":154,"language":10,"library":5,"_id":155},"/guides/python/aiohttp/generate-a-document-with-full-height","Generate a document with full height","content:guides:python:aiohttp:generate-a-document-with-full-height.md",{"_path":157,"title":158,"language":10,"library":5,"_id":159},"/guides/python/aiohttp/loading-css-from-a-string","Loading CSS from a string","content:guides:python:aiohttp:loading-css-from-a-string.md",{"_path":161,"title":162,"language":10,"library":5,"_id":163},"/guides/python/aiohttp/loading-css-from-a-url","Loading CSS from a URL","content:guides:python:aiohttp:loading-css-from-a-url.md",{"_path":165,"title":166,"language":10,"library":5,"_id":167},"/guides/python/aiohttp/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:python:aiohttp:loading-javascript-from-a-string.md",{"_path":169,"title":170,"language":10,"library":5,"_id":171},"/guides/python/aiohttp/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:python:aiohttp:loading-javascript-from-a-url.md",{"_path":173,"title":174,"language":10,"library":5,"_id":175},"/guides/python/aiohttp/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:python:aiohttp:protecting-the-generated-pdf.md",{"_path":177,"title":178,"language":10,"library":5,"_id":179},"/guides/python/aiohttp/receive-a-webhook-event","Receive a webhook event","content:guides:python:aiohttp:receive-a-webhook-event.md",{"_path":4,"title":8,"language":10,"library":5,"_id":111},{"_path":182,"title":183,"language":10,"library":5,"_id":184},"/guides/python/aiohttp/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:python:aiohttp:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":186,"title":187,"language":10,"library":5,"_id":188},"/guides/python/aiohttp/send-custom-http-headers","Send custom HTTP headers","content:guides:python:aiohttp:send-custom-http-headers.md",{"_path":190,"title":191,"language":10,"library":5,"_id":192},"/guides/python/aiohttp/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:python:aiohttp:using-cookies.md",{"_path":194,"title":195,"language":10,"library":5,"_id":196},"/guides/python/aiohttp/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:python:aiohttp:waiting-for-a-custom-element-to-be-ready.md",1785426833297]