Recent

Author Topic: How can I programmatically and silently convert an html into pdf?  (Read 1827 times)

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Platform:  Lazarus 1.8.4   FPC 3.0.4   OS:  Windows Server 2016 (All updates)

Goal:
I want my users to be able to click a button and in the background an existing .html template file is silently converted to a .pdf file.  I will do StringReplace() on the html file just before the conversion to .pdf.  I can assign the name of the .pdf programmatically, I don't want to involve the user during the conversion.  I also wd like to use the built-in .Pdf printer in the OS.  Sometimes it's called "Microsoft Print to PDF" and other times it's named "Print to PDF"

Questions:
Is there a way to do this using only Lazarus and the OS?
Is there a way to do this using a browser, like IE or Brave or Chrome?
(I don't want to use the CEF4 libraries.  They're huge and using them is cumbersome.

Please include code snippets to help me out.

Remember that I don't want the user to be involved in clicking things in a dialog box to make choices about what to name the file etc.

If none of the above, then, is there a commercial component that does this?  I already have a licensed commercial copy of QuickPDF installed, but that deals with viewing and manipulating .Pdfs, not converting .html to .Pdf.

Thanks in advance for any help you can provide.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: How can I programmatically and silently convert an html into pdf?
« Reply #1 on: June 27, 2019, 12:28:31 am »
for the PDF look at this
Code: Pascal  [Select][+][-]
  1. var AfrReport: TfrReport;
  2. aForm.AfrReport.ExportTo(TFrTNPDFExportFilter, UserDir + cSQL + '.pdf')
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: How can I programmatically and silently convert an html into pdf?
« Reply #2 on: June 27, 2019, 03:38:38 am »
Just looking at the documentation for LazReport... I'm unsure if it can load a .html file.  Seems like it is only for running reports on database information.  What is the command to make LazReport load an .html file so it can then be output as a .pdf file as per your code lines above?

RedOctober

  • Sr. Member
  • ****
  • Posts: 452
Re: How can I programmatically and silently convert an html into pdf?
« Reply #3 on: June 27, 2019, 03:49:46 am »
I was able to get this to work in a test project:

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TfrmMain.btnConvertClick(Sender: TObject);
  3. var AProcess: TProcess;
  4. begin
  5.  
  6.   AProcess := TProcess.Create(nil);
  7.  
  8.   // Tell the new AProcess what the command to execute is.
  9.   AProcess.Executable:= 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe';
  10.  
  11.   AProcess.Parameters.Add('--headless');
  12.   AProcess.Parameters.Add('--print-to-pdf=D:\Projects\Lazarus\TryOut\HTML2PDF\file1.pdf');
  13.   AProcess.Parameters.Add('D:\Projects\Lazarus\TryOut\HTML2PDF\example.html');
  14.  
  15.   // We will define an option for when the program
  16.   // is run. This option will make sure that our program
  17.   // does not continue until the program we will launch
  18.   // has stopped running.                vvvvvvvvvvvvvv
  19.   AProcess.Options := AProcess.Options + [poWaitOnExit];
  20.  
  21.   // Now let AProcess run the program
  22.   AProcess.Execute;
  23.  
  24.   // This is not reached until ppc386 stops running.
  25.   AProcess.Free;
  26. end;
  27.  
  28.  
  29.  
  30.  

Result:  file1.pdf is produced in the designated folder.


lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How can I programmatically and silently convert an html into pdf?
« Reply #4 on: June 27, 2019, 04:12:33 am »
Just to be on the sure side, you should use try...finally and test the ExitCode of the process:
Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.btnConvertClick(Sender: TObject);
  2. var AProcess: TProcess;
  3. begin
  4.   AProcess := TProcess.Create(nil);
  5.   try
  6.     // Tell the new AProcess what the command to execute is.
  7.     AProcess.Executable:= 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe';
  8.  
  9.     AProcess.Parameters.Add('--headless');
  10.     AProcess.Parameters.Add('--print-to-pdf=D:\Projects\Lazarus\TryOut\HTML2PDF\file1.pdf');
  11.     AProcess.Parameters.Add('D:\Projects\Lazarus\TryOut\HTML2PDF\example.html');
  12.  
  13.     // We will define an option for when the program
  14.     // is run. This option will make sure that our program
  15.     // does not continue until the program we will launch
  16.     // has stopped running.                vvvvvvvvvvvvvv
  17.     AProcess.Options := AProcess.Options + [poWaitOnExit];
  18.  
  19.     // Now let AProcess run the program
  20.     AProcess.Execute;
  21.  
  22.     // This is not reached until Chrome stops running.
  23.     {Note: Before release, test that 0 is *really* the "all-right" ExitCode}
  24.     if AProcess.ExitCode <> 0 then
  25.       ShowMessageFmt('Chrome error: %d', [AProcess.ExitCode]);
  26.   finally
  27.     AProcess.Free;
  28.   end;
  29. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018