Corba
Corba

Reputation: 309

PuppeteerSharp Number of Chromium Instances

We use PuppeteerSharp to add the ability for users to download a PDF of our application. Unfortunately sometimes a huge number of Chromium processes are started and don't stop until a server restart.

Normally when a user downloads a pdf, 5 chromium processes start and those disappear when the download is finished.

This is our code:

private static LaunchOptions launchOptions = new LaunchOptions
{
    ExecutablePath = chromepath,
    Headless = true,
    DefaultViewport = ViewPortOptions
};

public static void ExportPdf(string url, string location)
{
    try
    {
        Task<string> task = Task.Run<string>(async () => await ExportPdfASync(url, location)));
        task.Wait();
    }
    catch (Exception)
    {
        // Exception handling
    }
}

public static async Task<string> ExportPdfASync(string url, string location)
{
    using (var browser = await Puppeteer.LaunchAsync(LaunchOptions))
    using (var page = await browser.NewPageAsync())
    {
        await page.SetViewportAsync(new ViewPortOptions() { Width = 1440, Height = 990, IsMobile = false, DeviceScaleFactor = 1.0 });

        await page.SetJavaScriptEnabledAsync(true);
        await page.GoToAsync(url);
        await page.WaitForTimeoutAsync(1500);

        var marginOptions = new MarginOptions()
        {
            Top = "10mm",
            Left = "10mm",
            Right = "10mm",
            Bottom = "10mm"
        };

        var pdfOptions = new PdfOptions()
        {
            PrintBackground = true,
            Format = PaperFormat.A4,
            MarginOptions = marginOptions,
            Landscape = landscape
        };
        await page.PdfAsync(location, pdfOptions);
    }

    return "";
}

The browser and page are in the using blocks so, even with an error those should be disposed.

Does anyone have a solution for this problem? We, and our servers would be very happy;)

enter image description here

Upvotes: 1

Views: 2206

Answers (1)

polkus
polkus

Reputation: 21

Try this:

public static async Task<string> ExportPdfASync(string url, string location)
{
 try
 {
   using (var browser = await Puppeteer.LaunchAsync(LaunchOptions))
   using (var page = await browser.NewPageAsync())
   {
    await page.SetViewportAsync(new ViewPortOptions() { Width = 1440, Height = 990, IsMobile = false, DeviceScaleFactor = 1.0 });

    await page.SetJavaScriptEnabledAsync(true);
    await page.GoToAsync(url);
    await page.WaitForTimeoutAsync(1500);

    var marginOptions = new MarginOptions()
    {
        Top = "10mm",
        Left = "10mm",
        Right = "10mm",
        Bottom = "10mm"
    };

    var pdfOptions = new PdfOptions()
    {
        PrintBackground = true,
        Format = PaperFormat.A4,
        MarginOptions = marginOptions,
        Landscape = landscape
    };
    await page.PdfAsync(location, pdfOptions);
  }
  }
  catch (Exception ex)
  {
  }
  finally {
    browser.CloseAsync();
    page.CloseAsync();
  }
  return "";
}

Upvotes: 1

Related Questions