How to make a PDF from Delphi / C++Builder / Lazarus
Michael Philippenko
Michael Philippenko
[SHOWTOGROUPS=4,20]
How to make a PDF from Delphi / C++Builder / Lazarus
Michael Philippenko
Quite often, you need to get a PDF document from a Pascal application - either Delphi or Lazarus.
Usually it is recommended to use specialized solutions, PDF converters (such as itext, Synopse PDF Engine, PowerPDF, HotPDF, PDF Creator Pilot, PDFtoolkit VCL, Debenu Quick PDF Library etc.). In this article we will not consider their advantages and disadvantages.
Unexpectedly for us, it turned out that even if there is no “PDF” in the product name, the capabilities of FastReport VCL for PDF output in Delphi cover many of the developer’s needs. And many of our customers use ONLY FastReport features to make PDF!
So, first of all – you can send any arbitrary complex report to PDF. Let’s try!
Create a new VCL application in Delphi
Add TfrxReport, TfrxPDFExport and TButton components from the FastReport tab to the form.
Double-click on TfrxReport, enter FR Designer. Create a new report (File -> New report). Add TfrxMemoView with the text “Test text” to MasterData1.
Set MasterData1.RowCount = 200 (to repeat the line 200 times and generate a 3-4-page report, since our example does not use the dataset), exit the designer and write the OnClick handler for Button1:
In fact, this code is enough for comprehensive work, generating a report and exporting to PDF. Now create a document of any complexity in Design time (you can connect any data sources and take the information from there) – tables, lists, illustrations, maps, QR codes – for this make sure to add the appropriate components to the project first (otherwise they will not exist in Run time), composite and multi-page documents with anchors, inline links and a table of contents – anything you need and any size – a one-page receipt, a one-page catalog, an annual report of factory staff movements on thousands of pages.
And don’t forget to put the PDF export component to our project! Launch and click on the only button on the form.
We can see the preview window and the export to PDF button.
Go ahead – save a PDF from our Delphi application!
Click on the button – call the export from the preview below I’ll show you how to do all this from the code, you can just click on the link – send to PDF from the code). Immediately we see the resulting PDF settings dialog.
As you can see, any professional PDF converter will envy such a set of options! We can choose which pages of our document to send to PDF, which version of PDF to use, compression reduces the size of the resulting file, embedding fonts allows to save the appearance of the document of any third-party device. We can choose if the background will be attached to the PDF document; our PDF can also be optimized for printing (image quality will be better but the size will be larger) or only for on-screen presentation. We can set if the resulting PDF will contain an external table of contents as in the original report (I don’t have it in my example so it is not possible to select it), transparency, compression ratio of bitmap images. By the way, one of the important features of FastReport VCL 6.6 is that vector images will remain a vector form in a PDF file, in other words – lossless, and this will be especially noticeable on 2D barcodes and maps. Another feature – all text, including RTF, will be vectorized when saved to PDF, i.e. the quality will not be lost while retaining the ability to copy a section of text from PDF (unless you prohibit the appropriate option which I will mention below).
Save to: where exactly we will send our PDF (local file or e-mail or clouds). Open after export - the resulting file will be opened immediately after export by the PDF viewer assigned by default to the operating system (for example, Adobe Acrobat Reader).
You can export the generated document in archive formats such as PDF/A-1a, PDF/A-1b, PDF/A-2a, PDF/A-2b, PDF/A-3a, PDF/A-3b – they are specially designed for keeping documents unchanged in electronic form. For example, fonts, images, third-party objects that are present in a document are automatically embedded in the document in this standard. Quite often one of these standards is used for electronic document management in large organizations. By the way, here you can read a little more about the Для просмотра ссылки Войдиили Зарегистрируйся
The non-archive PDF format also has several versions (and you can choose which one to save).
Service information, which will also go to a PDF file: title, author, subject, keywords (you can upload PDF to the web, it will be perfectly indexed), PDF authoring tool, document producer.
Security – protecting the document from opening by using a password (using RC4 encryption).
The ability to prohibit printing and modifying a document, copying of text and graphics, adding or modifying text notes.
Setting up the PDF viewer when you open the document:
Hide toolbar, hide menubar, hide window user interface, fit window, center window, print scaling. Usually, when exporting I use the parameters set by default but this time I reviewed all the parameters.
So, if we or our users do not need all this visual diversity, then we can immediately
send to PDF from Delphi or Lazarus code
How to make a PDF from Delphi / C++Builder / Lazarus
Michael Philippenko
Quite often, you need to get a PDF document from a Pascal application - either Delphi or Lazarus.
Usually it is recommended to use specialized solutions, PDF converters (such as itext, Synopse PDF Engine, PowerPDF, HotPDF, PDF Creator Pilot, PDFtoolkit VCL, Debenu Quick PDF Library etc.). In this article we will not consider their advantages and disadvantages.
Unexpectedly for us, it turned out that even if there is no “PDF” in the product name, the capabilities of FastReport VCL for PDF output in Delphi cover many of the developer’s needs. And many of our customers use ONLY FastReport features to make PDF!
So, first of all – you can send any arbitrary complex report to PDF. Let’s try!
Create a new VCL application in Delphi
Add TfrxReport, TfrxPDFExport and TButton components from the FastReport tab to the form.
Double-click on TfrxReport, enter FR Designer. Create a new report (File -> New report). Add TfrxMemoView with the text “Test text” to MasterData1.
Set MasterData1.RowCount = 200 (to repeat the line 200 times and generate a 3-4-page report, since our example does not use the dataset), exit the designer and write the OnClick handler for Button1:
1 2 3 4 5 6 7 | procedure TForm1.Button1Click(Sender: TObject); begin {Generate a report. The report must be generated before exporting} if frxReport1.PrepareReport then frxReport1.ShowPreparedReport; {and show preview window} end; |
And don’t forget to put the PDF export component to our project! Launch and click on the only button on the form.
We can see the preview window and the export to PDF button.
Go ahead – save a PDF from our Delphi application!
Click on the button – call the export from the preview below I’ll show you how to do all this from the code, you can just click on the link – send to PDF from the code). Immediately we see the resulting PDF settings dialog.
As you can see, any professional PDF converter will envy such a set of options! We can choose which pages of our document to send to PDF, which version of PDF to use, compression reduces the size of the resulting file, embedding fonts allows to save the appearance of the document of any third-party device. We can choose if the background will be attached to the PDF document; our PDF can also be optimized for printing (image quality will be better but the size will be larger) or only for on-screen presentation. We can set if the resulting PDF will contain an external table of contents as in the original report (I don’t have it in my example so it is not possible to select it), transparency, compression ratio of bitmap images. By the way, one of the important features of FastReport VCL 6.6 is that vector images will remain a vector form in a PDF file, in other words – lossless, and this will be especially noticeable on 2D barcodes and maps. Another feature – all text, including RTF, will be vectorized when saved to PDF, i.e. the quality will not be lost while retaining the ability to copy a section of text from PDF (unless you prohibit the appropriate option which I will mention below).
Save to: where exactly we will send our PDF (local file or e-mail or clouds). Open after export - the resulting file will be opened immediately after export by the PDF viewer assigned by default to the operating system (for example, Adobe Acrobat Reader).
You can export the generated document in archive formats such as PDF/A-1a, PDF/A-1b, PDF/A-2a, PDF/A-2b, PDF/A-3a, PDF/A-3b – they are specially designed for keeping documents unchanged in electronic form. For example, fonts, images, third-party objects that are present in a document are automatically embedded in the document in this standard. Quite often one of these standards is used for electronic document management in large organizations. By the way, here you can read a little more about the Для просмотра ссылки Войди
The non-archive PDF format also has several versions (and you can choose which one to save).
Service information, which will also go to a PDF file: title, author, subject, keywords (you can upload PDF to the web, it will be perfectly indexed), PDF authoring tool, document producer.
Security – protecting the document from opening by using a password (using RC4 encryption).
The ability to prohibit printing and modifying a document, copying of text and graphics, adding or modifying text notes.
Setting up the PDF viewer when you open the document:
Hide toolbar, hide menubar, hide window user interface, fit window, center window, print scaling. Usually, when exporting I use the parameters set by default but this time I reviewed all the parameters.
So, if we or our users do not need all this visual diversity, then we can immediately
send to PDF from Delphi or Lazarus code
saving to PDF from Delphi with PDF parameters |