How to add custom header / footer in printed document

Do not post support requests, bug reports or feature requests. Discuss CEF here. Non-CEF related discussion goes in General Discussion!

How to add custom header / footer in printed document

Postby guruc00l » Wed Feb 17, 2016 4:47 pm

Hi,

I would like to add custom header / footer in printed PDF document using CEF(branch-2454) engine. Latest release has CefPrintHandler class http://magpcss.org/ceforum/apidocs/projects/%28default%29/CefPrintHandler.html with supporting function GetPrintHeaderFooter which is not available in branch 2454 https://bitbucket.org/chromiumembedded/cef/src/2782fb8fc85f55b87ea3c8bedaccec243eba65ed/?at=2454. Is there a way to accomplish the same thing? Unfortunately I can't use latest release for my project.

Thanks,
guruc00l
Newbie
 
Posts: 5
Joined: Wed Feb 17, 2016 4:32 pm

Re: How to add custom header / footer in printed document

Postby magreenblatt » Wed Feb 17, 2016 6:08 pm

GetPrintHeaderFooter was only supported in CEF1. You can use CefBrowserHost::PrintToPDF which is supported by 2454 branch.
magreenblatt
Site Admin
 
Posts: 12406
Joined: Fri May 29, 2009 6:57 pm

Re: How to add custom header / footer in printed document

Postby guruc00l » Mon Feb 22, 2016 4:11 pm

magreenblatt wrote:GetPrintHeaderFooter was only supported in CEF1. You can use CefBrowserHost::PrintToPDF which is supported by 2454 branch.


Thanks magreenblatt! I checked PrintToPDF API and found that it only supports adding string via
Code: Select all
CefPdfPrintSettings -> header_footer_url;
Is it true that I can't add image etcetera for custom branding in header / footer section using CefPdfPrintSettings APIs.

Best,
guruc00l
Newbie
 
Posts: 5
Joined: Wed Feb 17, 2016 4:32 pm

Re: How to add custom header / footer in printed document

Postby magreenblatt » Mon Feb 22, 2016 4:19 pm

Correct, if you want fancier header/footers you'll need to do that in the web content (e.g. create a new HTML document with whatever you want to print).
magreenblatt
Site Admin
 
Posts: 12406
Joined: Fri May 29, 2009 6:57 pm

Re: How to add custom header / footer in printed document

Postby guruc00l » Mon Feb 22, 2016 5:11 pm

magreenblatt wrote:Correct, if you want fancier header/footers you'll need to do that in the web content (e.g. create a new HTML document with whatever you want to print).


Thanks! Is there a way to add / update source HTML with styles and HTML from within CEF? I attempted injecting HTML+CSS in source DOM by calling
ExecuteJavaScript(...)
under
OnLoadEnd(...) event handler
but it didn't work.

Best,
--
guruc00l
Newbie
 
Posts: 5
Joined: Wed Feb 17, 2016 4:32 pm

Re: How to add custom header / footer in printed document

Postby magreenblatt » Mon Feb 22, 2016 5:24 pm

It depends on the content. Using ExecuteJavaScript will work, but you need to have correct JavaScript. You can use the DevTools console to try JavaScript snippets and find what works.
magreenblatt
Site Admin
 
Posts: 12406
Joined: Fri May 29, 2009 6:57 pm

Re: How to add custom header / footer in printed document

Postby guruc00l » Wed Feb 24, 2016 2:07 pm

magreenblatt wrote:It depends on the content. Using ExecuteJavaScript will work, but you need to have correct JavaScript. You can use the DevTools console to try JavaScript snippets and find what works.

This is what I came up with (with no success modifying original HTML content though). Following JS creates a simple div, adds an img tag to it, updates dom with the new div. Finally JS is called OnLoadEnd event.

Code: Select all
void SimpleHandler::UpdateDOM(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame)
{
   std::string code = "var div = document.createElement('div');";
   code += "div.id = 'pageHeader';";
   code += "var img = document.createElement('img');";
   code += "img.src = 'some_local_path\\logo.jpg';";
   code += "img.width = 250;";
   code += "img.height = 250";
   code += "div.appendChild(img)";
   code += "document.body.appendChild(div);";
   frame->ExecuteJavaScript(code, frame->GetURL(), 0);
}

void SimpleHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser,
    CefRefPtr<CefFrame> frame,
    int httpStatusCode)
{
   if (!CefCurrentlyOn(TID_UI)) {
      // Execute on the UI thread.
      CefPostTask(TID_UI,
         base::Bind(&SimpleHandler::UpdateDOM, this, browser, frame));
      return;
   }
   setTimeOut(browser, frame, _pdffile, _instructions);   
}



Am I missing something obvious?

Thanks!
guruc00l
Newbie
 
Posts: 5
Joined: Wed Feb 17, 2016 4:32 pm

Re: How to add custom header / footer in printed document

Postby magreenblatt » Wed Feb 24, 2016 2:09 pm

Is your UpdateDOM method called? Did you try the JavaScript in DevTools?
magreenblatt
Site Admin
 
Posts: 12406
Joined: Fri May 29, 2009 6:57 pm


Return to CEF Discussion

Who is online

Users browsing this forum: No registered users and 14 guests