6

There is code in Chromium that's used for generating a PDF version of the current page, before displaying it in Print Preview.

Is it possible for a Chrome extension to access this API, to get a PDF version of the current page?

Alf Eaton
  • 5,226
  • 4
  • 45
  • 50

3 Answers3

4

Chrome does not offer any such API for extensions.

If your aim is to generate PDF files using Chrome, you can do that by Print > Destination > Save as PDF.

4

You can use page Capture API which returns page data as M HTML blob and then convert MTHML blob to PDF.

References:

Let me know if you need more information.

Community
  • 1
  • 1
Sudarshan
  • 18,140
  • 7
  • 53
  • 61
  • The Capture API looks like the way forward, thanks - perhaps PDF will be added as an option there in the future. I also found [chrome.tabs.captureVisibleTab](http://developer.chrome.com/extensions/tabs.html#method-captureVisibleTab) which can capture as PNG or JPEG. – Alf Eaton Dec 23 '12 at 14:53
-1

2022 answer

Chrome/Edge/Other variations

Have all changed since 10 years ago, Extensions have come and gone, many links to past methods above no longer work and now Page Print to PDF is common via inpage JavaScript.

The current means to access Chrome API and thus print to PDF methods is via Puppeteer

Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.

For simple programable command line use Chrome based browsers have a --headless --print-to-PDF mode. It does have a few limitations, but is so fast and easy to run in one line that it is worth using as a first pass test of PDF output, before trying other techniques such as editing the remote html in local browser before print using more complex DevTools scripting.

No point in showing lots of examples here for everybody's different needs but here are links to some different recent related usage.

Output PDF from HTML form as seen in browser https://stackoverflow.com/a/69307631/10802527

How to convert SVG to PDF https://stackoverflow.com/a/70735224/10802527

How to PDF print just one <div>ision https://stackoverflow.com/a/72002108/10802527

enter image description here

How to pretty print XML to PDF https://stackoverflow.com/a/68839283/10802527

K J
  • 8,045
  • 3
  • 14
  • 36