hardkoded / puppeteer-sharp

Headless Chrome .NET API
https://www.puppeteersharp.com
MIT License
3.41k stars 442 forks source link

Not all links produce an export #100

Closed Speta643 closed 6 years ago

Speta643 commented 6 years ago

As stated yesterday not all links produce an export when this happens i get put into break mode and gives me an error that states the following:

foreach (var list_item in sql_list)
{
    string id = list_item.Item2.ToString();
    string extension = ".pdf";
    string exportname = id + extension;
    var chromiumRevision = 526987;

    Console.WriteLine("Downloading chromium");
    await Downloader.CreateDefault().DownloadRevisionAsync(chromiumRevision);

    Console.WriteLine("Navigating " + list_item.Item1);
    var browser = await Puppeteer.LaunchAsync(new LaunchOptions
    {
        Headless = true
    }, chromiumRevision);

    var page = await browser.NewPageAsync();
    await page.GoToAsync(list_item.Item1);

    Console.WriteLine("Generating PNG");
    await page.PdfAsync(Path.Combine(directory,exportname));

    Console.WriteLine("Export " + exportname + " completed");
    Console.WriteLine();

}
Meir017 commented 6 years ago

You should move the creation of the Browser into a using statement

kblok commented 6 years ago

Your code should look something like this:

Console.WriteLine("Downloading chromium"); await Downloader.CreateDefault().DownloadRevisionAsync(Downloader.DefaultRevision);

var sql_list = new Tuple<string,string>[]
{
    new Tuple<string, string>("site2", "https://www.wunderground.com/weather/be/antwerp"),
    new Tuple<string, string>("site1", "https://darksky.net/forecast/51.2211,4.3997/si12/en")

};

var options = new LaunchOptions
{
    Headless = true
};

using (var browser = await Puppeteer.LaunchAsync(options, Downloader.DefaultRevision))
{
    foreach (var list_item in sql_list)
    {
        string id = list_item.Item1;
        string extension = ".pdf";
        string exportname = id + extension;

        Console.WriteLine("Navigating " + list_item.Item2);

        using (var page = await browser.NewPageAsync())
        {
            await page.GoToAsync(list_item.Item2);

            Console.WriteLine("Generating PNG");
            await page.PdfAsync(Path.Combine(Directory.GetCurrentDirectory(), exportname));

            Console.WriteLine("Export " + exportname + " completed");
            Console.WriteLine();
        }
    }
}

I took the URLs from the comment you made in one comment. We're failing at creating PDFs for both. So it's something we need to take a look at.

Speta643 commented 6 years ago

Hey again, do you have an estimation of when this bug will be resolved? also it works with NodeJS and the cli

kblok commented 6 years ago

@Speta643 I'll make a release this week with this fix.

kblok commented 6 years ago

@Speta643 I've just published v0.3.2, Could you take a look?