@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
2
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;
}
}