Skip to content

Commit c7ddadf

Browse files
committed
delphi: simplify code
1 parent 588f53b commit c7ddadf

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

Delphi/PDFReportGenerator.dpr

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,25 @@ uses
1313
dxReport, // DevExpress Reports frontend
1414
dxReport.Parameters;
1515

16-
// Export a report to a PDF file in headless mode:
16+
// Export a report to a PDF file in non-interactive mode:
1717
// without showing the report in the Report Designer or Report Viewer, or otherwise using UI.
18-
19-
procedure ExportReportToPdf(const ALayout, AOrderID, AOutput: string);
2018
// Parameters:
21-
// ALayout: path to the report layout file (.repx), default value: '..\Order.repx'
22-
// AOrderID: order ID to filter report data, default value: '11077'
23-
// AOutput: path to the output PDF file, default value: '<ALayout>_<AOrderID>.pdf'
19+
// AOrderID: order ID to use in report data, for example, '11077'
20+
procedure ExportReportToPdf(const AOrderID: string);
2421
var
2522
AConnection: TdxBackendDatabaseSQLConnection;
2623
AReport: TdxReport;
2724
AReportParameter: TdxReportParameter;
2825
AStream: TMemoryStream;
26+
AOutputFileName: string;
2927
begin
3028
// Step 1: Create a report instance and load the report layout
3129
AReport := TdxReport.Create(nil);
3230
try
3331
// Set an internal report name (does not affect the exported PDF content and file name)
3432
AReport.ReportName := 'OrderReport';
3533
// Load the report layout from the specified file
36-
AReport.Layout.LoadFromFile(ALayout);
34+
AReport.Layout.LoadFromFile('..\Order.repx');
3735

3836
// Step 2: Create a database connection
3937
AConnection := TdxBackendDatabaseSQLConnection.Create(nil);
@@ -46,18 +44,17 @@ begin
4644
// Step 3: Define Report Parameter Values
4745
AReport.LoadParametersFromReport;
4846
// Set the value of the "OrderIdParameter" parameter in the report layout
49-
for AReportParameter in AReport.Parameters do
50-
if AReportParameter.Name = 'OrderIdParameter' then
51-
AReportParameter.Value := AOrderID;
47+
AReport.Parameters['OrderIdParameter'].Value := AOrderID;
5248

5349
// Step 4: Export a report to PDF
5450
AStream := TMemoryStream.Create;
5551
try
5652
// Export a report to PDF format and save it to a memory stream
5753
AReport.ExportToPDF(AStream);
5854
// Write the content of the memory stream to a file
59-
AStream.SaveToFile(AOutput);
60-
Writeln('Report saved to: ', AOutput);
55+
AOutputFileName := '..\Order_' + AOrderID + '.pdf';
56+
AStream.SaveToFile(AOutputFileName);
57+
Writeln('Report saved to: ', AOutputFileName);
6158
finally
6259
AStream.Free;
6360
end;
@@ -72,30 +69,34 @@ end;
7269
// The helper application entry point that parses command-line parameters and calls the export procedure
7370
begin
7471
var
75-
ALayout, AOrderID, AOutput, AParam: string;
72+
AOrderID, AParam: string;
73+
Key, Value: string;
74+
i: Integer;
7675
try
77-
ALayout := '..\Order.repx'; // Default value
78-
AOrderID := '11077'; // Default value
76+
AOrderID := ''; // Initialize as empty
7977

8078
// Parse command-line parameters in the format "key=value"
81-
for var i := 1 to ParamCount do
79+
for i := 1 to ParamCount do
8280
begin
8381
AParam := ParamStr(i);
8482
if AParam.Contains('=') then
8583
begin
86-
var Key := Copy(AParam, 1, Pos('=', AParam) - 1).ToLower;
87-
var Value := Copy(AParam, Pos('=', AParam) + 1, Length(AParam));
84+
Key := Copy(AParam, 1, Pos('=', AParam) - 1).ToLower;
85+
Value := Copy(AParam, Pos('=', AParam) + 1, Length(AParam));
8886

89-
if Key = 'alayout' then
90-
ALayout := Value
91-
else if Key = 'aorderid' then
87+
if Key = 'orderid' then
9288
AOrderID := Value;
9389
end;
9490
end;
9591

96-
AOutput := ChangeFileExt(ALayout, '') + '_' + AOrderID + '.pdf';
92+
// Check if AOrderID is provided
93+
if AOrderID.IsEmpty then
94+
begin
95+
Writeln('Error: OrderID parameter is required. For example: PDFReportGenerator.exe orderid=11077');
96+
Halt(1); // Exit with error code 1
97+
end;
9798

98-
ExportReportToPdf(ALayout, AOrderID, AOutput);
99+
ExportReportToPdf(AOrderID);
99100
except
100101
on E: Exception do
101102
Writeln(E.ClassName, ': ', E.Message);

0 commit comments

Comments
 (0)