Home

WebView2 bug with pdfs

edited June 9

Hello, I have QuestPdf code that generates a PDF file and displays it using WebView2, every ~second run it breaks and displays a see through output where I can see my desktop, is there any solution to this bug?

async Task Main()
{
    QuestPDF.Settings.License = LicenseType.Community;

    var s = new MemoryStream();
    Document.Create(container =>
{
    container.Page(page =>
    {


        page.Size(PageSizes.A4);
        page.Margin(0, Unit.Centimetre);
        page.PageColor(Colors.White);

        page.Content().AlignCenter().Image(File.ReadAllBytes(@"...\test.png"));
    });


    container.Page(page =>
    {


        page.Size(PageSizes.A4);
        page.Margin(0, Unit.Centimetre);
        page.PageColor(Colors.White);

        page.Content().AlignCenter().Image(File.ReadAllBytes(@"...\test.png"));
    });



})
//.GeneratePdf(s);
.GeneratePdf("foo.pdf");

    var webView = new WebView2();

    await webView.EnsureCoreWebView2Async();

    webView.Dump();
    webView.Source = new Uri($"file://{Path.GetFullPath("foo.pdf").DumpTell()}");
}


public class InvoiceDocument : IDocument
{
    /* code omitted */

    void ComposeContent(IContainer container)
    {
        container.PaddingVertical(40).Column(column =>
        {
            column.Spacing(5);

            column.Item().Element(ComposeTable);
        });
    }

    void ComposeTable(IContainer container)
    {
        container
            .Height(250)
            .Background(Colors.Grey.Lighten3)
            .AlignCenter()
            .AlignMiddle()
            .Text("Table").FontSize(16);
    }

    void ComposeComments(IContainer container)
    {
        container.Background(Colors.Grey.Lighten3).Padding(10).Column(column =>
        {
            column.Spacing(5);
            column.Item().Text("Comments").FontSize(14);
        });
    }

    public DocumentMetadata GetMetadata()
    {
        throw new NotImplementedException();
    }

    public void Compose(IDocumentContainer container)
    {
        throw new NotImplementedException();
    }
}
// <NuGetReference Version="1.0.2535.41">Microsoft.Web.WebView2</NuGetReference>
//  <NuGetReference Version="2024.3.10">QuestPDF</NuGetReference>

Comments

  • I managed to reproduce this once, but not again, so it's really hard to diagnose. You can work around it by adding Util.NewProcess = true; to your query - this ensures a clean environment for the next run.

  • Also, a word of warning: this will crash on the non-xcopy version of LINQPad because WebView2 will try to write temporary files to the folder where LINQPad is installed in %programfiles%. To work around this, you must specify a writable data folder for WebView2. You can pick a random folder, or use LINQPad's helper methods:

    var env = await CoreWebView2Environment.CreateAsync (
        Util.BrowserEngine.GetWebView2ExecutableFolder(),
        Util.BrowserEngine.GetWebView2DataFolder());
    
    await webView.EnsureCoreWebView2Async (env);
    
Sign In or Register to comment.