
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
simple-pdf-generator
Advanced tools
Node module that coverts HTML5 files to PDFs.
This is a Node.js module available through the npm registry. Before installing, download and install Node.js.
Installation is done using the npm install command:
$ npm install simple-pdf-generator
Simple PDF Generator:
To create a template you need to, create a .ts file with the class:
import path from 'path';
import { PdfField, PdfFiller, PdfTable, PdfTemplate } from 'simple-pdf-generator';
@PdfTemplate({
templatePath: path.join(__dirname, 'template.html')
})
export class Template extends PdfFiller {
@PdfField()
firstField = '';
@PdfField()
secondField = '';
}
Add and html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container-fluid">
<div class="container">
<div class="row justify-content-center">
<div class="col-10 text-center">
<h1>Hello %%firstField%%</h1>
<h2>Welcome in %%secondField%%</h2>
</div>
</div>
</div>
</div>
</body>
</html>
Then it's finally possible to use the template:
import path from 'path';
import { Template } from './Template';
(async () => {
const doc = new Template();
doc.firstField = 'World';
doc.secondField = 'Simple PDF Generator';
doc.fill(path.join(__dirname, 'doc.pdf'));
})();
To generate tebles is necessary to declare a PdfTable in the class:
import path from 'path';
import { PdfField, PdfFiller, PdfTable, PdfTemplate } from 'simple-pdf-generator';
@PdfTemplate({
templatePath: path.join(__dirname, 'template.html'),
})
export class Template extends PdfFiller {
@PdfField({ fieldName: 'field' })
field = '';
@PdfTable({ fieldName: 'table' })
table = '';
}
In the HTML file write something like this:
<inject-table :items="data" class="table customClass">
<inject-column prop="index" label="#" />
<inject-column prop="name" label="Name" />
<inject-column prop="surname" label="Surname" />
<inject-column prop="email" label="Email" />
</inject-table>
And then use it like this:
import path from 'path';
import { Template } from './Template';
(async () => {
const doc = new Template();
doc.field = 'World';
doc.table = [
{ index: 1, name: 'James', surname: 'Smith', email: 'james@smith.com' },
{ index: 2, name: 'Robert', surname: 'Johnson', email: 'robert@johnson.com' },
];
doc.fill(path.join(__dirname, 'doc.pdf'))
})();
Through the class decorator is possible to include css and js files. Do not import them in the HTML file, they will be automatically imported.
import path from 'path';
import { PdfField, PdfFiller, PdfTable, PdfTemplate } from 'simple-pdf-generator';
@PdfTemplate({
templatePath: path.join(__dirname, 'template.html'),
includes: [
{ path: path.join(__dirname, 'template.css') },
{ path: path.join(__dirname, 'template.js') },
]
})
export class Template extends PdfFiller {
@PdfField({ fieldName: 'firstField' })
firstField = '';
@PdfField({ fieldName: 'secondField' })
secondField = '';
}
PdfFiller fill
function accept two parameters (that can be omitted):
outputPath
for the generated file;pdfOptions
that accept puppeteer options;PdfFiller fill
function returns the buffer of the PDF file.
The pdfOptions
parameter could also be set in the class decorator.
For puppeteer PDF options look at their documentation, we show only a brief example:
pdfOptions = {
background: true,
displayHeaderFooter: true,
headerTemplate: `
<style>
div { margin-left: 15px !important }
</style>
<div>Header</div>
`,
footerTemplate: `
<div>Footer</div>
`,
margin: {
top: '5cm',
bottom: '4cm',
left: '1cm',
right: '1cm'
},
};
@PdfTemplate({
templatePath: path.join(__dirname, 'template.html'),
includes: [
{ path: path.join(__dirname, 'template.css') },
{ path: path.join(__dirname, 'template.js') },
],
pdfOptions: pdfOptions,
})
export class Template extends PdfFiller {
@PdfField({ fieldName: 'firstField' })
firstField = '';
@PdfField({ fieldName: 'secondField' })
secondField = '';
}
const doc = new Template();
doc.firstField = 'World';
doc.secondField = 'Simple PDF Generator';
doc.fill(path.join(__dirname, 'doc.pdf'), pdfOptions);
This library is developed by:
FAQs
Generator of PDF files from HTML templates using TS decorators
The npm package simple-pdf-generator receives a total of 17 weekly downloads. As such, simple-pdf-generator popularity was classified as not popular.
We found that simple-pdf-generator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.