Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Evaluate hangs inside of embedded PDF frames #10696

Closed
2 tasks
liamdalg opened this issue Aug 4, 2023 · 2 comments
Closed
2 tasks

[Bug]: Evaluate hangs inside of embedded PDF frames #10696

liamdalg opened this issue Aug 4, 2023 · 2 comments

Comments

@liamdalg
Copy link

liamdalg commented Aug 4, 2023

Minimal, reproducible example

import puppeteer from 'puppeteer';

const sleep = t => new Promise(r => setTimeout(r, t));
const url = `file://${process.cwd()}/example.pdf`;

const browser = await puppeteer.launch({
    headless: 'new',
});

const page = await browser.newPage();
await page.goto(url);
await sleep(5000);

const frames = page.frames();
for (const f of frames) {
    console.log(`Evaluating in ${f._id}, ${f.url()}, ${f.isOOPFrame()}`);
    await f.evaluate(() => 1 + 1);
}

console.log('Done');
await browser.close();

Error string

no error

Bug behavior

  • Flaky
  • PDF

Background

In new headless or headful mode (i.e., when PDFs are rendered in the browser), attempting to evaluate inside of the PDF frame hangs indefinitely. This is true for any methods which attempt to use the frame's execution context. From some limited debugging, it seems that puppeteer is waiting indefinitely for an execution context that doesn't exist.

Possibly related (?): the CDP event "Page.frameDetached","params":{"frameId":"31218988D67AB9B8AC1BD6140FCC9433","reason":"swap"} is fired which, according to the FrameManager code, means that the frame should now be an OOPIF frame, but frame.isOOPFrame() returns false.

Debug: hang.log
PDF used (any will work): example.pdf

Expectation

Either:

  • Evaluation to succeed inside of the PDF context, or
  • Puppeteer to throw an error stating that the execution context was destroyed.

Reality

Program hangs on frame.evaluate. More specifically, it hangs when waiting for the deferred execution context (https://github.com/puppeteer/puppeteer/blob/main/packages/puppeteer-core/src/util/Deferred.ts#L74).

Puppeteer configuration file (if used)

No response

Puppeteer version

21.0.1

Node version

18.16.1

Package manager

npm

Package manager version

9.5.1

Operating system

Linux

@github-actions
Copy link

github-actions bot commented Aug 4, 2023

The issue has been labeled as confirmed by the automatic analyser.
Someone from the Puppeteer team will take a look soon!


Analyzer run

@o0101
Copy link

o0101 commented Nov 15, 2023

The new headless mode is still in beta and there may be stability and compatibility issues.

You probably already know this, but just in case, if you're looking for ways to render PDFs while using a headless browser, a reliable solution is:

  1. using the old headless mode
  2. capturing the download, and
  3. converting the downloaded PDF to PNG images using ImageMagick.

It's a few steps, but it reliably renders PDFs.

However if you're looking specifically to evaluate JS code in the context of PDFs for some "Chrome PDF viewer" plugin enhancement, this method will not help you. That being said, using the convert method described here will enable you to build your own "PDF viewer" fairly simply.

For some reference code that works in production (albeit using the underlying DevTools protocol rather than a Puppeteer API, to which you'd need to convert if you wish to use pptr), take a look here for the convert to PDF code; and here, here and here for the capture download and spawn convert process code.

All the best @liamdalg !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants