我正在使用 puppeteer 库使用 nodejs 生成 PDF 文件。我能够在 Windows 上本地生成 PDF 文件。但是在 Amazon Linux 生产服务器上尝试时,由于缺少 chrome 浏览器(安装了 chromium-browser)而出现错误。
有没有办法在没有 chromium 浏览器的情况下使用 Puppeteer 库?有谁尽快给我一个解决方案。
try {
data = await Admissions.find({ student: id});
if (fs.existsSync("./public/documentformats/StudentApplicationForm.html")) {
let filename = "Student_Basic_Admission_Details_"+randomize('A0', 10)+"_"+moment(new Date()).format("YYYY-MM-DD")+".pdf";
let html = await fs.promises.readFile('./public/documentformats/StudentApplicationForm.html', 'utf8');
try{
const filledHtmlContent = html.replace(/\${(.*?)}/g, (_, match) => data[0][match.trim()] );
const browser = await puppeteer.launch({headless: "new"});
const page = await browser.newPage();
await page.setContent(filledHtmlContent);
let mydir = './public/exports/admission';
if (!fs.existsSync(mydir)) {
fs.mkdirSync(mydir);
}
await page.pdf({ path: `./public/exports/admission/${filename}`, format: 'A3'});
await browser.close();
return {file : filename};
} catch (error) {
console.error('Error creating pdf file: ', error);
throw error;
}
} else {
throw new Error('The client file format was not found.');
}
} catch (error) {
console.error("Error in generating admission details:", error);
throw error;
}
不计算文件开销,Chromium 安装最多可达 300 MB。
AWS 限制为 50 MB,因此浏览器总是可能多次超出该限制。
有一些替代方案旨在填补您的空白,目前维护的一个是 https://github.com/sparticuz/chromium