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

@permees/htmltopdf

v1.0.4

Published

This package is used to convert html to pdf

Downloads

209

Readme

HTML to PDF Converter

A package that converts HTML to PDF using jsPDF and html2canvas, optimized for handling large HTML documents.

Features

  • Convert HTML to PDF with high fidelity
  • Efficiently handles large HTML documents
  • Built on top of jsPDF and html2canvas
  • Easy to use API
  • Customizable options

How it works

This package uses html2canvas to render the HTML content to a canvas element, which is then converted to a PDF document using jsPDF. The challenge with large HTML documents is that while html2canvas can handle large HTML elements, browser limitations often prevent rendering these large elements to a single canvas. An approach of cutting the HTML element page by page for rendering leads to poor performance. Our package addresses this by reducing the number of calls to html2canvas as much as possible. It achieves this by splitting the canvas element when it reaches browser limitations, rather than splitting the HTML itself. This approach significantly improves performance for large documents.

Browser limitations:

Chrome: Maximum height/width: 32,767 pixels Maximum area: 268,435,456 pixels (e.g., 16,384 x 16,384)

Firefox: Maximum height/width: 32,767 pixels Maximum area: 472,907,776 pixels (e.g., 22,528 x 20,992)

Internet Explorer: Maximum height/width: 8,192 pixels Maximum area: N/A

Safari: Maximum height/width: 4,096 pixels Maximum area: 16,777,216 pixels

Package limitations

For optimal performance, this package splits the canvas without regard for specific element boundaries. As a result, you need to ensure that all pages have the same height. If you have multiple pages with different heights, we recommend pre-processing the HTML element to standardize page heights before using this package.

Installation

npm install @permees/htmltopdf

yarn add @permees/htmltopdf

pnpm intall @permees/htmltopdf

Usage

import { htmlToPdf } from '@permees/htmltopdf';


const Component = () => {
  const ref = useRef(null)
  const onClick = () => {
    htmlToPdf(ref.current, {
      fileName: `report.pdf`,
      html2canvasOptions: {
        scale: 2,
        //... add any other html2canvas options
      },
      jsPDFOptions: {
        orientation: 'landscape',
        //... add any other jsPDF options
      },
      heightPerPage: 840,
      onBeforeCapture: () => {
        const style = document.createElement('style')
        document.head.append(style)
        style.sheet?.insertRule('body p { display: inline-block; font-size: 12px; }')
      },
      onSuccess: () => {
        toast.success('Report downloaded successfully')
      },
      onError: () => {
        toast.error('Error downloading report')
      },
      onSettled: () => {
        toast.info('The process has been completed')
      },
    })
  }
  return (
    <div>
      <div ref={ref}>
        <h1>HTML to PDF Converter</h1>
        <p>This package converts HTML to PDF using @permees/htmltopdf.</p>
      </div>
      <button onClick={onClick}>Download PDF</button>
    </div>
  )
}

API

  • element: HTMLElement: The HTML element to be converted to PDF
  • options: TGeneratePDFOptions: An object containing the following properties:

TGeneratePDFOptions

import { Options as THtml2canvasOptions } from "html2canvas";
import { jsPDFOptions as TJsPDFOptions } from "jspdf";
type TGeneratePDFOptions = {
  fileName?: string;
  heightPerPage?: number;
  html2canvasOptions?: Partial<THtml2canvasOptions>;
  jsPDFOptions?: Partial<TJsPDFOptions>;
  onSuccess?: () => void;
  onError?: () => void;
  onSettled?: () => void;
  onBeforeCapture?: () => void;
};
  • fileName: The name of the PDF file to be downloaded
  • html2canvasOptions: Options to be passed to html2canvas
  • jsPDFOptions: Options to be passed to jsPDF
  • heightPerPage: The height of each page in the PDF document
  • onBeforeCapture: A callback function to be executed before capturing the HTML element
  • onSuccess: A callback function to be executed after the PDF has been successfully generated
  • onError: A callback function to be executed if an error occurs during PDF generation
  • onSettled: A callback function to be executed after the PDF generation process has been completed

Why choose this package?

Unlike several other libraries with similar functionality, this package is specifically designed to handle large HTML documents efficiently. It employs optimized rendering techniques to ensure smooth conversion of complex and extensive HTML content to PDF format.

License

This project is licensed under the MIT License