The PDF document will be served in the HTTP response from the HTTP API endpoint. The isBase64Encoded flag tells the API Gateway service to render binary content. Uses wkhtmltopdf on the back-end which renders HTML using Webkit. The body field contains the PDF document encoded as a Base64 string. Here are the response values needed to return a PDF document from the Lambda runtime. Returning PDFs from AWS Lambda FunctionsĪWS Lambda functions control API Gateway responses using a custom JSON response format. HTTP APIs simply need to provide a isBase64Encoded flag in the API response. REST APIs need manual configuration for media types and content handling to support binary responses. Setting up a HTTP endpoint to return binary content is simpler using HTTP than REST APIs. no request validation, X-Ray support or private endpoints. HTTP APIs (announced in 2019) are simpler, cheaper and faster than the older-style REST APIs, but with a restricted set of features, e.g. bind( buffers))īinary Content with HTTP APIs using AWS API GatewayĪWS’s API Gateway service supports two types of API endpoints - HTTP or REST APIs. Here is all the JavaScript you need to create this custom PDF.Ĭonst options = Ĭonst doc = new PDFDocument( options). The library had a really simple API for generating PDFs from dynamic content. No native libraries to compile or external dependencies needed. It was a good choice for a serverless application for the following reasons: There are numerous Node.js libraries for generating PDFs including PDFKit, pdf-lib and HummusJS. Here’s how I did it… Creating custom PDF documents in Node.js I hadn’t previously built anything that generated PDFs dynamically (let alone using serverless) but discovered how easy this was to do on AWS Lambda using PDFKit and HTTP APIs with API Gateway. API responses had to include the document’s binary contents directly, rather than forwarding to an external storage service. PDFs would be accessed via a HTML link from an internal web-app. Custom PDFs needed to be created in real-time with dynamic content from an internal data source. I recently had this exact use-case on a client project. We have considered HTML data to be from a URL, local file or a string.How can you return dynamically generated PDF documents as HTTP responses from serverless functions? Our setup: we are using a ruby on rails backend, loading the PDF Kit gem Our front end is uploading images and sending them to our RAILS API to generate the HTML document Our controllers use the uploaded images to generate the HTML document that gets fed into PDF Kit Helper in controller: dochelper. We have successfully converted a HTML data to PDF. om_string(htmlstr, 'sample.pdf') Run Summary Var htmlstr = 'Heading 2Sample paragraph.' If your HTML data is stored in a Python variable, you can use from_string() function and convert the HTML string to PDF. om_file('local.html', 'sample.pdf') Run Example 2: Convert HTML String to PDF If your HTML file is stored locally, you can use from_file() function and convert the local HTML file to PDF. Output pdf file would look like Example 2: Convert HTML to PDF from Local File The converted PDF file is saved to the current path in the command prompt or terminal. Now that the environment is setup, following is a simple example to convert HTML to PDF, where HTML is downloaded from a URL. Restart the command prompt, if you are running the python program using command prompt python command for the Path to take effect. Adding wkhtmltopdf/bin to Path environment variable. For example, C:\Program Files\wkhtmltopdf\bin. We should add bin folder to the system PATH variable in Environment Variables. It would be mostly installed at the path C:\Program Files\wkhtmltopdf. Once downloaded, double click on the binary file and continue with the installation. The file size would be around 25MB and takes a moment to download. Windows users can download wkhtmltopdf from this official github repository wkhtmltopdf. Ubuntu or Debian users can install wkhtmltopdf using below apt-get command. pip install pdfkit Run Install pdfkit library Install wkhtmltopdf To install pdfkit, run the following pip command.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |