CEF3-2454 printToPDF function - how to use it

1,140 views
Skip to first unread message

rapra

unread,
Nov 29, 2015, 3:46:46 AM11/29/15
to delphichromiumembedded
Hello!

I need to load page with amount of graphics and then print it to PDF. Chrome crashes when trying to print large graphics so I want to do it by CEF3. There is a working example on cefbuilds.com which in top menu has great working print to PDF function - asking of path and name to save PDF an saving it fast and properly.
I want just to save test.pdf in program location but I cant apply a function in program - it needs to build a callback function with many options.
procedure PrintToPdf(const path: ustring; settings: PCefPdfPrintSettings; const callback: ICefPdfPrintCallback);
procedure PrintToPdfProc(const path: ustring; settings: PCefPdfPrintSettings; const callback: TOnPdfPrintFinishedProc);

For now I have procedure:
function printCallback() : ICefPdfPrintCallback;
begin
  showMessage
('done');
end;

procedure 
TForm1.btnPrintClick(Sender: TObject);
var
  pSettings 
: PCefPdfPrintSettings;
begin
  pSettings
.backgrounds_enabled := 1;
  crm
.Browser.getHost.PrintToPdf('test.pdf',pSettings, printCallback);
end;

It prints empty pdf with size of A4 horizontal. I have no idea how to repair that code

Could anyone look at ceflib code and give me an advice how to start? 

FigthingNinjaSucks

unread,
Dec 26, 2016, 10:44:55 PM12/26/16
to delphichromiumembedded
Just had to implement PrintToPDF support too and was also struggling with it. It's a year since your original post, but just in case if anybody else is running into the same problems i thought i write my findings here:


The following works for me (using CEF 2704):

procedure TFrameAnalyzer.OnPdfPrintFinished(const path: ustring; ok: Boolean);
begin
  showMessage('done printing!');
end;

procedure TFrameAnalyzer.acPrintExecute(Sender: TObject);
var
  pSettings : TCefPdfPrintSettings;

begin

  pSettings.header_footer_title := CefString('Hello!');
  pSettings.header_footer_url := CefString('www.test.com!');
  pSettings.page_width:=0;
  pSettings.page_height:=0;
  pSettings.margin_top:=0;
  pSettings.margin_right:=0;
  pSettings.margin_bottom:=0;
  pSettings.margin_left:=0;
  pSettings.margin_type:= PDF_PRINT_MARGIN_DEFAULT;
  pSettings.header_footer_enabled:= 1;
  pSettings.selection_only:= 0;
  pSettings.landscape:= 0;
  pSettings.backgrounds_enabled := 1;

  crm.browser.Host.PrintToPdfProc ( 'd:\test.pdf', @pSettings, OnPdfPrintFinished );

end;



There seems to be a bug that if you specify any page width/height then the PrintToPDF will just step over the printing without generating the output or calling the callback (This might be fixed in CEF 2840).  Also, if you don't initialize the print-settings record then you might run into all kind of other problems and crashes. 


Reply all
Reply to author
Forward
0 new messages