I'm trying to use Puppeteer to dinamically convert an HTML into pdf to later be able to send the file through email in my NextJS API. It works perfectly fine when I'm working on dev mode, thing is that whenever I try to deploy my app to vercel, and then call the API route to execute the call, I get the following error:
- info Loaded env from /var/task/.env
- error Error: Could not find Chrome (ver. 113.0.5672.63). This can occur if either
1. you did not perform an installation before running the script (e.g. `npm install`) or
2. your cache path is incorrectly configured (which is: /home/sbx_user1051/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
at ChromeLauncher.resolveExecutablePath (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:300:27)
at ChromeLauncher.executablePath (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:181:25)
at ChromeLauncher.computeLaunchArguments (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:97:37)
at async ChromeLauncher.launch (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:83:28)
at async POST (/var/task/.next/server/app/api/p2/route.js:201:21)
at async /var/task/.next/server/chunks/625.js:2905:37
I already tried with the config files suggested in the Puppeteer docs, but nothing has worked so far... This is the code I wrote if it helps:
import { NextResponse } from "next/server";
const puppeteer = require("puppeteer");
import mustache from "mustache";
const fs = require("fs");
const nodemailer = require("nodemailer");
export async function POST(request: Request) {
const data = {
nombre: "Vanessa",
precio: 100,
acciones: 12,
};
const user = await request.json();
const browser = await puppeteer.launch({
headless: "new",
args: ["--no-sandbox"],
product: "firefox",
});
const page = await browser.newPage();
const html = fs.readFileSync("src/app/api/p2/template.html", "utf8");
const filledHtml = mustache.render(html, data);
await page.setContent(filledHtml, { waitUntil: "domcontentloaded" });
await page.emulateMediaType("screen");
const pdf = await page.pdf({
format: "A4",
printBackground: true,
margin: {
top: "20px",
bottom: "40px",
left: "20px",
right: "20px",
},
path: "result.pdf",
});
await browser.close();
console.log("Se genero el pdf");
let transporter = nodemailer.createTransport({
host: "m40.siteground.biz",
port: 465,
secure: true, // upgrade later with STARTTLS
auth: {
user: someuser,
pass: somepass,
},
});
let mailOptions = {
from: someemail,
to: someotheremail,
subject: "Sending Email using Node.js",
html: "<h1>Welcome</h1><p>That was easy!</p>",
attachments: [
{
filename: "result.pdf",
path: "result.pdf",
contentType: "application/pdf",
},
],
};
transporter.sendMail(mailOptions, function (error: any, info: any) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
return NextResponse.json("Se genero el pdf");
}