puppeteer-sharp icon indicating copy to clipboard operation
puppeteer-sharp copied to clipboard

PDF: Page.PdfStreamAsync throws MessageException: Protocol error (IO.read): Read failed

Open axunonb opened this issue 3 years ago • 0 comments

Description

Complete minimal example reproducing the issue

var options = new PuppeteerSharp.LaunchOptions
{
    // --use-cmd-decoder=passthrough causes Chromium error
    Headless = true,
    Args = new[]
        {"--no-sandbox", "--disable-gpu", "--disable-extensions", "--use-cmd-decoder=validating"},
    ExecutablePath = pathToChromium, Timeout = 10000
};
using var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(options).ConfigureAwait(false);
using var page = await browser.NewPageAsync().ConfigureAwait(false);
var html = "<html><body><h1>Test HTML to PDF</h1></body></html>";
await page.SetContentAsync(html);

// Throws PuppeteerSharp.MessageException: Protocol error (IO.read): Read failed
var result = await page.PdfStreamAsync(new PuppeteerSharp.PdfOptions
        {Scale = 1.0M, Format = PaperFormat.A4}).ConfigureAwait(false));

Expected behavior:

PdfStreamAsync returns a stream with the converted PDF document

Actual behavior:

  • Local machine: as expected
  • External web server: PuppeteerSharp.MessageException: Protocol error (IO.read): Read failed

Versions

  • PuppeteerSharp 2.0.4
  • .NET Core 3.1
  • Chromium 83.0.4099.0 (pre-installed)

Additional Information

This works on the web server:

  1. Invoking Chromium without PuppeteerSharp but with PuppeteerSharp's arguments generates a PDF file as expected
var tempdir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempdir);
var tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

new System.Diagnostics.ProcessStartInfo(pathToChromium,
        $"--disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=TranslateUI --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --headless --hide-scrollbars --mute-audio --no-sandbox --disable-gpu --disable-extensions --use-cmd-decoder=validating --no-margins --user-data-dir={tempdir} --print-to-pdf={tempFile} https://google.com/")
  1. Screenshot works as expected
var result = await page.ScreenshotStreamAsync(new PuppeteerSharp.ScreenshotOptions
                            {FullPage = true, Quality = 100, Type = ScreenshotType.Jpeg}).ConfigureAwait(false);

Logged before calling PdfStreamAsync:

  • Chromium Process: physical memory = 37.773.312 bytes, exited = False, start arguments = --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=TranslateUI --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --headless --hide-scrollbars --mute-audio about:blank --no-sandbox --disable-gpu --disable-extensions --use-cmd-decoder=validating --remote-debugging-port=0 --user-data-dir="D:\Temp\2jw
  • Chromium CDP session: Closed = False, Reason = , SessionId = 6C80D4972FE859B5908E25010587C8DE

axunonb avatar Mar 22 '21 19:03 axunonb