question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

printPDF to return a stream

See original GitHub issue

Currently printToPDF returns a base64 encoded string. It works great in general, but we are generating a bit huge PDFs - min 40MBs, they include lot of pages with high resolution images. Keeping such big data in memory causes performance and memories issues with node. It would be nice to take PDF content as a stream, or as a file.

Currently we are thinking about workaround with page per page printing. But it dramatically increases complexity of the service. So my question is: are there any plans to support streams for printToPDF?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cyrus-andcommented, Feb 19, 2022

@nyroDev something along the lines of:

const CDP = require('chrome-remote-interface');
const fs = require('fs');

async function streamPdfToFile(url, file, chunkSize = undefined /* means auto */) {
    const client = await CDP();

    try {
        const {IO, Page} = client;

        console.log(`Navigating to ${url}`);

        await Page.enable();
        await Page.navigate({url});
        await Page.loadEventFired();

        console.log(`Page loaded, requesting the PDF stream`);

        const {stream: handle} = await Page.printToPDF({
            transferMode: 'ReturnAsStream'
        });

        let fileStream;
        while (true) {
            const {base64Encoded, data, eof} = await IO.read({
                handle,
                size: chunkSize
            });

            if (!fileStream) {
                fileStream = fs.createWriteStream(file, {
                    encoding: base64Encoded ? 'base64' : 'binary'
                });
            }

            if (eof) {
                console.log(`PDF stream finished, saved to ${file}`);

                await IO.close({handle});
                fileStream.close();
                break;
            }

            console.log(`Processing the next chunk of ${data.length} (decoded) bytes`);

            fileStream.write(data);
        }
    } finally {
        client.close();
    }
}

streamPdfToFile('https://nodejs.org/api/fs.html', '/tmp/out.pdf', 1 << 20);
0reactions
nyroDevcommented, Feb 19, 2022

@cyrus-and thank you so much for your quick and detailed response, it works perfectly!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I display the print option for a pdf stream returned by ...
My wcf service create a pdf stream and return it, through the ajax call I'm properly handling the display in a new window...
Read more >
Options for Printing PDF Stream Files which are Stored ... - IBM
The simplest way to print PDF files which are stored in the Integrated File System (IFS) is open the PDF in a PDF...
Read more >
Printing a PDF from a web page - Dev Notes
I used an ASPX page which manipulates and streams the PDF file into a zero-height, zero-width IFrame to keep the user from manipulating...
Read more >
Save PDF file to Stream and Load PDF file from Stream in C#
Save PDF file to Stream and Load PDF file from Stream in C# ; Step 1: New a PDF instance. ; Step 2:...
Read more >
How to return a pdf file in response ? · Issue #1090 · nestjs/nest
we suppose that I have a Buffer object , How to return it in a response object? ... printPdf(body); const stream = this.printService....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found