Skip to content

Using with Playwright #223

Description

@SteveDunn

I've got a page, it contains a line chart. Everything is fine with the page when viewing it in my browser. But when I use Playwright to print the page, the chart never appears.

I guess it's down to the fact that the chart is drawn in a Canvas.

I observed that, when viewing normally, SetupCompletedCallback is called, but it is never called when the page is loaded by Playwright.

Here's my Playwright code:

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions { Headless = true });

var page = await browser.NewPageAsync();

string url = _getCurrentServerAddress.Get();

IResponse? response = await page.GotoAsync(
    $"{url}/PrintReport",
    new PageGotoOptions
    {
        WaitUntil = WaitUntilState.NetworkIdle
    });

await page.WaitForLoadStateAsync(LoadState.NetworkIdle);

// Generate the PDF
var margin = "16px";

string content = await page.ContentAsync();

byte[] output = await page.PdfAsync(new PagePdfOptions
{
    Format = "A4", // or "letter"
    Landscape = false,
    Path = "output.pdf",
    Margin = new Margin { Top = margin, Left = margin, Bottom = margin, Right = margin },
    PrintBackground = true,
    DisplayHeaderFooter = false
});

await page.CloseAsync();

Any ideas? I guess it's a combination of js and canvas, but I've exhausted what I've found online on how to fix it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions