admin管理员组文章数量:1333442
Background
I am working in a node.js express application where we have a need to generate PDFs. Currently we are using Puppeteer from Google which makes this simple. In the documentation the way it shows to do this is by passing a path to the object which tells Puppeteer where to write the PDF.
Problem
I would prefer to not write this PDF file to disk. The goal here is to have a client hit an end point where a PDF will be generated and returned to the client. Creating a file for 2 seconds adds a tiny bit of state which causes me to have to deal with a lot more headaches in order to deploy to production.
Example
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(
'/',
);
await page.pdf({
path: filePath,
format: 'A4',
margin: {
top: '20px',
left: '20px',
right: '20px',
bottom: '20px',
},
});
await browser.close();
Question
In this example of code I am creating a PDF and storing it to disk. How can I create this PDF but instead of writing it to disk, return it to the client in the response immediately?
Background
I am working in a node.js express application where we have a need to generate PDFs. Currently we are using Puppeteer from Google which makes this simple. In the documentation the way it shows to do this is by passing a path to the object which tells Puppeteer where to write the PDF.
Problem
I would prefer to not write this PDF file to disk. The goal here is to have a client hit an end point where a PDF will be generated and returned to the client. Creating a file for 2 seconds adds a tiny bit of state which causes me to have to deal with a lot more headaches in order to deploy to production.
Example
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(
'https://example./',
);
await page.pdf({
path: filePath,
format: 'A4',
margin: {
top: '20px',
left: '20px',
right: '20px',
bottom: '20px',
},
});
await browser.close();
Question
In this example of code I am creating a PDF and storing it to disk. How can I create this PDF but instead of writing it to disk, return it to the client in the response immediately?
Share Improve this question asked Jun 1, 2018 at 22:15 wunowuno 9,88521 gold badges105 silver badges182 bronze badges1 Answer
Reset to default 11Strange I am also having the same requirement.
The problem by itself is relatively simple. All you need to do is remove the path attribute and wait for the page.pdf to plete its task, it will return a bytearray just send that as a response.
You can find the documentation for page.pdf(options)
:
path
<string> The file path to save the PDF to. If path is a relative path, then it is resolved relative to current working directory. If no path is provided, the PDF won't be saved to the disk.- returns: <Promise<Buffer>> Promise which resolves with PDF buffer.
async generatePdf(url) {
await page.goto(url, {waitUntil: 10000});
const buffer = await page.pdf({
format: 'A4'
});
return buffer;
}
本文标签:
版权声明:本文标题:javascript - How to create a PDF with Puppeteer in a node environment without writing it to disk - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289664a2447561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论