Search code examples
c#google-chromechromium-embeddedcefsharp

CEF Sharp: printing resolution


I'm using CEFSharp (the C# wrapper for CEF) to print a web page to PDF, like this:

browser.PrintToPdfAsync(@"C:\out.pdf", new PdfPrintSettings
{
    BackgroundsEnabled = true,
    HeaderFooterEnabled = false,
    Landscape = false,

    MarginType = CefPdfPrintMarginType.Custom,
    MarginBottom = 0,
    MarginTop = 0,
    MarginLeft = 0,
    MarginRight = 0,

    PageWidth =  210000,
    PageHeight = 297000
});

However, the problem is that the resulting PDF is very blurry when compared to manually printing the same page in the "real" Chrome application.

I've made a comparison screenshot to show the difference:

enter image description here

(open it at full resolution to notice the difference)

Basically, as you can see, CEF seems to be compressing images and other non-vector graphics much more than the native Chrome printing function.

Ideally, I'd like to disable compression completely, or at least it bring it closer to native Chrome levels. Can it be done?

Also related: is there was a way to print at higher resolution? The PdfPrintSettings class only accepts width and height measurements in microns, but I don't see any way to set the rendering definition (DPIs)... is it possible?


Solution

  • I guess images are blurry because PDF gets printed as a preview: https://bitbucket.org/chromiumembedded/cef/src/6006f77bd9e030e9b456e180798c7c13d19cae08/libcef/browser/printing/print_view_manager.cc?at=master&fileviewer=file-view-default#print_view_manager.cc-110

    It was my pull-request which added PDF printing to CEF. Printing as a preview seemed good enough for me. It allowed to write less code and implement less components involved in PDF printing.

    It's also possible that some other settings make images blurry. For example: https://bitbucket.org/chromiumembedded/cef/annotate/6006f77bd9e030e9b456e180798c7c13d19cae08/libcef/browser/printing/print_view_manager.cc?at=master&fileviewer=file-view-default#print_view_manager.cc-50

    It needs some debugging. In order to do that you most likely would need to build CEF from source: https://bitbucket.org/chromiumembedded/cef/wiki/BranchesAndBuilding

    DPI can not be set because Chromium itself does not have such setting in its PDF print dialog. Although you can try to set different DPI options here: https://bitbucket.org/chromiumembedded/cef/src/6006f77bd9e030e9b456e180798c7c13d19cae08/libcef/browser/printing/print_view_manager.cc?at=master&fileviewer=file-view-default#print_view_manager.cc-53 But it's possible that those settings are just ignored by Chromium.