Skip to content

Commit

Permalink
refactor code example
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongtrh committed Jun 2, 2022
1 parent 04c1185 commit 2d90d32
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
105 changes: 62 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,23 @@ Generate a simple PDF invoice from HTML using [puppeteer](https://github.com/Goo
## How to use

- Run `npm install` to install package in package.json
- Run `node pdf.js` to generate invoice.pdf
- Run `node example.js` to generate invoice.pdf

## The PDF Invoice from HTML

1. Prepare content html (invoice.html)
2. Using handlebars to binding data to content html
3. Using Puppeteer to generate pdf from final html

index.js

```js
const fs = require("fs");
const path = require("path");
const puppeteer = require("puppeteer");
const handlebars = require("handlebars");

(async () => {
var dataBinding = {
items: [
{
name: "item 1",
price: 100,
},
{
name: "item 2",
price: 200,
},
{
name: "item 3",
price: 300,
},
],
total: 600,
isWatermark: false,
};

var templateHtml = fs.readFileSync(
path.join(process.cwd(), "invoice.html"),
"utf8"
);
var template = handlebars.compile(templateHtml);
var finalHtml = encodeURIComponent(template(dataBinding));

var options = {
format: "A4",
headerTemplate: "<p></p>",
footerTemplate: "<p></p>",
displayHeaderFooter: false,
margin: {
top: "40px",
bottom: "100px",
},
printBackground: true,
path: "invoice.pdf",
};
module.exports.html_to_pdf = async ({ templateHtml, dataBinding, options }) => {
const template = handlebars.compile(templateHtml);
const finalHtml = encodeURIComponent(template(dataBinding));

const browser = await puppeteer.launch({
args: ["--no-sandbox"],
Expand All @@ -80,7 +44,62 @@ const handlebars = require("handlebars");
});
await page.pdf(options);
await browser.close();
})();
};
```

example.js

```js
const fs = require("fs");
const path = require("path");
const { html_to_pdf } = require(".");

try {
(async () => {
const dataBinding = {
items: [
{
name: "item 1",
price: 100,
},
{
name: "item 2",
price: 200,
},
{
name: "item 3",
price: 300,
},
],
total: 600,
isWatermark: true,
};

const templateHtml = fs.readFileSync(
path.join(process.cwd(), "invoice.html"),
"utf8"
);

const options = {
format: "A4",
headerTemplate: "<p></p>",
footerTemplate: "<p></p>",
displayHeaderFooter: false,
margin: {
top: "40px",
bottom: "100px",
},
printBackground: true,
path: "invoice.pdf",
};

await html_to_pdf({ templateHtml, dataBinding, options });

console.log("Done: invoice.pdf is created!");
})();
} catch (err) {
console.log("ERROR:", err);
}
```

## How to display paid stamp watermark on invoice?
Expand Down
50 changes: 50 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require("fs");
const path = require("path");
const { html_to_pdf } = require(".");

try {
(async () => {
const dataBinding = {
items: [
{
name: "item 1",
price: 100,
},
{
name: "item 2",
price: 200,
},
{
name: "item 3",
price: 300,
},
],
total: 600,
isWatermark: true,
};

const templateHtml = fs.readFileSync(
path.join(process.cwd(), "invoice.html"),
"utf8"
);

const options = {
format: "A4",
headerTemplate: "<p></p>",
footerTemplate: "<p></p>",
displayHeaderFooter: false,
margin: {
top: "40px",
bottom: "100px",
},
printBackground: true,
path: "invoice.pdf",
};

await html_to_pdf({ templateHtml, dataBinding, options });

console.log("Done: invoice.pdf is created!");
})();
} catch (err) {
console.log("ERROR:", err);
}
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const puppeteer = require("puppeteer");
const handlebars = require("handlebars");

module.exports.html_to_pdf = async ({ templateHtml, dataBinding, options }) => {
const template = handlebars.compile(templateHtml);
const finalHtml = encodeURIComponent(template(dataBinding));

const browser = await puppeteer.launch({
args: ["--no-sandbox"],
headless: true,
});
const page = await browser.newPage();
await page.goto(`data:text/html;charset=UTF-8,${finalHtml}`, {
waitUntil: "networkidle0",
});
await page.pdf(options);
await browser.close();
};
Binary file modified invoice.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"author": "chuongtrh",
"license": "MIT",
"dependencies": {
"handlebars": "^4.0.12",
"puppeteer": "^1.8.0"
"handlebars": "^4.7.7",
"puppeteer": "^14.2.0"
}
}
57 changes: 0 additions & 57 deletions pdf.js

This file was deleted.

0 comments on commit 2d90d32

Please sign in to comment.