Page 1 of 1

PrintToPdfAsync with custom header/footer

PostPosted: Tue Apr 27, 2021 7:25 am
by des
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?

Re: PrintToPdfAsync with custom header/footer

PostPosted: Tue Apr 27, 2021 2:56 pm
by amaitland
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.

Re: PrintToPdfAsync with custom header/footer

PostPosted: Wed Apr 28, 2021 6:48 am
by des
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.

Re: PrintToPdfAsync with custom header/footer

PostPosted: Wed Apr 28, 2021 2:19 pm
by amaitland
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.

Re: PrintToPdfAsync with custom header/footer

PostPosted: Thu Apr 29, 2021 8:25 am
by des
I have not tried yet, but I think the only way to proceed is to change the title of the page with EvaluateScriptAsync()