-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
133 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters