PrintToPdfAsync with custom header/footer

Having problems with building or using the CefSharp .NET binding? Ask your CEF-related questions here. Please ask general usage questions on StackOverflow.

Moderator: amaitland

PrintToPdfAsync with custom header/footer

Postby des » Tue Apr 27, 2021 7:25 am

Hi,
I'm trying to print a page to PDF but the custom header/footer data I use only get half printed.

This is the code

Code: Select all
PdfPrintSettings pdfSettings = new PdfPrintSettings
{
  BackgroundsEnabled = false,
  HeaderFooterEnabled = true,
  HeaderFooterUrl = "some url string",
  HeaderFooterTitle = "some title string "
};
cef.PrintToPdfAsync(fileName, pdfSettings).Wait();

The page is correclty printed, but the headers are the date on the left, and the original page title on the right; while the footer has HeaderFooterUrl on the left and the page counter on the right.

How can I take full control of these elements?
des
Newbie
 
Posts: 3
Joined: Tue Apr 27, 2021 7:15 am

Re: PrintToPdfAsync with custom header/footer

Postby amaitland » Tue Apr 27, 2021 2:56 pm

When you print to PDF in Chrome is there an option to add a custom header/footer? If yes can you see how that behaves?

CEF doesn't do much more than populate a settings dictionary that is passed to chromium.

https://github.com/chromiumembedded/cef ... ger.cc#L56

There aren't options for taking control of how the header/footer are positioned. You might have more luck leaving them off and post processing the PDF to add them yourself.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1290
Joined: Wed Jan 14, 2015 2:35 am

Re: PrintToPdfAsync with custom header/footer

Postby des » Wed Apr 28, 2021 6:48 am

I don't care that much of where they're positioned as much as being able to access them.

At the moment there seem to be only 2 fields: HeaderFooterUrl and HeaderFooterTitle, of which only the first is actually used.
On the printed page there are 4 fields though.

I'll follow the link you posted, but I was hoping there was some option I missed that would have taken care of the problem.
des
Newbie
 
Posts: 3
Joined: Tue Apr 27, 2021 7:15 am

Re: PrintToPdfAsync with custom header/footer

Postby amaitland » Wed Apr 28, 2021 2:19 pm

I suspect the problem lies in Chromium though you'll need to find a way to do some testing.

From a CefSharp point of view I believe CefPdfPrintSettings is populated correctly.

https://github.com/cefsharp/CefSharp/bl ... er.cpp#L83

From a CEF point of view the dictionary looks like the keys match what is expected in Chromium.

https://source.chromium.org/chromium/ch ... s=chromium

I'd suggest searching the chromium issue tracker to see if there is a bug already opened.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1290
Joined: Wed Jan 14, 2015 2:35 am

Re: PrintToPdfAsync with custom header/footer

Postby des » Thu Apr 29, 2021 8:25 am

I have not tried yet, but I think the only way to proceed is to change the title of the page with EvaluateScriptAsync()
des
Newbie
 
Posts: 3
Joined: Tue Apr 27, 2021 7:15 am


Return to CefSharp Forum

Who is online

Users browsing this forum: No registered users and 7 guests