-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFReportGenerator.dpr
More file actions
76 lines (66 loc) · 2.5 KB
/
PDFReportGenerator.dpr
File metadata and controls
76 lines (66 loc) · 2.5 KB
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
66
67
68
69
70
71
72
73
74
75
76
program PDFReportGenerator;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, System.Classes, System.StrUtils, dxBackend, dxBackend.Bundled,
dxBackend.ConnectionString.SQL, dxReport, dxReport.Parameters;
var
AOrderID: string;
// Export a report to a PDF file in non-interactive mode:
// without showing the report in the Report Designer or Report Viewer, or otherwise using UI.
// Parameters:
// AOrderID: order ID to use in report data, for example, '11077'
procedure ExportReportToPdf(const AOrderID: string);
var
AConnection: TdxBackendDatabaseSQLConnection;
AReport: TdxReport;
AStream: TMemoryStream;
AOutputFileName: string;
begin
// Step 1: Create a report instance and load the report layout
AReport := TdxReport.Create(nil);
try
// Set an internal report name (does not affect the exported PDF content and file name)
AReport.ReportName := 'OrderReport';
// Load the report layout from the specified file
AReport.Layout.LoadFromFile('Order.repx');
// Step 2: Create a database connection
AConnection := TdxBackendDatabaseSQLConnection.Create(nil);
try
// Assign a database name matching the name specified in the report layout
AConnection.DisplayName := 'NWindConnectionString';
// Assign a connection string required to use the local SQLite database
AConnection.ConnectionString := 'XpoProvider=SQLite; Data Source=nwind.db; Mode=ReadOnly';
// Step 3: Define Report Parameter Values
AReport.LoadParametersFromReport;
// Set the "OrderIdParameter" value in the report layout
AReport.Parameters['OrderIdParameter'].Value := AOrderID;
// Step 4: Export a report to PDF
AStream := TMemoryStream.Create;
try
// Export a report to a memory stream in the PDF format
AReport.ExportToPDF(AStream);
// Save memory stream content to a file
AOutputFileName := 'Order_' + AOrderID + '.pdf';
AStream.SaveToFile(AOutputFileName);
Writeln('Report saved to: ', AOutputFileName);
finally
AStream.Free;
end;
finally
AConnection.Free;
end;
finally
AReport.Free;
end;
end;
// The helper application entry point that parses command-line parameters and calls the export procedure
begin
if ParamCount < 1 then
begin
Writeln('Error: OrderID parameter is required. For example: PDFReportGenerator.exe 11077');
Halt(1); // Exit with error code 1
end;
AOrderID := ParamStr(1); // Read the first parameter as OrderID
ExportReportToPdf(AOrderID);
end.