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-from

v0.2.2

Published

Easily generate full-featured PDF files from HTML and other web formats

Downloads

515

Readme

pdf-from

Easily generate full-featured PDF files from HTML and other web formats

NPM Version Node Requirement License Number of Downloads

This package is for rendering PDF files on the server, not on the client. If any resources are not accessible to the server, then they will not be included in the resulting PDF.

Installation

To use pdf-from in your Node app:

npm

npm install --save pdf-from

yarn

yarn add pdf-from

Usage

PDF from Html

Basic Example

const pdfFrom = require("pdf-from");

const htmlString = `
<html>
    <body>
        <h1>Hello PDF</h1>
        <p>This is a PDF</p>
    </body>
</html>
`
await pdfFrom.html(htmlString);

Express Example

This example renders a template, converts the resulting HTML to a PDF, and then sends the PDF back to the browser.

const pdfFrom = require("pdf-from");

const template = "my_pug_template";
res.render(template, structuredData, async (err, htmlString) => {
    const applicationPdf = await pdfFrom.html(htmlString);
    if (applicationPdf && Buffer.isBuffer(applicationPdf)) {
        res.type("application/pdf");
        res.send(applicationPdf);
    } else {
        res.status(404).send("PDF not found");
    }
});

Example of Changing PDF Options

const pdfFrom = require("pdf-from");

const htmlString = `
<html>
    <body>
        <h1>Hello PDF</h1>
        <p>This is a PDF</p>
    </body>
</html>
`
await pdfFrom.html(htmlString, {
    displayHeaderFooter: true,
    headerTemplate:
        "<span class='title'></span> - <span class='date'></span>"
    footerTemplate:
        "<span class='pageNumber'></span> of <span class='totalPages'></span>",
    format: "Legal",
    landscape: true,
    margin: {
        top: '100px',
        right: '20px',
        bottom: '10px',
        left: '20px'
    },
    scale: 1.25
});

Available Options

  • wrapOutput normally a buffer is returned with the raw PDF data, this switch causes the PDF data to be wrapped in an object: { pdf: [Buffer] }
  • useScreenMedia normally the print styles will be used if they are available, this switch causes any print styles to be ignored

The package also respects all of the Puppeteer PDF rendering options.

Caveats

Docker

The default [node Docker image](node docker image yarn) does not come bundled with all the libraries required by this package, they have to be added manually. The following needs to be added to your Dockerfile

# Install Chromium dependencies
RUN apt-get update && \
    apt-get install -yq \
        ca-certificates \
        fonts-liberation \
        gconf-service \
        libappindicator1 \
        libappindicator3-1 \
        libasound2 \
        libatk1.0-0 \
        libc6 \
        libcairo2 \
        libcups2 \
        libdbus-1-3 \
        libexpat1 \
        libfontconfig1 \
        libgcc1 \
        libgconf-2-4 \
        libgdk-pixbuf2.0-0 \
        libglib2.0-0 \
        libgtk-3-0 \
        libnspr4 \
        libnss3 \
        libpango-1.0-0 \
        libpangocairo-1.0-0 \
        libstdc++6 \
        libx11-6 \
        libx11-xcb1 \
        libxcb1 \
        libxcomposite1 \
        libxcursor1 \
        libxdamage1 \
        libxext6 \
        libxfixes3 \
        libxi6 \
        libxrandr2 \
        libxrender1 \
        libxss1 \
        libxtst6 \
        lsb-release \
        xdg-utils wget