Skip to content

Commit a44e3c9

Browse files
NickVolynkinAlex-Pertsev-DevExpressIlia-Nenashev-devXdennis-garavsky
committed
delphi: Add an example application
- Load a report layout from 'Order.repx' - Read the order ID as a CLI parameter - Connect to the sample database `nwind.db` using SQLite - Export and write a report to 'Order_<id>.pdf' Co-authored-by: Alexey Pertsev <alexey.pertsev@devexpress.com> Co-authored-by: Ilia Nenashev <ilia.nenashev@devexpress.com> Co-authored-by: Denis Garavsky <denis.garavsky@devexpress.com>
1 parent ae23b3e commit a44e3c9

8 files changed

Lines changed: 1407 additions & 0 deletions

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# Most of the time, files here have not there place in a code repository.
3232
#Win32/
3333
#Win64/
34+
#Win64x/
3435
#OSX64/
3536
#OSXARM64/
3637
#Android/
@@ -61,6 +62,7 @@
6162
*.cfg
6263
*.hpp
6364
*Resource.rc
65+
*.rsp
6466

6567
# Delphi local files (user-specific info)
6668
*.local
@@ -75,6 +77,39 @@ __history/
7577
__recovery/
7678
*.~*
7779

80+
81+
# ------------------------------------------------------------
82+
# C++Builder specific
83+
# ------------------------------------------------------------
84+
85+
# C++Builder compiler outputs
86+
*.obj
87+
*.hpp
88+
*.ilc
89+
*.ild
90+
*.ilf
91+
*.ils
92+
*.map
93+
*.tds
94+
95+
# Precompiled headers
96+
*.pch
97+
98+
# C++Builder packages and libraries
99+
*.bpl
100+
*.bpi
101+
*.lib
102+
*.a
103+
*.dll
104+
*.so
105+
106+
# C++Builder intermediate / cache files
107+
*.cbproj.local
108+
*.cbproj.identcache
109+
*.cbproj.user
110+
*.cbtemp
111+
112+
78113
# Castalia statistics file (since XE7 Castalia is distributed with Delphi)
79114
*.stat
80115

Delphi/Order.repx

Lines changed: 159 additions & 0 deletions
Large diffs are not rendered by default.

Delphi/PDFReportGenerator.dpr

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
program PDFReportGenerator;
2+
3+
{$APPTYPE CONSOLE}
4+
5+
{$R *.res}
6+
7+
uses
8+
System.SysUtils, System.Classes, System.StrUtils, dxBackend, dxBackend.Bundled,
9+
dxBackend.ConnectionString.SQL, dxReport, dxReport.Parameters;
10+
var
11+
AOrderID: string;
12+
13+
// Export a report to a PDF file in non-interactive mode:
14+
// without showing the report in the Report Designer or Report Viewer, or otherwise using UI.
15+
// Parameters:
16+
// AOrderID: order ID to use in report data, for example, '11077'
17+
procedure ExportReportToPdf(const AOrderID: string);
18+
var
19+
AConnection: TdxBackendDatabaseSQLConnection;
20+
AReport: TdxReport;
21+
AStream: TMemoryStream;
22+
AOutputFileName: string;
23+
24+
begin
25+
// Step 1: Create a report instance and load the report layout
26+
AReport := TdxReport.Create(nil);
27+
try
28+
// Set an internal report name (does not affect the exported PDF content and file name)
29+
AReport.ReportName := 'OrderReport';
30+
// Load the report layout from the specified file
31+
AReport.Layout.LoadFromFile('Order.repx');
32+
33+
// Step 2: Create a database connection
34+
AConnection := TdxBackendDatabaseSQLConnection.Create(nil);
35+
try
36+
// Assign a database name matching the name specified in the report layout
37+
AConnection.DisplayName := 'NWindConnectionString';
38+
// Assign a connection string required to use the local SQLite database
39+
AConnection.ConnectionString := 'XpoProvider=SQLite; Data Source=nwind.db; Mode=ReadOnly';
40+
41+
// Step 3: Define Report Parameter Values
42+
AReport.LoadParametersFromReport;
43+
// Set the "OrderIdParameter" value in the report layout
44+
AReport.Parameters['OrderIdParameter'].Value := AOrderID;
45+
46+
// Step 4: Export a report to PDF
47+
AStream := TMemoryStream.Create;
48+
try
49+
// Export a report to a memory stream in the PDF format
50+
AReport.ExportToPDF(AStream);
51+
// Save memory stream content to a file
52+
AOutputFileName := 'Order_' + AOrderID + '.pdf';
53+
AStream.SaveToFile(AOutputFileName);
54+
Writeln('Report saved to: ', AOutputFileName);
55+
finally
56+
AStream.Free;
57+
end;
58+
finally
59+
AConnection.Free;
60+
end;
61+
finally
62+
AReport.Free;
63+
end;
64+
end;
65+
66+
// The helper application entry point that parses command-line parameters and calls the export procedure
67+
begin
68+
if ParamCount < 1 then
69+
begin
70+
Writeln('Error: OrderID parameter is required. For example: PDFReportGenerator.exe 11077');
71+
Halt(1); // Exit with error code 1
72+
end;
73+
74+
AOrderID := ParamStr(1); // Read the first parameter as OrderID
75+
ExportReportToPdf(AOrderID);
76+
end.

Delphi/PDFReportGenerator.dproj

Lines changed: 1137 additions & 0 deletions
Large diffs are not rendered by default.

Delphi/PDFReportGenerator.res

26.3 KB
Binary file not shown.

Delphi/PDFReportGenerator_Icon.ico

26 KB
Binary file not shown.

Delphi/Products.pdf

67.6 KB
Binary file not shown.

Delphi/nwind.db

644 KB
Binary file not shown.

0 commit comments

Comments
 (0)