在Heroku上的Puppeteer pdf空白 [英] Puppeteer pdf blank on Heroku
问题描述
我正在我的Web应用程序中进行路由,将一些html转换为pdf.这在开发中效果很好,但在Heroku上却失败了.其他Puppeteer任务在heroku上也可以正常工作.我正在渲染的HTML很小.
const generatePDF = asyncMiddleware(async (req, res) => {
const browser = await puppeteer.launch({
dumpio: true,
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
page.on('error', err => {
console.error('err', err, err.stack)
browser.close();
});
await page.goto(`data:text/html,${req.body.html}`, {
waitUntil: 'networkidle2',
});
// stupid attempt at debugging, making sure page has time to render?
await page.waitFor(6000);
const pdf = await page.pdf();
res.set('content-type', 'application/pdf');
await browser.close();
res.send(pdf);
});
module.exports = require('express')
.Router()
.post('/', generatePDF)
当我在Heroku上打路线时,我得到一个空白的PDF.木偶产生以下标准输出
[0131/123556.374089:ERROR:gpu_process_transport_factory.cc(1043)] Lost UI shared context.
当我编写此代码时,我的版本为0.10.x,但我认为升级可能会有所帮助,因此我将其升级为1.0.0.在两个版本上问题仍然存在.节点版本为8.x.x
我认为这可能是服务器内存问题,因此我升级为2x dyno,但这无济于事.
我认为这可能与以下两个问题之一有关: - https://github.com/GoogleChrome/puppeteer/issues/1875 - https://github.com/GoogleChrome/puppeteer/issues/1925 >
但是,这些似乎与Heroku无关,也不能解释它在一个环境而不是另一个环境中的工作原理
更新:当我查看有效负载时,pdf的数据看起来像一个UUID!
我知道了!我服务器的客户端是axios.我需要告诉axios我正在提供二进制文件.
const { data: pdf } = await req({
method: 'post',
url: 'api/pdfs',
responseType: 'arraybuffer', // <-- this was missing
data: { html }
});
I'm making a route in my webapp that turns some html into a pdf. This works great in development, but fails on Heroku. Other Puppeteer tasks work fine on heroku. The HTML I'm rendering is quite small.
const generatePDF = asyncMiddleware(async (req, res) => {
const browser = await puppeteer.launch({
dumpio: true,
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
page.on('error', err => {
console.error('err', err, err.stack)
browser.close();
});
await page.goto(`data:text/html,${req.body.html}`, {
waitUntil: 'networkidle2',
});
// stupid attempt at debugging, making sure page has time to render?
await page.waitFor(6000);
const pdf = await page.pdf();
res.set('content-type', 'application/pdf');
await browser.close();
res.send(pdf);
});
module.exports = require('express')
.Router()
.post('/', generatePDF)
When I hit the route on Heroku, I get a blank PDF. Puppeteer produces the following stdout
[0131/123556.374089:ERROR:gpu_process_transport_factory.cc(1043)] Lost UI shared context.
When I wrote this code I was on version 0.10.x, but I thought upgrading might help, so I went up at 1.0.0. The problem persists on both versions. Node version is 8.x.x
I thought it might be a server memory issue, so I upgraded to a 2x dyno, but that didn't help.
I think it could be related to one of these two issues: - https://github.com/GoogleChrome/puppeteer/issues/1875 - https://github.com/GoogleChrome/puppeteer/issues/1925
But, these don't seem super related to Heroku, or explain how it works in one env and not another
Update: when I look at the payload, the data of the pdf looks like a UUID!
I figured it out! The client to my server was axios. I needed to tell axios that I was serving up binary.
const { data: pdf } = await req({
method: 'post',
url: 'api/pdfs',
responseType: 'arraybuffer', // <-- this was missing
data: { html }
});
这篇关于在Heroku上的Puppeteer pdf空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!