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

@abhisek507/html2multipagepdf

v2.0.8

Published

Convert a pretty looking HTML template into a multipage PDF file.

Downloads

733

Readme

html2multipagepdf v2.0.8

Easily convert HTML templates to PDF in React.js/Next.js. Generate dynamic, server-side PDFs for invoices, reports, or documents with seamless integration into your React.js/Next.js application.

Installation

Using npm:

$ npm i -g npm
$ npm i @abhisek507/[email protected]

In React.js/Next.js/Vanilla JavaScript:

import { generatePDF } from "@abhisek507/html2multipagepdf";
import {
  A4,
  Quality,
  RegularPageSelector,
  PageWithMaxPossibleWidthSelector,
  PageElementSelector,
  JSPDFOutputType,
} from "@abhisek507/html2multipagepdf/constants";

Setup

CSS styles:

.hidden {
  display: none;
}

Wrap the JSX/HTML template:

const JSX = () => (
  <div className="">
    <div className={`flex flex-col ${RegularPageSelector}`}>
      <div className="header">
        {/* header section */}
      </div>

      <div className={`${PageElementSelector}`}>
        {/* content row 1 */}
      </div>

      <div className={`${PageElementSelector}`}>
        {/* content row 2 */}
      </div>

      {/* ... */}

      <div className={`${PageElementSelector}`}>
        {/* content row n */}
      </div>

      <div className="footer">
        {/* footer section */}
      </div>
    </div>

    <div className={`flex flex-col ${PageWithMaxPossibleWidthSelector}`}>
      <div className="header">
        {/* header section */}
      </div>

      <div className={`${PageElementSelector}`}>
        {/* content row 1 */}
      </div>

      <div className={`${PageElementSelector}`}>
        {/* content row 2 */}
      </div>

      {/* ... */}

      <div className={`${PageElementSelector}`}>
        {/* content row n */}
      </div>

      <div className="footer">
        {/* footer section */}
      </div>
    </div>
  </div>
);

Generate a PDF

import { generatePDF } from "@abhisek507/html2multipagepdf";
import {
  A4,
  Quality,
  RegularPageSelector,
  PageWithMaxPossibleWidthSelector,
  PageElementSelector,
  JSPDFOutputType,
} from "@abhisek507/html2multipagepdf/constants";

const handleGeneratePDF = async () => {
  const pageSelectors = [RegularPageSelector, PageWithMaxPossibleWidthSelector];
  const pageOptions = A4; // A4 || Letter || Legal
  const elementSelector = PageElementSelector;
  const quality = Quality[100] // quality can be 100% || 90% || 80% ... || 10%

  try {
    const pdf = await generatePDF(pageSelectors, pageOptions, elementSelector, quality);
    const time = new Date().getTime();
    pdf.save(`${name}-${time}.pdf`);

    // base64 image
    const output = pdf.output(JSPDFOutputType.datauristring);
    return output;
  } catch (e) {

  }
};

What are pageSelectors?

A page selector is a css class will be used by the script to determine a page. There are 2 types of pageSelectors,

  1. RegularPageSelector
  2. PageWithMaxPossibleWidthSelector

RegularPageSelector

The PDF pages generated using RegularPageSelector can have dis-similar content width. But the script will try to utilize most of the available PDF page width for the content.

PageWithMaxPossibleWidthSelector

The PDF pages will have exact same content width. To achieve this behaviour the script might not use most of the PDF page width.

For e.g: 2 pages are generated using PageWithMaxPossibleWidthSelector. The 1st page content has a width A & 2nd page content has a width B.

Let's consider A is smaller than B then both the page contents will have width A to make sure they are of exact same width.

Best practices

  1. We must use PageElementSelector inside the PageWithMaxPossibleWidthSelector or RegularPageSelector.
  2. As many PageElementSelector we use inside 1 pageSelector the more better PDF output we can expect.
  3. The PageElementSelector must not have more height than the available page height.

Source Code

See the package source for more details.

Support

Tested in Chrome 131, Firefox 133, Safari 18, DuckDuckGo 1.118.0.