44[ ![ ] ( https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square )] ( #does-this-example-address-your-development-requirementsobjectives )
55<!-- default badges end -->
66
7- # DevExpress VCL Reports— Generate a PDF Report Using a Headless (non-GUI) application
7+ # DevExpress VCL Reports — Generate a Report Using a Headless (non-GUI) Application
88
99This example uses the DevExpress VCL Reports components to generate a PDF report using a command line application.
10- The example demonstrates the capabilities of VCL Reports for producing reports in backend applications, webservers, or command line applications, all without the need for a GUI.
10+ The example demonstrates the capabilities of VCL Reports for producing reports in backend applications, Windows services,
11+ or plain command line applications, all without the need for a GUI.
1112
1213The example includes projects for both [ Delphi] ( ./Delphi ) and [ C++Builder] ( ./CPB ) .
1314
@@ -25,16 +26,95 @@ See: [DevExpress Reports Prerequisites](https://docs.devexpress.com/VCL/405773/E
2526By following the steps outlined in this example, you'll create an application that produce the
2627[ DevExpress VCL Reports] ( https://docs.devexpress.com/VCL/405469/ExpressReports/vcl-reports ) .
2728
28- ### Prepare a Template Report Layout
29+ ### Step 1: Initialize a Report and Import a Report Layout
2930
3031An application needs a template report layout, created in the [ Report Designer] ( https://docs.devexpress.com/VCL/405469/ExpressReports/vcl-reports ) .
3132
32- You can [ store a report layout in a REPX file] ( https://github.com/DevExpress-Examples/vcl-reports-store-layout-template-file )
33- or [ in a database] ( https://github.com/DevExpress-Examples/vcl-reports-store-layout-template-database ) .
34-
35- This example loads a report layout from the [ ` Order.repx ` ] ( ./Order.repx ) file.
36-
37- ###
33+ You can [ import a report layout from a REPX file] ( https://github.com/DevExpress-Examples/vcl-reports-store-layout-template-file )
34+ or [ read a layout from a database] ( https://github.com/DevExpress-Examples/vcl-reports-store-layout-template-database ) .
35+
36+ The current example imports a report layout from the ` Order.repx ` file.
37+
38+ ** Delphi:**
39+ ``` pas
40+ // Step 1: Create a report instance and load the report layout
41+ AReport := TdxReport.Create(nil);
42+ try
43+ // Set an internal report name (does not affect the exported PDF content and file name)
44+ AReport.ReportName := 'OrderReport';
45+ // Load the report layout from the specified file
46+ AReport.Layout.LoadFromFile(ALayout);
47+ // ...
48+ finally
49+ AReport.Free;
50+ end;
51+ ```
52+
53+ ### Step 2: Create a Database Connection
54+
55+ A VCL Report requires a source of data, which is usually represented by a database connection.
56+ The current example is using SQLite with the Northwind example database stored in ` nwind.db ` .
57+
58+ ``` pas
59+ // Step 2: Create a database connection
60+ AConnection := TdxBackendDatabaseSQLConnection.Create(nil);
61+ try
62+ // Assign a database name matching the name specified in the report layout
63+ AConnection.DisplayName := 'NWindConnectionString';
64+ // Assign a connection string to use the local SQLite database
65+ AConnection.ConnectionString := 'XpoProvider=SQLite; Data Source=..\nwind.db; Mode=ReadOnly';
66+ // ...
67+ finally:
68+ AConnection.Free;
69+ end:
70+ ```
71+
72+ ### Step 3: Define Report Parameter Values
73+
74+ A report layout may have one or more parameters.
75+ VCL Reports use parameters to modify database queries and produce different reports using the same layout and data.
76+ For example, ` Order.repx ` has a single ` OrderIDParameter ` used to produce reports on customer orders with given Order IDs.
77+
78+ To modify parameters, load them using the ` LoadParametersFromReport ` method and assign values to members of the ` Report.Parameters ` list:
79+
80+ ** Delphi:**
81+ ``` pas
82+ // Step 2: Define Report Parameter Values
83+ AReport.LoadParametersFromReport;
84+ // Set the value of the "OrderIdParameter" parameter in the report layout
85+ for AReportParameter in AReport.Parameters do
86+ if AReportParameter.Name = 'OrderIdParameter' then
87+ AReportParameter.Value := AOrderID;
88+ ```
89+
90+ To learn more, follow the guide: [ VCL Backend: Supported Database Systems] ( https://docs.devexpress.com/VCL/405703/ExpressCrossPlatformLibrary/vcl-backend/vcl-backend-supported-database-systems ) .
91+
92+ ### Step 4: Export and Save to a File
93+
94+ Once you have configured a report and a data source, and defined report parameters.
95+ you can export a report to one or more formats.
96+
97+ The current example exports a PDF report and saves it to a file:
98+
99+ ** Delphi:**
100+ ``` pas
101+ // Step 4: Export a report to PDF
102+ AStream := TMemoryStream.Create;
103+ try
104+ // Export a report to PDF format and save it to a memory stream
105+ AReport.ExportToPDF(AStream);
106+ // Write the content of the memory stream to a file
107+ AStream.SaveToFile(AOutput);
108+ Writeln('Report saved to: ', AOutput);
109+ finally
110+ AStream.Free;
111+ end;
112+ ```
113+
114+ ### Optional: Exporting Multiple Reports
115+
116+ Headless approach allows you to efficiently produce multiple reports using the same layout and data, but different parameter combinations.
117+ You can repeat steps 3 and 4 for each parameter combinations.
38118
39119
40120## Files to Review
0 commit comments