[英]How to create a pdf using html in nodejs
我正在嘗試將 html 轉換為 pdf,我想在其中傳遞一些動態值,然后我想在使用此代碼時生成一個 pdf,其中我創建了一個外部 html 文件,然后我就可以轉換 pdf這是代碼
發票.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h2>Approve Page ,</h2>
</body>
</html>
這是 pdf.js 文件
const fs = require("fs");
const path = require("path");
const utils = require("util");
const puppeteer = require("puppeteer");
const hb = require("handlebars");
const readFile = utils.promisify(fs.readFile);
async function getTemplateHtml() {
console.log("Loading template file in memory");
try {
const invoicePath = path.resolve("./invoice.html");
return await readFile(invoicePath, "utf8");
} catch (err) {
console.log(err);
}
}
async function generatePdf() {
let data = {};
getTemplateHtml()
.then(async (res) => {
console.log("Compiing the template with handlebars");
const template = hb.compile(res, { strict: true });
const result = template(data);
const html = result;
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(html);
await page.pdf({ path: "invoice.pdf", format: "A4" });
await browser.close();
console.log("PDF Generated");
return;
})
.catch((err) => {
console.error(err);
});
}
generatePdf();
但我想以這種方式實施
const fs = require("fs");
const path = require("path");
const utils = require("util");
const puppeteer = require("puppeteer");
const hb = require("handlebars");
const readFile = utils.promisify(fs.readFile);
const A = "invoice";
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h2>Approve Page ,${A}</h2>
</body>
</html>
`;
async function getTemplateHtml() {
console.log("Loading template file in memory");
try {
return await readFile(html, "text/html utf8");
} catch (err) {
console.log(err);
}
}
async function generatePdf() {
let data = {};
getTemplateHtml()
.then(async (res) => {
console.log("Compiing the template with handlebars");
const template = hb.compile(res, { strict: true });
const result = template(data);
const html = result;
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(html);
await page.pdf({ path: "invoice.pdf", format: "A4" });
await browser.close();
console.log("PDF Generated");
return;
})
.catch((err) => {
console.error(err);
});
}
generatePdf();
但收到此錯誤
消息:'您必須將字符串或 Handlebars AST 傳遞給 Handlebars.compile。 您傳遞了未定義的',名稱:'錯誤',
我能夠用很少的代碼解決這個問題,但想問一下是否可以將其轉換為 base64 而不是創建 pdf 文件。 這是解決方案
const fs = require("fs");
const path = require("path");
const utils = require("util");
const puppeteer = require("puppeteer");
const hb = require("handlebars");
const readFile = utils.promisify(fs.readFile);
(async () => {
const A = "invoice";
const htmlContent = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h2>Approve Page ,${A}</h2>
</body>
</html>
`;
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(htmlContent);
await page.pdf({ path: "html.pdf", format: "A4" });
await browser.close();
})();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.