nondanee / puppeteer-electron

Use Puppeteer's API with Electron
https://www.npmjs.com/package/puppeteer-electron
MIT License
23 stars 9 forks source link

Screenshoting option? #2

Open sxiii opened 4 years ago

sxiii commented 4 years ago

Is it possible to make screenshots with electron? Here's what I've tried (index.js):

const puppeteer = require('puppeteer-electron');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
  await page.pdf({path: 'hn.pdf', format: 'A4'});
  await browser.close();
})();

In the result; it runs but does nothing.

nondanee commented 4 years ago

browser.newPage() cause error

UnhandledPromiseRejectionWarning: Error: Protocol error (Target.createTarget): Not supported

Because electron do not support multi-tab view

page.pdf cause error

UnhandledPromiseRejectionWarning: Error: Protocol error (Page.printToPDF): PrintToPDF is not implemented

Because "Puppeteer Docs" say https://github.com/puppeteer/puppeteer/blob/v2.1.0/docs/api.md#pagepdfoptions

NOTE Generating a pdf is currently only supported in Chrome headless.

It is not possible for puppeteer-core but possible for electron https://github.com/electron/electron/blob/master/docs/api/web-contents.md#contentsprinttopdfoptions

I can make a proxy for page.pdf to call contents.printToPDF in the future

Now you can directly use electron-pdf (https://github.com/fraserxu/electron-pdf)

sxiii commented 4 years ago

Oh I just realized electron requires the "xvfb" package which does not exist (and probably won't) for my distro. So I probably will have to stick to chromium/chrome binaries for screenshots as they work in headless without the xvfb. But thanks for debug @nondanee