如何将pdf文件转换成base64
How to convert a pdf file into base64
我正在尝试将 pdf 转换为 base64 并作为电子邮件的附件发送,但我无法转换为 base64 而不是创建一个文件我想将其转换为 base64 以便我可以作为 attachment.here是代码
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();
})();
我不想在此处创建 html.pdf,而是想将其转换为 base64,以便发送电子邮件。
试试这个:
const puppeteer = require("puppeteer");
(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);
const buffer = await page.pdf({ format: "A4" });
const base64 = buffer.toString('base64');
console.log(`data:application/pdf;base64,${base64}`); // Test it in a browser.
await browser.close();
})();
我正在尝试将 pdf 转换为 base64 并作为电子邮件的附件发送,但我无法转换为 base64 而不是创建一个文件我想将其转换为 base64 以便我可以作为 attachment.here是代码
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();
})();
我不想在此处创建 html.pdf,而是想将其转换为 base64,以便发送电子邮件。
试试这个:
const puppeteer = require("puppeteer");
(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);
const buffer = await page.pdf({ format: "A4" });
const base64 = buffer.toString('base64');
console.log(`data:application/pdf;base64,${base64}`); // Test it in a browser.
await browser.close();
})();