diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..30bc162 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules \ No newline at end of file diff --git a/README.md b/README.md index e464a55..04caee1 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ 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 @@ -24,51 +24,15 @@ Generate a simple PDF invoice from HTML using [puppeteer](https://github.com/Goo 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: "
", - footerTemplate: "", - 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"], @@ -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: "", + footerTemplate: "", + 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? diff --git a/example.js b/example.js new file mode 100644 index 0000000..cdfc7b2 --- /dev/null +++ b/example.js @@ -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: "", + footerTemplate: "", + 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); +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..5ba78b1 --- /dev/null +++ b/index.js @@ -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(); +}; diff --git a/invoice.pdf b/invoice.pdf index 822a2c9..f1dc1b7 100644 Binary files a/invoice.pdf and b/invoice.pdf differ diff --git a/package.json b/package.json index afd6a8d..d86fe28 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/pdf.js b/pdf.js deleted file mode 100644 index b6f7b9b..0000000 --- a/pdf.js +++ /dev/null @@ -1,57 +0,0 @@ -const fs = require("fs"); -const path = require("path"); -const puppeteer = require('puppeteer'); -const handlebars = require("handlebars"); - -try { - (async () => { - var dataBinding = { - items: [{ - name: "item 1", - price: 100 - }, - { - name: "item 2", - price: 200 - }, - { - name: "item 3", - price: 300 - } - ], - total: 600, - isWatermark: true - } - - 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: "", - footerTemplate: "", - displayHeaderFooter: false, - margin: { - top: "40px", - bottom: "100px" - }, - printBackground: true, - path: 'invoice.pdf' - } - - 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(); - - console.log('Done: invoice.pdf is created!') - })(); -} catch (err) { - console.log('ERROR:', err); -}