admin 管理员组

文章数量: 1086019

I have this method to generate a pdf using Puppeteer

public async Task<IActionResult> SavePDF(string content)
{
    try
    {
        var browser = await Puppeteer.LaunchAsync(new LaunchOptions
        {
            Headless = true,
        });

        var page = await browser.NewPageAsync();

        // Set a timeout (60 sec) while loading the content
        await page.SetContentAsync(content, new NavigationOptions { Timeout = 60000 });

        var fileName = $"C:/CustomerData/{Guid.NewGuid()}_{CompanyNumber}.pdf";
        await page.PdfAsync(fileName);

        // Dispose of the browser properly
        await browser.CloseAsync();

        return Json(fileName);
    }
    catch (Exception ex)
    {
        ASLog(ex);
        return Problem($"Error generating PDF: {ex.Message}");
    }
}

And I download the browser once in Program.cs like this

// Download Chromium once when the application starts
await new BrowserFetcher().DownloadAsync();

This code works on a test server and a development server but not in the live production server. In the production server the following error occurs

2025-03-27T00:44:03.4471493+00:00 80001bae-0000-f300-b63f-84710c7967bb [ERR] An unhandled exception has occurred while executing the request. (48a46595)
System.TimeoutException: Timeout of 180000 ms exceeded
   at PuppeteerSharp.Helpers.TaskHelper.WithTimeout[T](Task`1 task, TimeSpan timeout, Func`2 exceptionFactory) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Helpers/TaskHelper.cs:line 185
   at PuppeteerSharp.Cdp.CdpCDPSession.SendAsync(String method, Object args, Boolean waitForCallback, CommandOptions options) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Cdp/CdpCDPSession.cs:line 113
   at PuppeteerSharp.CDPSession.SendAsync[T](String method, Object args, CommandOptions options) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/CDPSession.cs:line 48
   at PuppeteerSharp.Cdp.CdpPage.PdfInternalAsync(String file, PdfOptions options) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Cdp/CdpPage.cs:line 864
   at PuppeteerSharp.Page.PdfStreamAsync(PdfOptions options) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Page.cs:line 389
   at ASWeb.Controllers.MainMenu.SalesInvoicing.SalesInvoicingController.SendEmailPDF(InvoicePDFViewModel model) in C:\Code\AS-Web\ASWeb.App\Controllers\MainMenu\SalesInvoicing\SalesInvoicingController.cs:line 2401
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at ASWeb.Middlewares.TrialVersionMiddleware.Invoke(HttpContext httpContext, ISharedService shared) in C:\Code\AS-Web\ASWeb.App\Middlewares\TrialVersionMiddleware.cs:line 39
   at ASWeb.Middlewares.XeroDisconnectedMiddleware.Invoke(HttpContext httpContext, ISettingsService settings) in C:\Code\AS-Web\ASWeb.App\Middlewares\XeroDisconnectedMiddleware.cs:line 22
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Program.<>c__DisplayClass0_0.<<<Main>$>b__9>d.MoveNext() in C:\Code\AS-Web\ASWeb.App\Program.cs:line 210
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddlewareImpl.<Invoke>g__Awaited|10_0(ExceptionHandlerMiddlewareImpl middleware, HttpContext context, Task task)

Any hint into why this issue might be happening is greatly appreciated. Thanks in advance.

本文标签: cIssue with Puppeteer on a production serverStack Overflow