javascript - 如何在nodejs中使用html创建pdf
问题描述
我正在尝试将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。你通过了 undefined', name: 'Error',
解决方案
我能够用很少的代码解决这个问题,但想问是否可以将其转换为 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();
})();
推荐阅读
- java - Java 11 中的 OpenWebBeans CDI 失败
- java - 在插入数据库之前检查资源是否存在或等到 DAO 抛出异常?
- r - 处理缺失值的 R 程序(类似于 Python 中的应用函数)
- node.js - 无法在 Google Cloud 中加载 JS 和 CSS
- java - 为什么我的服务执行一次,然后即使在后台运行也无法运行?
- c++ - 向量排序算法,只对大于 0 的元素进行排序
- android - Android SQLite 在删除行后更新所有 id
- javascript - 如何在 Angular 中为自定义验证器添加自定义验证错误消息
- google-calendar-api - 启用“sendNotifications”时“quotaExceeded:超出日历使用限制”
- laravel - 当我从我在 Laravel 中构建的网站联系表单发送消息时,消息显示为我发送的