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

Problem with page.Request event #2781

Open
albertobelardi opened this issue Sep 13, 2024 · 5 comments
Open

Problem with page.Request event #2781

albertobelardi opened this issue Sep 13, 2024 · 5 comments

Comments

@albertobelardi
Copy link

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();
@kblok
Copy link
Member

kblok commented Sep 13, 2024

Where is that image.jpg located? because you are injecting the HTML.

@albertobelardi
Copy link
Author

Right, the image won't load even if I put a full path like:
<img src='file:///C:/folder_on_disk/chart.jpeg' alt='Local Image'>
The event request raise only use this format in HTML:
<img src='data:image/jpeg;base64,{img}' alt='Local Image'>
But then I couldn't inject the base64 string into the HTML

@kblok
Copy link
Member

kblok commented Sep 13, 2024

And why do you need that event?

@albertobelardi
Copy link
Author

I don't need of the event but the image visibile in the PDF file. What's wrong with the code snippet I posted? Why doesn't work?

@albertobelardi
Copy link
Author

I would simply to say that in this context in the PDF you cannot see the image. The path is correct. This code does't work but for documentation in right. Can you help me?

var htmlContent = @"
<html>
    <head><title>Test</title></head>
    <body>
        <img src='file:///folder/to/the/images/chart.jpeg' alt='Local image'>
    </body>
</html>";

var browserFetcher = new BrowserFetcher()
{
    BaseUrl = BaseDirectory,
    Browser = SupportedBrowser.Chrome,
};

var installedBrowser = await browserFetcher.DownloadAsync(BrowserTag.Stable);

var options = new LaunchOptions
{
    ExecutablePath = installedBrowser.GetExecutablePath(),
    Headless = true,
    Args = new[]
    {
        "--no-sandbox", 
        "--disable-setuid-sandbox",
        "--disable-gpu",
        "--disable-web-security"
    }
};

var browser = await Puppeteer.LaunchAsync(options);

var page = await browser.NewPageAsync();

await page.SetContentAsync(htmlContent);

await page.WaitForSelectorAsync("img");

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

await browser.CloseAsync();

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

No branches or pull requests

2 participants