npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

better-html-pdf

v0.1.4

Published

A better library to create highly customizable PDFs from HTML or URL as buffer, base64 string and .pdf file.

Downloads

243

Readme

🖥️ HTML2PDF

An easy-to-use but reliable asynchronous library to create highly customizable PDFs from HTML or URL as buffer, base64 string and .pdf file. Other than most npm-solutions for html-to-pdf conversion, this one leverages puppeteer as opposed to the deprecated and unmaintained phantomJS. It also provides far more options to configure to your liking than most similar solutions. This might be the best html-to-pdf solution on npm at this point.

Installation

npm i [-g] better-html-pdf

Available functions

Other than the main converter function, HTML2PDF exposes two more functions that allow writing pdf file content as both base64 or buffer. Those functions are also used internally to replace the situationally unreliable puppeteer-native file save function that is controlled through the path option.

Function signatures

Convert html to pdf

html2pdf(html: string, options: Object) : Promise<string | Buffer>

Convert base64 file content to PDF file

base64ToPdf(base64: string, file: string) : void

Convert file content buffer to PDF file

bufferToPdf(buffer: Buffer, file: string) : void

Usage

Using HTML2PDF is quick and simple: Import the html2pdf function and pass it the html and options.

const { html2pdf, base64ToPdf, bufferToPdf } = require('better-html-pdf');

//[...]
// convert html to PDF
const fileContentB64 = await html2pdf('<h1>Test</h1>', { avoidTableRowBreak: true, marginTop: 10, repeatTableHeader: false });
const fileContentBuffer = await html2pdf('<h1>Test</h1>', { fileType: 'buffer', url: 'https://google.com/', viewPort: '1000x700' });

//convert output to pdf file
base64ToPdf(fileContentB64, './test1.pdf');
bufferToPdf(fileContentBuffer, './test2.pdf');

Full type declarations are included so ES6 imports are available, too.

import { html2pdf } from 'better-html-pdf';

//[...]
// convert html to PDF
const fileContentB64 = await html2pdf('<h1>Test</h1>', { avoidTableRowBreak: true, marginTop: 10, repeatTableHeader: false });

Options

This solution provides a great number of options to configure for your conversion, passed as a javascript object. Find a detailed doc of all options and defaults here: Options Documentation.

{
    fileType: 'base64', // 'base64' | 'buffer'
    url: '', //if specified ignores html
    viewPort: '1920x1080', //string (width)x(height)
    timeout: 5000, //timeout for page loading in ms
    landscape: false,
    format: '', //letter | legal | tabloid | ledger | a0 | a1 | a2 | a3 | a4 | a5 | a6
    repeatTableHeader: true, //repeat html table headers on each page - note: headers only repeat when in <thead>
    repeatTableFooter: true, //repeat html table footers on each page - note: footers only repeat when in <tfoot>
    displayHeaderFooter: true,
    headerTemplate: '',
    footerTemplate: '',
    width: '1920', //document size in pixels or with units (in, mm, cm)
    height: '1080', //document size in pixels or with units (in, mm, cm)
    marginTop: 0, //num in pixels or with units (in, mm, cm)
    marginBottom: 0, //num in pixels or with units (in, mm, cm)
    marginLeft: 0, //num in pixels or with units (in, mm, cm)
    marginRight: 0, //num in pixels or with units (in, mm, cm)
    breakImages: false, //break images between pages
    avoidTableRowBreak: true, //tries avoiding breaking table rows between pages
    avoidDivBreak: false, //tries to avoid breaking divs between pages - can cause unwanted behavior
    omitBackground: false, //hide html background, allows for transparency
    pageRanges: '', //'1-12' | '3-5'
    path: '', //file save path, if empty no file is created
    disableJavascript: false, //disable javascript on the target site/html
    preferCSSPageSize: false, //css-declared page size takes precedent over format, width and height
    printBackground: true, //apply background styling
    trueColors: true, //use unaltered colors
    scale: 1, //render scale, must be between 0.1 and 2
    screenMedia: false //use 'screen' instead of 'print' CSS media
}

Dependencies

HTML2PDF depends on puppeteer.