h2pdf
v1.0.4
Published
A lightweight and easy-to-use package for generating PDF files from HTML content without the use of external dependencies.
Downloads
8
Maintainers
Readme
H2PDF
H2PDF is a Node.js package that allows you to easily convert HTML to PDF in your Express.js applications. It uses a custom-built PDF generation engine and does not rely on Puppeteer or any other third-party PDF generation tools.
Installation
You can install H2PDF using npm:
npm install h2pdf
Usage
Using H2PDF is very simple. Here's an example of how you can use it in your Express.js app:
const express = require('express');
const app = express();
const h2pdf = require('h2pdf');
app.get('/pdf', async (req, res) => {
const html = '<h1>Hello, World!</h1>';
const pdf = await h2pdf(html);
res.contentType('application/pdf');
res.send(pdf);
});
app.listen(3000, () => console.log('Server started on port 3000'));
In this example, we define a route that generates a PDF file from an HTML string and sends it as a response to the client.
API
H2PDF exports a single function with the following signature:
async function h2pdf(html: string, options: object = {}): Promise<Buffer>
The html
parameter is the HTML string that you want to convert to a PDF.
The options
parameter is an optional object that allows you to customize the PDF generation process. Here are the available options:
pageSize
: A string or object that specifies the size of the PDF page. You can use any of the predefined page sizes (e.g. 'A4', 'Letter', etc.) or define your own custom size using an object with width and height properties (in millimeters).
pageOrientation
: A string that specifies the orientation of the PDF page ('portrait' or 'landscape').
marginTop
, marginBottom
, marginLeft
, marginRight
: Numbers that specify the size of the margins (in millimeters).
headerTemplate
, footerTemplate
: Strings that specify the content of the header and footer sections of the PDF.
displayHeaderFooter
: A boolean that specifies whether to display the header and footer sections of the PDF.
printBackground
: A boolean that specifies whether to print the background colors and images of the HTML.
scale
: A number that specifies the scaling factor for the PDF.
License
H2PDF is licensed under the MIT License.