2.1.1 • Published 6 months ago

@abhisek507/html2multipagepdf v2.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

html2multipagepdf v2.1.1

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.

Live Demo | Example A | Example B

Installation

Install npm globally (ignore if already installed):

npm i -g npm

Install the html2multipagepdf library:

npm i @abhisek507/html2multipagepdf@2.1.1

Setup

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

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

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 className='flex align-items-center gap-4'>
          <span>Page</span>
          <span className={CustomPageNoElementSelector}></span>
        </div>
      </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, PageConfig } 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%
  /**
   * @type {PageConfig}
   */
  const pageConfig = {
    quality,
    alignCenter: true,
  };

  try {
    const pdf = await generatePDF(pageSelectors, pageOptions, elementSelector, pageConfig);
    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.

Display page number

We can either display each page number or custom page number,

  1. PageNoElementSelector
  2. CustomPageNoElementSelector

PageNoElementSelector

The page no will be conted from first to last. Whenever the className PageNoElementSelector will be there on a page, it will be updated with the page no.

CustomPageNoElementSelector

The CustomPageNoElementSelector will allow us to display the page numbers in a controlled sequence. For eg. page 1 & 3 have the className CustomPageNoElementSelector, then page 1 will be considered as page 1 but page 3 will be considered as page 2.

Credits

  1. html2canvas
  2. jsPDF

Source Code

See the package source for more details.

Support

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

2.1.1

6 months ago

2.1.0

10 months ago

2.0.9

10 months ago

2.0.8

11 months ago

2.0.7

11 months ago

2.0.6

11 months ago

2.0.5

11 months ago

2.0.4

11 months ago

2.0.3

11 months ago

2.0.2

11 months ago

2.0.1

11 months ago

2.0.0

11 months ago

1.0.0

11 months ago