Closed Bug 1599994 Opened 5 years ago Closed 5 years ago

Implement Page.printToPDF

Categories

(Remote Protocol :: CDP, task, P1)

task

Tracking

(firefox73 fixed)

RESOLVED FIXED
Tracking Status
firefox73 --- fixed

People

(Reporter: whimboo, Assigned: whimboo)

References

(Blocks 1 open bug, )

Details

(Whiteboard: [puppeteer-beta-mvp])

Attachments

(1 file)

Implementation bug for the Page.printToPDF method. Taking the puppeteer-alpha whiteboard entry from the meta bug.

I will have a look if it's implementable. See also the comment which I made on the meta bug before:

(In reply to Henrik Skupin (:whimboo) [⌚️UTC+2] from bug 1549479 comment #1)

Note that there is a WebExtension API which allows to do that for add-ons:
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/saveAsPDF

It's implementation can be found here:
https://searchfox.org/mozilla-central/rev/11d9c7b7fa82fdfb8ac2a8f0864e9d8d5fe2b926/browser/components/extensions/parent/ext-tabs.js#1304

It uses the underlying PrintSettingsService to print the document to a PDF file. I assume that we could make use of it too.

Assignee: nobody → hskupin
Status: NEW → ASSIGNED
Priority: -- → P1
Depends on: 1600053
Depends on: 1600058

I got the implementation working, which basically prints the document to a PDF file. By default Puppeteer uses ReturnAsStream for the transferMode, which means we need IO.read and IO.close implemented too. Once those are done, we will have the ReturnAsBase64 for free.

One interesting problem I noticed when implementing the ReturnAsBase64 transfer mode is that even when printing is done, the chosen PDF file hasn't been finished writing yet. To prevent race conditions we will have to wait until all the data has been written. I will use a timer for that and check with an interval of 100ms if that is the case.

Remaining work to do on this bug is to write some tests.

Blocks: 1601570
Blocks: 1601571

https://github.com/gramener/gramex/tree/dev/gramex/apps/capture is a real world use of print to PDF that we should test and see how we compare.

(In reply to David Burns :automatedtester from comment #5)

https://github.com/gramener/gramex/tree/dev/gramex/apps/capture is a real world use of print to PDF that we should test and see how we compare.

PDF generation works fine with this tool once Network.getCookies, Network.deleteCookie, Network.setExtraHTTPHeaders and Emulation.setEmulatedMedia have been implemented. Do we want to have meta bugs for different tools, so we can easily mark dependencies?

Flags: needinfo?(dburns)

I will file the bugs for the capture.js tool and mark its dependencies.

Flags: needinfo?(dburns)
Blocks: 1602829

Here another try which includes the addition two awaits that I was missing before, and which probably caused the IO tests on Windows to fail:
https://treeherder.mozilla.org/#/jobs?repo=try&revision=22b90f00c1ee24e0297637a8548bacffd5b8c376

The clean-up function fails on Windows to remove the temporary files if those are still open. As such we should attempt to close them first:
https://treeherder.mozilla.org/#/jobs?repo=try&revision=f4482289b579d8a01b32be876e28e3256bcc7795

Whiteboard: [puppeteer-alpha] → [puppeteer-alpha-reserve]
Depends on: 1603739
Whiteboard: [puppeteer-alpha-reserve] → [puppeteer-beta-mvp]
Attachment #9113766 - Attachment description: Bug 1599994 - [remote] Implement Page.printToPDF. → Bug 1599994 - [remote] Implement Page.printToPDF. r=#remote!
Pushed by hskupin@mozilla.com:
https://hg.mozilla.org/integration/autoland/rev/44c5a2312fd6
[remote] Implement Page.printToPDF. r=remote-protocol-reviewers,ato,maja_zf
Status: ASSIGNED → RESOLVED
Closed: 5 years ago
Resolution: --- → FIXED
Component: CDP: Page → CDP
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: