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

convert-to-pdf

v4.4.2

Published

Convert html to pdf using puppeteer

Downloads

992

Readme

convert-to-pdf

Convert html to pdf using puppeteer

The module converts the given HTML template to PDF. The module uses puppeteer for creating PDFs and mustache for templates

Soon to come: ejs to PDF

Methods

  • htmlToPdf

        htmlToPdf(options: HtmlToPdfOptions): Promise<Buffer>

Options available

const options = {
    // executable path for Puppeteer. Default path provided by puppeteer will be used if this option is not provided.
    puppeteerExecPath: 'Puppeteer executable path',
    // page options(used for rendering the content via puppeteer)
    page: { // OPTIONAL
        height: height of page - number - (default: 1600),
        width: width of page - number - (default: 745.60),
    },
    // PDF options(used while creating the PDF)
    pdf: { // OPTIONAL
        writeStream: for stream of PDF - NodeJS.WritableStream - (default: none(i.e. return PDF as Buffer)),
        path: The path to save the file to - string - If the path is relative, it's resolved relative to the current working directory - (default: '' which means the PDF will not be written to disk),
        dimensions: { will be ignored if format option is provided
          width: PDF width in pixes - number or string with px as unit - Example: 300 or '300px',
          height: PDF height in pixes - number or string with px as unit - Example: 300 or '300px',
        },
        scale: Scale of the webpage rendering - number - (default: 1) - value must be  between 0.1 and 2,
        format: Paper format - PaperFormat,
        landscape: Paper orientation - boolean - (default: false),
        margin: Paper margin - object - (default: none) - keys: top, bottom, right, left,
        printBackground: Print Page background on PDF? - boolean - (default: false),
        transparentBackground: Transparent background on PDF? - boolean - (default: false),
    },
    // Template options(used while rendering by puppeteer)
    template: {
      type: Type of html template - string - values: FILE/CONTENT - (default: CONTENT)
      content: html template - string - (file path if type is FILE or HTML string if type is CONTENT)
      css: {
          type: Type of css content - string - values: FILE/CONTENT/URL - (default: CONTENT),
          content: css style sheet - string - (file path if type is FILE or CSS string if type is CONTENT or URL )
      },
      script: {
          type: Type of script content - string - values: FILE/CONTENT/URL - (default: CONTENT),
          content: javascript code - string - (file path if type is FILE or code string if type is CONTENT or URL )
      },
      header: HTML template for the print header. Should be valid HTML markup. the following classes can be used to inject printing values:
        - `date` formatted print date
        - `title` document title
        - `url` document location
        - `pageNumber` current page number
        - `totalPages` total pages in the document
      footer: HTML template for the print footer. Should use the same format as the header.
      partials: MustacheJs partials (sub-templates) - Object<string, string> - Example - {
        [templateName]: [template content]
      }
    },
    // Url options
    url: {
      link: URL to render - string
      auth: Authentication for the given url(if required) - object - keys: username, password
    }
    data: Data to render on template - object,
    // Additional data to render on template. For example, Can be used to provide translations on the template. Check the second example below
    additionalData: {
      resourceType: Type of resource data - string - values: FILE/CONTENT - (default: CONTENT),
      data: Data to render - object | string - (file path if type is FILE or JSON object if type is CONTENT )
    }
}

At least one of template or url must be specified.

For more information about the options, see the documentation for puppeteer here

Examples

import { htmlToPdf } from 'convert-to-pdf';

const options = {
  // template options
  template: {
    type: 'FILE', // If the template is in the form of a file
    content: path.resolve(__dirname, 'index.html'),
    css: {
      type: 'FILE',
      content: path.resolve(__dirname, 'index.css'),
    },
  },
  // data to render on the template
  data: {
    name: 'John Doe',
  },
};
const pdf = await htmlToPdf(options);
// here pdf is in the form of Buffer
import { htmlToPdf } from 'convert-to-pdf';

const options = {
  pdf: {
    writeStream: res, // http response as writable stream
  },
  // template options
  template: {
    type: 'CONTENT', // If the template in in the form of a file
    content: `
    <!DOCTYPE html>
      <html>
      <head>
          <meta charset='utf-8'>
          <meta http-equiv='X-UA-Compatible' content='IE=edge'>
          <title>Page Title</title>
          <meta name='viewport' content='width=device-width, initial-scale=1'>
      </head>
      <body>
          <h1>{{HELLO}} {{name}}!</h1>
      </body>
    </html>
`,
    css: {
      type: 'CONTENT',
      content: `
        h1 {
          color: #f00;
        }
      `,
    },
  },
  // data to render on the template
  data: {
    name: 'John Doe',
  },
  // additional data, used here as translations key/value
  additionalData: {
    resourceType: 'CONTENT',
    data: {
      HELLO: 'Hej',
    },
  },
};
await htmlToPdf(options);
// Here PDF will be piped to the specified writable stream
  • getDataRenderedTemplate

        getDataRenderedTemplate(options: RenderOptions): Promise<string>

Options available

const options = {
    template: {
      type: Type of template - string - values: FILE/CONTENT - (default: CONTENT)
      content: template content - string - (file path if type is FILE or string if type is CONTENT)
    },
    data: Data to render on template - object,
    // Additional data to render on template. For example, Can be used to provide translations on the template. Check the example above
    additionalData: {
      resourceType: Type of resource data - string - values: FILE/CONTENT - (default: CONTENT),
      data: Data to render - object | string - (file path if type is FILE or JSON object if type is CONTENT )
    }
  }