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

pdf-pager

v0.0.13

Published

A library for rendering HTML to PDF, with support for headers and footers.

Downloads

6

Readme

pdf-pager

A library for rendering HTML to PDF, with support for headers and footers.

Why?

I wanted a modern way to develop PDFs with static headers and footers, and I wanted to use a modern browser engine to do it.

I tried a few solutions but was unable to find anything which worked exactly like how I wanted.

How it works

  • The process begins by rendering the HTML template using Puppeteer.
  • Extracts and measures the dimensions of the header and footer, capturing each as a separate PDF.
  • Rerenders the template excluding the header and footer.
  • Captures the main content as a PDF, with careful attention to correct pagination.
  • Calculates the height of the main content area by subtracting the combined heights of the header and footer from the total paper height.
  • Ensures the calculated height is valid; throws an error if it's not.
  • Captures the main content as a PDF using the adjusted height.
  • Creates a new PDF document and loads the content from the main content PDF.
  • Iterates through each page:
    • Adjusts each page's size to accommodate the header and footer.
    • If available, embeds the header at the top of the page.
    • If available, embeds the footer at the bottom of the page.
    • Embeds the main content between the header and footer.
  • Saves and returns the modified document.

Usage

import {PDFPager} from "pdf-pager";

// see tests/examples/invoice.html
const file = await PDFPager.create().fromFile(path.join(__dirname, 'invoice.html'));
await fs.promises.writeFile('invoice.pdf', file);

Template

The template should contain a #header and #footer element. These will be used to capture the header and footer.

Future Plans

  • Support page numbers

Other Solutions

  • https://wkhtmltopdf.org/ -- Uses WebKit to render HTML to PDF
  • https://www.princexml.com/ -- Commercial solution, uses an older browser engine
  • https://pdfkit.org/ -- Draw PDFs using a canvas like API
  • https://weasyprint.org/ -- Not browser based, no JS support

References

  • https://github.com/Hopding/pdf-lib/issues/128
  • https://github.com/Hopding/pdf-lib/issues/143 -- PaperSizes