MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
455 stars 55 forks source link

[Feature]: Add generatePdfDocumentOutline to print options #4466

Open RickStrahl opened 8 months ago

RickStrahl commented 8 months ago

Describe the feature/enhancement you need

Chromium now has support for generating an optional document outline as part of the PDF generation process. This works in Edge and Chromium/Chrome via command line. It would be really nice if this was supported via the print options as an optional feature because adding a TOC after the fact is really resource intensive.

Here are docs of the Chromium API which includes that option (although marked as experimental) image

https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF

Also works from the command line:

 & "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"  
     --headless --generate-pdf-document-outline 
     --print-to-pdf="c:\temp\test.pdf" "D:\..\HtmlSampleFile-SelfContained.html"

The scenario/use case where you would use this feature

I've created a generic library that wraps the HtmlToPdf printing from the WebView that is headless and can run in non-UI environments. It works great and it would be nice if TOC generation could be integrated natively. As it is I provided that functionality externally but it requires several additional libraries (bloating distribution size) and easily doubles generation overhead as we basically have to intercept the HTML, parse out headers and painfully map them to PDF pages (which can be error prone).

https://github.com/RickStrahl/WestWind.WebView.HtmlToPdf

How important is this request to you?

Impactful. My app's user experience would be significantly compromised without it.

Suggested implementation

No response

What does your app do? Is there a pending deadline for this request?

I have several applications that render HTML and allow printing and output to PDF. Specifcally a markdown editor which allows PDF output from generated HTML text.

AB#50281146

RickStrahl commented 8 months ago

As a side note - it looks like the WebView also ignores the extra command line parameter (likely because the actual print operation doesn't occur in the original host process but in a DevTools instance?)

This has no effect:

protected async void InitializeAsync()
{
    // must create a data folder if running out of a secured folder that can't write like Program Files
    var environment = await CoreWebView2Environment.CreateAsync(
        userDataFolder: HtmlToPdfHost.WebViewEnvironmentPath,
        options: new CoreWebView2EnvironmentOptions {  
            AdditionalBrowserArguments = "--generate-pdf-document-outline"
        }
    );
victorhuangwq commented 8 months ago

If this is a feature in CDP, you should be able to follow this to access it? https://learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/chromium-devtools-protocol

RickStrahl commented 8 months ago

Perhaps - but that's a bit more complicated to do than having it work through the published API that you're providing. This feature is clearly available in Chromium (and through the command line in Edge) - why not expose that through your API as it's very useful and one of the big missing features IMHO.

RickStrahl commented 8 months ago

Just for kicks I gave the DevTools protocol extension a try and that unfortunately does not work at all - any call the PrintToPDFAsync() results in a null exception inside of the runtime. I call this from the same place that I call the WebView2.PrintToPdfAsycn() method which works so I know that the document is ready etc.

image

RickStrahl commented 8 months ago

I also noticed that that API's return value has funky types for things like the Stream being typed as string. I doubt that anybody has actually tested this API in any meaningful way.

Point remains - we really shouldn't need to go through this separate API/NuGet Package and its dependencies just to make one extra PDF print option that is available in the runtime work.

victorhuangwq commented 8 months ago

@monica-ch could you help see if this flag --generate-pdf-document-outline can be used through CDP?

In the meantime, I will point out that we do not typically release APIs for experimental browser features, as it could leave us maintaining a feature that the browser eventually deprecates.

RickStrahl commented 7 months ago

... and yet you support the command line for the feature for CLI startup. :smile: Unlikely that that feature will go away.

I'm not sure whether PrintToPdf is actually using the Developer Tools API or something else to print to PDF?

monica-ch commented 7 months ago

@RickStrahl In Chromium, the --generate-pdf-document-outline flag was introduced to enable printing a PDF in headless mode. The CDP PrintToPDF API exclusively in headless mode, so this behavior aligns with expectations.

Regarding WebView2, the PrintToPDFAsync API does not rely on the CDP DevTools API for PDF printing. However, I understand your request, and I’ll add it to our backlog as a feature request for WebView2.