This example uses DevExpress Reports for Delphi/C++Builder to generate reports in a command line application. The application accepts an order ID parameter, queries the database for order data, and exports an "order" report as a PDF file.
This example bypasses the DevExpress Report Viewer dialog and generates a report using the DevExpress Reports backend. Use the approach demonstrated in this sample project to implement REST/Web API backends, Windows Services, workflows, and scheduled jobs. This approach can be particularly beneficial for the following usage scenarios (reports generated without direct user interaction):
- Mass-export reports to PDF, DOCX, image, and other formats.
- Share, email, and print report documents silently.
- Implement custom report management UIs.
DevExpress Reports Prerequisites
-
Open and build the Delphi project in the RAD Studio.
-
Start a console session and navigate to the Delphi project directory.
-
Run the app and specify the order ID (a number between 10248 and 11077):
> PDFReportGenerator.exe 11077 Report saved to: Order_11077.pdf -
Generate reports for other order IDs and compare generated PDF files.
Follow the steps below to create an application that imports a report layout from a file, binds the layout to data, and exports the generated report to a PDF file.
An application requires a template report layout previously created in the Report Designer. You can import a report layout from a REPX file or load a layout from a database.
This example imports a report layout from the Order.repx file.
Delphi:
AReport := TdxReport.Create(nil); // AReport: TdxReport;
try
// Set an internal report name (does not affect the exported content)
AReport.ReportName := 'Order Report';
// Load the report layout from the specified file
AReport.Layout.LoadFromFile('Order.repx');
// ...
finally
AReport.Free;
end;Create a database connection component to supply data to the report. This example uses a SQLite sample database (nwind.db).
Delphi:
// AConnection: TdxBackendDatabaseSQLConnection;
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';
// ...
finally
AConnection.Free;
end;For detailed information on data source management and supported database engines, refer to the following help topic: VCL Backend: Supported Database Systems.
A report layout may include one or more parameters.
Parameters allow you to modify database queries and generate different reports
based on the same report template and underlying data.
For example, Order.repx includes a single OrderIDParameter that filters data by order ID.
To modify parameters, load them using the TdxReport.Parameters.LoadFromLayout method and assign values to TdxReport.Parameters list members as follows:
Delphi:
AReport.Parameters.LoadFromLayout;
// Set the "OrderIdParameter" value in the report layout
AReport.Parameters['OrderIdParameter'].Value := AOrderID;This example exports a report to a PDF file using the TdxReport.ExportToPDF method:
Delphi:
AStream := TMemoryStream.Create; // AStream: TMemoryStream;
try
// Export a report to a memory stream in the PDF format
AReport.ExportToPDF(AStream);
// Save memory stream content to a file
AStream.SaveToFile('Order_' + AOrderID + '.pdf');
finally
AStream.Free;
end;For detailed information on available export formats, refer to the following help topic: TdxReport.ExportTo.
The approach outlined in this example allows you to generate and export multiple reports based on the same layout and data using a list of parameters. You need to initialize the report layout and data connection once (steps 1 and 2) and repeat steps 3 and 4 for each parameter.
Delphi:
// ...
AReport.LoadParametersFromReport;
for AOrderID in AOrderIDList:
AReport.Parameters['OrderIdParameter'].Value := AOrderID;
AStream := TMemoryStream.Create; // AStream: TMemoryStream;
try
// Export a report to a memory stream in the PDF format
AReport.ExportToPDF(AStream);
// Save memory stream content to a file
AStream.SaveToFile('Order_' + AOrderID + '.pdf');
finally
AStream.Free;
end;
end;- PDFReportGenerator.dpr generates a report in non-interactive (headless) mode.
- Order.repx contains a report layout designed to generate a customer order report. You can view and edit this file using the file storage example application.
- nwind.db contains the Northwind sample database.
- Introduction to VCL Reports
- Tutorial: Create a table report using the Report Wizard
- Use SQLite as a data source for reports (as demonstrated in the current example)
- API reference:
(you will be redirected to DevExpress.com to submit your response)
