admin管理员组文章数量:1287554
I am trying to convert the pdf into base64 and send as an attachment to the email but i am unable to convert into the base64 instead of creating a file i want to convert it into base64 so i can send as an attachment.here is the code
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();
})();
here instead of creating a html.pdf i wanted to convert it into a base64 so i can send the email.
I am trying to convert the pdf into base64 and send as an attachment to the email but i am unable to convert into the base64 instead of creating a file i want to convert it into base64 so i can send as an attachment.here is the code
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();
})();
here instead of creating a html.pdf i wanted to convert it into a base64 so i can send the email.
Share Improve this question edited Sep 28, 2020 at 13:54 vsemozhebuty 13.8k1 gold badge28 silver badges28 bronze badges asked Sep 28, 2020 at 4:28 ramb tumberramb tumber 1591 gold badge2 silver badges10 bronze badges 3- @kelvin no here i am creating a file i don't want to create a file i want to create a base64 only. – ramb tumber Commented Sep 28, 2020 at 4:34
-
@kelvin your link is in
java
, the question is asking innode.js
. – namgold Commented Sep 28, 2020 at 4:46 -
Great question. I'm looking for a similar solution. I was thinking of using PDFkit instead of
puppeteer
; still deciding though. – Caio Mar Commented Dec 15, 2021 at 18:33
2 Answers
Reset to default 9Try this:
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();
})();
Just a small edit from the above answer from vsemozhebuty
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(htmlContent);
const buffer = await page.pdf({ format: "A4" });
const base64 = Buffer.from(buffer).toString("base64"); // THE EDIT IS HERE
console.log(`data:application/pdf;base64,${base64}`);
await browser.close();
本文标签: javascriptHow to convert a pdf file into base64Stack Overflow
版权声明:本文标题:javascript - How to convert a pdf file into base64 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741295058a2370769.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论