Socket
Socket
Sign inDemoInstall

h2pdf

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    h2pdf

A lightweight and easy-to-use package for generating PDF files from HTML content without the use of external dependencies.


Version published
Weekly downloads
1
Maintainers
1
Install size
4.38 kB
Created
Weekly downloads
 

Readme

Source

H2PDF

npm dependents install size Downloads NPM Version run on repl.it

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.

Keywords

FAQs

Last updated on 31 Mar 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc