While there are helpful guides available elsewhere and many StackOverflow answers, we found the learning curve to be steep. The jsPDF documentation is entirely focused on the method definitions and lacks tutorials for using these methods. html2canvas also doesn't work in Node.js, though standard jsPDF does. html2canvas supports a wide range of CSS properties, but is still considered experimental and not recommended for production usage. It is primarily focused on creating PDFs in an element-by-element, object-by-object style, but it does support HTML to PDF rendering through the html2canvas library. JsPDF is one of the most popular JavaScript PDF generators. It's best for less complex documents, but still superior to any other HTML-to-PDF libraries. The only downside is Chrome's lack of PDF-specific functionality such as headers and footers, accessible PDFs, page float, to name just a few. It's the most popular open-source HTML-to-PDF converter and has the best support for modern HTML, CSS, and JavaScript. Puppeteer is a Node.js (no client-side support) library for accessing headless Chromium. We'll specifically note if a library is only server-side or client-side. The large majority of libraries support both server-side Node.js and client-side JavaScript, so we've created just one guide to cover both. We'll review the most popular Node.js and JavaScript HTML to PDF libraries, as well as a few non-HTML-based and even non-JavaScript-based options. We haven't tried all these libraries in a production environment, so please do further testing and evaluation on your document-but we believe our shared PDF knowledge provides useful insight. Our goal is to speed up your research and save you pain. Creating PDFs from HTML can be surprisingly different than creating standard web pages. exports.We'll use our extensive experience in PDF generation to compare the most popular JavaScript-based libraries. 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. 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. How can you return dynamically generated PDF documents as HTTP responses from serverless functions?
0 Comments
Leave a Reply. |