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 in node.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
Add a ment  | 

2 Answers 2

Reset to default 9

Try 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