1.1.4 • Published 2 years ago
html2pdf-lambda v1.1.4
html2pdf-lambda
Plugin for convert HTML page to PDF generation library for AWS Lambda
Note: This plugin will convert html string into PDF. This will work with Node.js and Lambda
Installation
npm install html2pdf-lambdaUsage
To convert HTML page to PDF using generatePdf method:
const pdf = require('html2pdf-lambda');
module.exports.handler = async (event) => {
const options = { format: 'A4' };
const html = "<h1>Welcome to html2pdf-lambda</h1>";
const pdfBuffer = pdf.generatePdf(html, options)
console.log("PDF Buffer: ", pdfBuffer);
return {
statusCode: 200,
body: JSON.stringify({
pdf: pdfBuffer,
}),
};
}generatePdf ( html, options )
Parameters
html <string> HTML file content of the PDF.
options <Object> Options object should have the following properties:
args<Array<string>> Additional arguments to pass to the browser instance. The list of Chromium flags can be found here. This options will overwrite the default arguments.path<string> The file path to save the PDF to. Ifpathis a relative path, then it is resolved to current working directory. If no path is provided, the PDF won't be saved anywhere.scale<number> Scale of the webpage rendering. Defaults to1. Scale amount must be between 0.1 and 2.displayHeaderFooter<boolean> Display header and footer. Defaults tofalse.headerTemplate<string> HTML template to print the header. Should be valid HTML markup with following classes used to inject printing values into them:dateformatted datetitledocument titleurldocument locationpageNumbercurrent page numbertotalPagestotal pages in the document
footerTemplate<string> HTML template to print the footer. Should use the same format as theheaderTemplate.printBackground<boolean> Print background graphics. Defaults tofalse.landscape<boolean> Paper orientation. Defaults tofalse.pageRanges<string> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.format<string> Paper format. If set, takes priority overwidthorheightoptions. Defaults to 'Letter'.width<string|number> Paper width, accepts values labeled with units.height<string|number> Paper height, accepts values labeled with units.margin<Object> Paper margins, defaults to none.preferCSSPageSize<boolean> Give any CSS@pagesize declared in the page priority over what is declared inwidthandheightorformatoptions. Defaults tofalse, which will scale the content to fit the paper size.
Return value
Promise which resolves with PDF buffer.
More Examples
You can see more examples here.