[{"data":1,"prerenderedAt":216},["Reactive",2],{"/guides/python/aiohttp/save-your-pdf-to-your-amazon-s3-bucket":3,"related:ntqnSzwtPv":134},{"_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":129,"_id":130,"_source":131,"_file":132,"_extension":133},"/guides/python/aiohttp/save-your-pdf-to-your-amazon-s3-bucket","aiohttp",false,"","Save your PDF to your Amazon S3 Bucket","This guides shows you how can generate a PDF document from HTML with PDFShift's API and save it to your own Amazon S3 bucket. This allows you to generate document and automate actions once the document has been created on your bucket. This guides explains you how to do it using Python and the aiohttp library.","Python","s3_destination","pdf",[14,15],"save-your-pdf-online-and-get-back-a-url","receive-a-webhook-event",{"type":17,"children":18,"toc":126},"root",[19,27,32,37,50,62,73,85,94,99,108,113],{"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 with PDFShift's API and save it to your own Amazon S3 bucket. This allows you to generate document and automate actions once the document has been created on your bucket.",{"type":20,"tag":21,"props":28,"children":29},{},[30],{"type":25,"value":31},"First, you'll need to set up permissions in your bucket to allow PDFShift to write in it.",{"type":20,"tag":21,"props":33,"children":34},{},[35],{"type":25,"value":36},"We crafted a very simple policy that only does what is required and nothing more.\nYou can copy/paste it and apply it on your bucket, but don't forget to change the name of your bucket:",{"type":20,"tag":38,"props":39,"children":44},"pre",{"className":40,"code":42,"language":43,"meta":7},[41],"language-json","{\n    \"Version\": \"2012-10-17\",\n    \"Statement\": [{\n        \"Sid\": \"Allows PDFShift.io to write the generated PDF in this bucket.\",\n        \"Effect\": \"Allow\",\n        \"Principal\": {\n            \"AWS\": [\"arn:aws:iam::804461045055:user/pdfshift\"]\n        },\n        \"Action\": [\"s3:PutObject\"],\n        \"Resource\": \"arn:aws:s3:::DOC-EXAMPLE-BUCKET/*\"\n    }]\n}\n","json",[45],{"type":20,"tag":46,"props":47,"children":48},"code",{"__ignoreMap":7},[49],{"type":25,"value":42},{"type":20,"tag":21,"props":51,"children":52},{},[53,55,60],{"type":25,"value":54},"Once this is done, you can send a request to PDFShift by adding the ",{"type":20,"tag":46,"props":56,"children":58},{"className":57},[],[59],{"type":25,"value":11},{"type":25,"value":61}," parameter. It takes the full s3 bucket as a URL.\nOnce the conversion will be done, PDFShift will",{"type":20,"tag":38,"props":63,"children":68},{"className":64,"code":66,"language":67,"meta":7},[65],"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    's3_destination': 's3://DOC-EXAMPLE-BUCKET/test/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 saved to your S3 Bucket')\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",[69],{"type":20,"tag":46,"props":70,"children":71},{"__ignoreMap":7},[72],{"type":25,"value":66},{"type":20,"tag":21,"props":74,"children":75},{},[76,78,83],{"type":25,"value":77},"Once the conversion is done, PDFShift will save the content to your given ",{"type":20,"tag":46,"props":79,"children":81},{"className":80},[],[82],{"type":25,"value":11},{"type":25,"value":84}," path and will return a response with a JSON body, such as:",{"type":20,"tag":38,"props":86,"children":89},{"className":87,"code":88,"language":43,"meta":7},[41],"{\n    \"success\": true,\n    \"url\": \"https://DOC-EXAMPLE-BUCKET.s3.amazonaws.com/test/example.pdf\",\n    \"filesize\": 34980,\n    \"duration\": 1423,\n    \"response\": {\n        \"status-code\": 200,\n        \"content-length\": 0,\n        \"requests\": 0,\n        \"duration\": 1263.254446029663\n    },\n    \"executed\": \"2024-03-06T10:13:33.154960\",\n    \"pdf_pages\": 1\n}\n",[90],{"type":20,"tag":46,"props":91,"children":92},{"__ignoreMap":7},[93],{"type":25,"value":88},{"type":20,"tag":21,"props":95,"children":96},{},[97],{"type":25,"value":98},"In the event you haven't setup the permissions properly, PDFShift will fail with the following JSON body error:",{"type":20,"tag":38,"props":100,"children":103},{"className":101,"code":102,"language":43,"meta":7},[41],"{\n    \"success\": false,\n    \"error\": \"The S3 destination you provided can not be accessed.\",\n    \"code\": 400\n}\n",[104],{"type":20,"tag":46,"props":105,"children":106},{"__ignoreMap":7},[107],{"type":25,"value":102},{"type":20,"tag":21,"props":109,"children":110},{},[111],{"type":25,"value":112},"This features is really interesting as it allows you to not have to write the logic once the file was generated if your intention is to backup the file (such as an invoice, a report or saving a document).",{"type":20,"tag":21,"props":114,"children":115},{},[116,118,124],{"type":25,"value":117},"You can couple it with the ",{"type":20,"tag":46,"props":119,"children":121},{"className":120},[],[122],{"type":25,"value":123},"webhook",{"type":25,"value":125}," parameter to increase the speed of processing. By doing so, PDFShift will treat your conversion request asynchronously and save the document in the given path. The request made to PDFShift will be immediate.",{"title":7,"searchDepth":127,"depth":127,"links":128},2,[],"markdown","content:guides:python:aiohttp:save-your-pdf-to-your-amazon-s3-bucket.md","content","guides/python/aiohttp/save-your-pdf-to-your-amazon-s3-bucket.md","md",[135,139,143,147,151,155,159,163,167,171,175,179,183,187,191,195,199,203,204,208,212],{"_path":136,"title":137,"language":10,"library":5,"_id":138},"/guides/python/aiohttp/accessing-secured-pages","Accessing secured pages","content:guides:python:aiohttp:accessing-secured-pages.md",{"_path":140,"title":141,"language":10,"library":5,"_id":142},"/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":144,"title":145,"language":10,"library":5,"_id":146},"/guides/python/aiohttp/adding-a-text-watermark","Adding a text watermark","content:guides:python:aiohttp:adding-a-text-watermark.md",{"_path":148,"title":149,"language":10,"library":5,"_id":150},"/guides/python/aiohttp/adding-an-image-watermark","Adding an image watermark","content:guides:python:aiohttp:adding-an-image-watermark.md",{"_path":152,"title":153,"language":10,"library":5,"_id":154},"/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":156,"title":157,"language":10,"library":5,"_id":158},"/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":160,"title":161,"language":10,"library":5,"_id":162},"/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":164,"title":165,"language":10,"library":5,"_id":166},"/guides/python/aiohttp/define-a-time-limit","Define a time limit","content:guides:python:aiohttp:define-a-time-limit.md",{"_path":168,"title":169,"language":10,"library":5,"_id":170},"/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":172,"title":173,"language":10,"library":5,"_id":174},"/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":176,"title":177,"language":10,"library":5,"_id":178},"/guides/python/aiohttp/loading-css-from-a-string","Loading CSS from a string","content:guides:python:aiohttp:loading-css-from-a-string.md",{"_path":180,"title":181,"language":10,"library":5,"_id":182},"/guides/python/aiohttp/loading-css-from-a-url","Loading CSS from a URL","content:guides:python:aiohttp:loading-css-from-a-url.md",{"_path":184,"title":185,"language":10,"library":5,"_id":186},"/guides/python/aiohttp/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:python:aiohttp:loading-javascript-from-a-string.md",{"_path":188,"title":189,"language":10,"library":5,"_id":190},"/guides/python/aiohttp/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:python:aiohttp:loading-javascript-from-a-url.md",{"_path":192,"title":193,"language":10,"library":5,"_id":194},"/guides/python/aiohttp/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:python:aiohttp:protecting-the-generated-pdf.md",{"_path":196,"title":197,"language":10,"library":5,"_id":198},"/guides/python/aiohttp/receive-a-webhook-event","Receive a webhook event","content:guides:python:aiohttp:receive-a-webhook-event.md",{"_path":200,"title":201,"language":10,"library":5,"_id":202},"/guides/python/aiohttp/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:python:aiohttp:save-your-pdf-online-and-get-back-a-url.md",{"_path":4,"title":8,"language":10,"library":5,"_id":130},{"_path":205,"title":206,"language":10,"library":5,"_id":207},"/guides/python/aiohttp/send-custom-http-headers","Send custom HTTP headers","content:guides:python:aiohttp:send-custom-http-headers.md",{"_path":209,"title":210,"language":10,"library":5,"_id":211},"/guides/python/aiohttp/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:python:aiohttp:using-cookies.md",{"_path":213,"title":214,"language":10,"library":5,"_id":215},"/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",1785426833300]