Skip to content

Problem with page.Request event #2781

Open
@albertobelardi

Description

@albertobelardi

Hi, I use the library PuppeteerSharp, I write a lot of code to figure out why I couldn't find a solution. The problem is that the event page.Request never raised. I use the latest version: 20.0.0. Below report a snippet of code that I use to complet the work.
The source of the problem is that the image does not show up in the PDF.

Thanks

var htmlContent = @"
<html>
    <head><title>Test</title></head>
    <body>
        <img src='image.jpg' alt='My image to be convert in base64'>
    </body>
</html>";


var installedBrowser = await new BrowserFetcher().DownloadAsync(BrowserTag.Stable);
var options = new LaunchOptions
{
    ExecutablePath = installedBrowser.GetExecutablePath(),
    Headless = true,
    Args = new[] { "--disable-web-security" }
};
var browser = await Puppeteer.LaunchAsync(options);
var page = await browser.NewPageAsync();

await page.SetRequestInterceptionAsync(true);

page.Request += async (sender, e) =>
{
    if (e.Request.ResourceType == ResourceType.Image)
    {
        await Task.Run(() => Console.WriteLine($"Request image url: {e.Request.Url}"));
    }
};

await page.SetContentAsync(htmlContent);

await page.WaitForSelectorAsync("img");

await page.PdfAsync("output.pdf", new PdfOptions
{
    Format = PaperFormat.A4,
    PrintBackground = true
});

await browser.CloseAsync();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions