@@ -7,96 +7,95 @@ program PDFReportGenerator;
77uses
88 System.SysUtils,
99 System.Classes,
10- System.IniFiles,
1110 dxBackend, // DevExpress Reports and Dashboards backend
1211 dxBackend.Bundled,
1312 dxBackend.ConnectionString.SQL,
1413 dxReport, // DevExpress Reports frontend
1514 dxReport.Parameters;
16- procedure ExportReportToPdf ;
1715
16+ // Export a report to a PDF file in headless mode:
17+ // without showing the report in the Report Designer or Report Viewer, or otherwise using UI.
18+
19+ procedure ExportReportToPdf (const ALayout, AOrderID, AOutput: string);
20+ // Parameters:
21+ // ALayout: path to the report ALayout file (.repx), default value: '..\Order.repx'
22+ // AOrderID: order ID to filter report data, default value: '11077'
23+ // AOutput: path to the AOutput PDF file, default value: '<ALayout>_<AOrderID>.pdf'
1824var
19- AConfig: TIniFile;
2025 AConnection: TdxBackendDatabaseSQLConnection;
2126 AReport: TdxReport;
2227 AReportParameter: TdxReportParameter;
2328 AStream: TMemoryStream;
2429begin
25- // Start the DevExpress backend.
26- TdxBackend.Instance.Start;
27-
28- // Read the configuration file.
29- AConfig := TIniFile.Create(' ..\PDFReportGenerator.ini' );
30+ // Create a database connection
31+ AConnection := TdxBackendDatabaseSQLConnection.Create(nil );
3032 try
31- // Create a database connection.
32- AConnection := TdxBackendDatabaseSQLConnection.Create(nil );
33+ // Assign a database name matching the name specified in the report ALayout
34+ AConnection.DisplayName := ' NWindConnectionString' ;
35+ // Assign a connection string to use the local SQLite database
36+ AConnection.ConnectionString := ' XpoProvider=SQLite; Data Source=..\nwind.db; Mode=ReadOnly' ;
37+
38+ // Create a report instance
39+ AReport := TdxReport.Create(nil );
3340 try
34- // Assign the name "NWindConnectionString" to the database connection.
35- // As part of this operation, the connection manager registers the connection and makes it available to reports by name.
36- AConnection.DisplayName := ' NWindConnectionString' ;
37- // AConnection.DisplayName := AConfig.ReadString('Connection', 'Name', '');
41+ // Set an internal report name (does not affect the exported PDF content and file name)
42+ AReport.ReportName := ' OrderReport' ;
43+ // Load the report ALayout from the specified file
44+ AReport.Layout.LoadFromFile(ALayout);
45+ // Load report parameters to enable changing their values
46+ AReport.LoadParametersFromReport;
3847
39- // Assign a connection string specifying a SQLite database in `nwind.db`.
40- AConnection.ConnectionString := ' XpoProvider=SQLite; Data Source=..\nwind.db; Mode=ReadOnly' ;
41- // AConnection.ConnectionString := AConfig.ReadString('Connection', 'ConnectionString', '');
48+ // Set the value of the "OrderIdParameter" parameter in the report ALayout
49+ for AReportParameter in AReport.Parameters do
50+ if AReportParameter.Name = ' OrderIdParameter' then
51+ AReportParameter.Value := AOrderID;
4252
43- // Create a new DevExpress Report.
44- AReport := TdxReport.Create(nil );
53+ AStream := TMemoryStream.Create;
4554 try
46- // Assign a name to the newly created report.
47- AReport.ReportName := ' Order' ;
48-
49- // Load report layout from a `.repx` layout file.
50- AReport.Layout.LoadFromFile(' ..\..\..\Order.repx' );
51- // AReport.Layout.LoadFromFile(AConfig.ReadString('Report', 'LayoutFileName', ''));
52-
53- // Read a list of parameter names from the layout file.
54- // (Presented layout file has a single parameter named `OrderIdParameter`.)
55- AReport.LoadParametersFromReport;
56-
57- // For each parameter, read and assign its value from the configuration file.
58- // Parameter values are used to filter data obtained from a database.
59- // The presented technique allows you to produce various reports by filtering data with parameters.
60- for AReportParameter in AReport.Parameters do
61- AReportParameter.Value := AConfig.ReadString(' Parameters' , AReportParameter.Name , ' ' );
62-
63- // Create a memory stream to hold the exported PDF data.
64- AStream := TMemoryStream.Create;
65- try
66- // Export the report to PDF format and save it to the memory stream.
67- AReport.ExportToPDF(AStream);
68- // Write the content of the memory stream to an output file.
69- // The file name is specified in the configuration file.
70-
71- Writeln(' Writing ' + AConfig.ReadString(' Parameters' , ' OrderIdParameter' , ' unknown' ) + ' .pdf' );
72- AStream.SaveToFile(AConfig.ReadString(' Parameters' , ' OrderIdParameter' , ' unknown' ) + ' .pdf' );
73-
74- // AStream.SaveToFile(AConfig.ReadString('Report', 'OutputFileName', ''));
75- finally
76- Writeln(' Running AStream.Free' );
77- AStream.Free;
78- Writeln(' Successful AStream.Free' );
79- end ;
55+ // Export the report to PDF format and save it to a Stream object
56+ AReport.ExportToPDF(AStream);
57+ // Write the content of the Stream object to a file
58+ AStream.SaveToFile(AOutput);
59+ Writeln(' Report saved to: ' , AOutput);
8060 finally
81- Writeln(' Running AReport.Free' );
82- AReport.Free;
83- Writeln(' Successful AReport.Free' );
61+ AStream.Free;
8462 end ;
8563 finally
86- Writeln(' Running AConnection.Free' );
87- AConnection.Free;
88- Writeln(' Successful AConnection.Free' );
64+ AReport.Free;
8965 end ;
9066 finally
91- Writeln(' Running AConfig.Free' );
92- AConfig.Free;
93- Writeln(' Successful AConfig.Free' );
67+ AConnection.Free;
9468 end ;
9569end ;
9670
71+
72+ // The helper application entry point that parses command-line parameters and calls the export procedure
9773begin
74+ var
75+ ALayout, AOrderID, AOutput, AParam: string;
9876 try
99- ExportReportToPdf;
77+ ALayout := ' ..\Order.repx' ; // Default value
78+ AOrderID := ' 11077' ; // Default value
79+
80+ // Parse command-line parameters in the format "key=value"
81+ for var i := 1 to ParamCount do
82+ begin
83+ AParam := ParamStr(i);
84+ if AParam.Contains(' =' ) then
85+ begin
86+ var Key := Copy(AParam, 1 , Pos(' =' , AParam) - 1 ).ToLower;
87+ var Value := Copy(AParam, Pos(' =' , AParam) + 1 , Length(AParam));
88+
89+ if Key = ' ALayout' then
90+ ALayout := Value
91+ else if Key = ' AOrderID' then
92+ AOrderID := Value ;
93+ end ;
94+ end ;
95+
96+ AOutput := ChangeFileExt(ALayout, ' ' ) + ' _' + AOrderID + ' .pdf' ;
97+
98+ ExportReportToPdf(ALayout, AOrderID, AOutput);
10099 except
101100 on E: Exception do
102101 Writeln(E.ClassName, ' : ' , E.Message);
0 commit comments