Jump to content
Sign in to follow this  
Mauro Botta

Delphi 12.3 DataSnap Server - Best HTML TO PDF converter ?

Recommended Posts

Hi

I have a Delphi 12.3 DataSnap Server application (Win64),
i have a problem with  HTML text To PDF converter. 

I'm using this codice: work fine but have a problem , if the application is Showed in Windows, Don't work (  "canvas does not allow drawing" error  )

if  i minimize my App server then work fine.


Are there any TWebView2 example for make it ?

 

Mauro

 


My code:

TgtPDFEngine : Last Version

THtmlViewer: Last Version

 

begin
gtPDFEngine1 := TgtPDFEngine.Create(nil);
gtPDFEngine1.Name := 'gtPDFEngine_Temp_' + IntToStr(Random(100000));
gtPDFEngine1.FileExtension := 'pdf';
gtPDFEngine1.FileDescription := 'Adobe PDF Files';
gtPDFEngine1.Preferences.ShowSetupDialog := False;
gtPDFEngine1.Preferences.OpenAfterCreate := False;

gtHtmlViewInterface1 := TgtHtmlViewInterface.Create(nil);
gtHtmlViewInterface1.Name := 'gtHtmlViewInterface_Temp_' + IntToStr(Random(100000));
gtHtmlViewInterface1.Engine := gtPDFEngine1;
gtHtmlViewInterface1.DoBeginDoc := True;
gtHtmlViewInterface1.DoEndDoc := True;
gtHtmlViewInterface1.AutoSizePage := True;


Randomize();


HtmlViewer1 := THtmlViewer.Create(nil);
HTMLViewer1.Name := 'HTMLViewer_Temp_' + IntToStr(Random(100000));
HTMLViewer1.Visible := False;
HTMLViewer1.Parent := Application.MainForm;  
HTMLViewer1.LoadFromString(sHTML);

gtPDFEngine1.FileName := sPDF_File_Dest;


if slPDF_ExtraInfo <> nil then
   begin
   gtPDFEngine1.DocInfo.Keywords := slPDF_ExtraInfo.Text;
   end;


 

try
gtHtmlViewInterface1.RenderDocument(HTMLViewer1);   //<------  "canvas does not allow drawing"
finally
HtmlViewer1.Free;
gtHtmlViewInterface1.Free;
gtPDFEngine1.Free;
end;

end;

Share this post


Link to post

Hi,

I believe Gnostice uses a modified version of HTMLViewer and first you should check if you version is actually the one coming with the Gnostice installation.

If your HTML does not contain any JavaScript then I would suggest you consider https://delphihtmlcomponents.com

If you have JavaScript in your HTML then you might need to shell execute Chromium based browser as "headless" and relevant other parameters to generate a pdf for you. This is CPU intensive and might take 0.5-2.0 seconds per pdf file.

Share this post


Link to post

You can always use an external tool. I run a Docker instance and have a running container of html2pdf image, which has API endpoints for passing HTML in the body, along with useful query parameters for manipulation. The obvious benefit is that it uses puppeteer.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×