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

@aeco-cloud/pdf-generator

v1.0.6

Published

Node package to generate PDF documents on the server. The package uses [pug](https://pugjs.org/api/getting-started.html) as a templating language to create dynamic html files and the aeco `pdf-api` to convert the html to a pdf file.

Downloads

15

Readme

PDF Generator

Node package to generate PDF documents on the server. The package uses pug as a templating language to create dynamic html files and the aeco pdf-api to convert the html to a pdf file.

How it works

const { pdf, html } = await pdfGenerator({
    pugPath: "path/to/your/pug/file.pug", // required 
    apiKey: "yourApiKey", // required 
    pdfSavePath: "path/to/a/location/to/save/your/document.pdf", // optional 
    htmlSavePath: "path/to/a/location/to/save/your/document.html" // optional 
    imagesPath: "path/to/a/directory/that/contains/images", // optional 
    data: {firstName:"Bob", lastName:"De Bouwer"}, // optional 
    apiEndpoint: "https://custom.apiendpoint.com", // optional
  });

Arguments:

  • pugPath: the path to your pug template
  • apiKey: the api key to authenticate in the pdf-api
  • pdfSavePath: (optional) if specified, your pdf will be saved in this location
  • htmlSavePath: (optional) if specified, the html rendered by the pugfile and your data and images will be saved in this location. This can be usefull for debugging purposes.
  • imagesPath: (optional) if specified, images in this directory will be available to reference in your pug file by using the syntax: img(src=images["imagename.jpg"])
  • data: (optional) if specified, the values from this object will be available to reference in your pug fuile by using the syntax: #{data.firstName}
  • apiEndpoint: (optional) if specified, this value will overwrite the default api endpoint

Returned values:

If the promise is resolved, the pdf is returned as a buffer and the html file is returned as a string.

If a pdfSavePath is specified, you can work with the saved pdf file instead of the buffer. This means that the following code can also be used:

await pdfGenerator({args..., pdfSavePath:"path/to/your/document.pdf"})

Full example

1) create a pug template

//- template.pug
doctype html
html(lang="en")
  head
    title= pageTitle
    style
      include styles.css
  body
    h1 This is a test template
    p
      | my first name is #{data.firstName}
      | and my last name is #{data.lastName}
    p And ow, how could I forget, here is a picture of an angry cat
    p
      img(src=images["cat.jpg"] width="300px")
    .pagebreak
    p
      | and this content is on the next page!

2) create styles for the template

@import url("https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap");
@page {
  size: 21cm 29.7cm;
  margin: 20mm 20mm 20mm 20mm;
}

@media print {
  .pagebreak {
    page-break-before: always;
  }
}

body {
  font-family: "Roboto", sans-serif;
  font-weight: 300;
}

h1 {
  color: red;
  text-align: center;
  font-weight: 500;
  font-size: 23px;
}

p {
  line-height: 150%;
  font-size: 15px;
  text-align: left;
  color: black;
  font-weight: 300;
}

3) save your images in a directory

images
├──cat.jpg

4) create a data object

const data = {firstName:"Bob", lastName:"De Bouwer"}

Your file structure should now be as follows:

.
├── styles.css
├── template.pug
├── images
│   ├── cat.jpg

5) Execute the pdfGenerator

await pdfGenerator({
    pugPath: "./template.pug", 
    apiKey: "yourApiKey", 
    pdfSavePath: "./document.pdf", 
    imagesPath: "./images", 
    data: {firstName:"Bob", lastName:"De Bouwer"}, 
  });

and the document.pdf will be saved to your disk.

CSS Tips

If you want to use SASS you will first have to compile your scss files to css since pug only accepts css stylesheets.

Use the following css code to make your pdf A4 format and use pagebreak:

@page {
  size: 21cm 29.7cm;
  margin: 20mm 20mm 20mm 20mm;
}

@media print {
  .pagebreak {
    page-break-before: always;
  }
}