How to make a PDF from Delphi / C++Builder / Lazarus

2020-05-14

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.
application form Delphi / Lazarus for saving PDF
Double-click on TfrxReport, enter FR Designer. Create a new report (File -> New report). Add TfrxMemoView with the text “Test text” to MasterData1.
Visual PDF designer in Delphi / Lazarus
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;

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.

Save to PDF button in Preview

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.

Settings of saving to PDF

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).

PDF/A-1a, PDF/A-1b, PDF/A-2b, PDF/A-2a, PDF/A-3a, PDF/A-3b standard selection

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 difference between PDF and PDF/A.

Version selection PDF 1.4, PDF 1.5, PDF 1.6, PDF 1.7

The non-archive PDF format also has several versions (and you can choose which one to save).

Service information setting of PDF-document

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.

PDF security settings

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.

Settings of Delphi PDF on preview open

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
procedure TForm1.Button1Click(Sender: TObject);
begin
{Generate a report. The report must be generated before exporting}
frxReport1.PrepareReport();
{Set the range of pages to export. By default, all pages of the generated report are exported.}
frxPDFExport1.PageNumbers := '2-3';
{Set the PDF standard
 TPDFStandard = (psNone, psPDFA_1a, psPDFA_1b, psPDFA_2a, psPDFA_2b, psPDFA_3a, psPDFA_3b);
It is required to add the frxExportPDFHelpers module to the uses list:
uses frxExportPDFHelpers;}
frxPDFExport1.PDFStandard := psNone;
{You can set the PDF standard version for PDFStandard = psNone
TPDFVersion = (pv14, pv15, pv16, pv17);
It is required to add the frxExportPDFHelpers module to the uses list:
uses frxExportPDFHelpers;}
frxPDFExport1.PDFVersion := pv17;
{To get smaller file size, you can set the compression}
frxPDFExport1.Compressed := True;
{Set whether to embed fonts in the resulting document.
 Embedding fonts significantly increases the size of the resulting document}
frxPDFExport1.EmbeddedFonts := False;
{Set whether we need to export the background image}
frxPDFExport1.Background := True;
{Disable export of objects with optimization for printing. With option enabled images will be high-quality but 9 times larger in volume}
frxPDFExport1.PrintOptimized := False;
{Set whether the resulting PDF will contain an external table of contents, as in the original report}
frxPDFExport1.Outline := False;
{Set whether to export images with transparency}
frxPDFExport1.Transparency := True;
{You can set the desired DPI of images. Enabling this option disables SaveOriginalImages option, which allows you to save images in their 
original form}
 frxPDFExport1.PictureDPI := 150;
 {Set the compression ratio of bitmap images}
 frxPDFExport1.Quality := 95;
 {Set whether to open the resulting file after export}
 frxPDFExport1.OpenAfterExport := False;
 {Set whether to display export progress
  (show which page is currently being exported)}
 frxPDFExport1.ShowProgress := False;
 {Set whether to display a dialog box with export filter settings}
 frxPDFExport1.ShowDialog := False;
{Set the name of the resulting file. Please note that if you do not set the file name and disable the export filter dialog box, the file name selection dialog will still be displayed}
frxPDFExport1.FileName := 'C:\Output\test.pdf';
{Fill in the corresponding fields of the Information tab}
frxPDFExport1.Title := 'Your Title';
frxPDFExport1.Author := 'Your Name';
 frxPDFExport1.Subject := 'Your Subject';
 frxPDFExport1.Keywords := 'Your Keywords';
 frxPDFExport1.Creator := 'Creator Name';
 frxPDFExport1.Producer := 'Producer Name';
 { Fill in the corresponding fields of the Security tab }
 frxPDFExport1.UserPassword := 'User Password';
 frxPDFExport1.OwnerPassword := 'Owner Password';
 frxPDFExport1.ProtectionFlags := [ePrint, eModify, eCopy, eAnnot];
 {Set the Viewer settings (Viewer tab)}
 frxPDFExport1.HideToolbar := False;
 frxPDFExport1.HideMenubar := False;
 frxPDFExport1.HideWindowUI := False;
 frxPDFExport1.FitWindow := False;
 frxPDFExport1.CenterWindow := False;
 frxPDFExport1.PrintScaling := False;
 {Export the report}
 frxReport1.Export(frxPDFExport1);
end;
 

 There you go – use it! Here are considered the functions and recording options from Lazarus and Delphi to PDF FastReport VCL version 6.6. The cost of a license is comparable (and often less) to the monthly salary of a developer, while only the options for setting up and saving to different versions of PDF here are equal to at least work-months of a developer with high qualifications and knowledge of the subtleties of different dialects of PDF.
By the way, you can even check the quality of the PDF conversion on our demo reports or some of your own for fun. We constantly check and test them for compliance with PDF standards (there are special validators but at the same time the validation passage does not always guarantee the correspondence of what you see and what will be printed, we have all those individual stories about it). For example, do you know how beautiful illustrations in your PDF will be? Read more here: https://www.fast-report.com/en/blog/200/show/.

November 20, 2024

Localization and Language Switching in FastReport VCL

FastReport VCL supports 40 languages for interface localization and allows you to change the language on the fly through menus or code, without recompilation.
November 01, 2024

New Features of the FastReport VCL Editor

We are considering new features of the report editor: extension lines, highlighting of intersecting objects, updated report and data trees.
October 30, 2024

Using Styles When Creating Reports in FastReport VCL

The article discusses one of the new features of FastReport VCL — the use of styles and style sheets.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.