Create an Angular Front-End Application with a Document Viewer
- 5 minutes to read
This document describes how to create a Reporting application with the Angular client part.
Note
You can use the DevExpress Angular SPA template to create an Angular Reporting application. For more information, refer to the following help topic: Use Visual Studio Templates to Create an Angular Application with a Document Viewer.
The project consists of the server and client parts.
Server (Back-End) Application
Use DevExpress CLI Template
You can use DevExpress CLI Templates to create an ASP.NET Core back-end application. To do this, follow the steps below:
Install DevExpress ASP.NET Core project templates from nuget.org:
dotnet new install DevExpress.AspNetCore.ProjectTemplates
Create a back-end Reporting application for a Document Viewer:
dotnet new dx.aspnetcore.reporting.backend -n ServerApp --add-designer false
You can use the following parameters to see available command options:
-? | -h | --help
.Enable cross-origin requests (CORS). Specify the policy that allows any local application to access the report’s back-end. Use the SetIsOriginAllowed method to set it up.
Call the UseCors method and pass the policy name as a parameter. The
UseCors
method should be called after theUseRouting
method and before any MVC-related code. Place theUseCors
method before theUseMvc
orUseEndpoints
methods.Open the application startup file and insert the following code:
var builder = WebApplication.CreateBuilder(args); builder.Services.AddCors(options => { options.AddPolicy("AllowCorsPolicy", builder => { // Allow all ports on local host. builder.SetIsOriginAllowed(origin => new Uri(origin).Host == "localhost"); builder.AllowAnyHeader(); builder.AllowAnyMethod(); }); }); var app = builder.Build(); app.UseRouting(); app.UseCors("AllowCorsPolicy"); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); app.Run();
Use Visual Studio Template
To create a back-end application from a Microsoft or DevExpress Template in the Visual Studio, review the following help topics:
- Document Viewer Server-Side Application (ASP.NET Core)
- Document Viewer’s Server-Side Configuration (ASP.NET MVC)
Client (Front-End) Application
Perform the following steps to create and configure the client part:
Make sure you have the current Node.js version with npm installed on your machine.
node -v npm -v
Open the console window and install the Angular CLI v16+ globally.
npm install -g @angular/cli
When prompted for options, select default settings (press Enter).
Create a new folder to store client files. Navigate to the created folder and run the command to create a new project:
ng new angular-document-viewer
Refer to the Angular documentation for information on the Angular application structure.
Change your current directory to the project root folder and install the following packages:
cd angular-document-viewer npm install devextreme@24.2-stable devextreme-angular@24.2-stable @devexpress/analytics-core@24.2-stable devexpress-reporting-angular@24.2-stable
Note
Front-end and back-end applications should use the same version of DevExpress controls.
Replace the content of the angular-document-viewer\src\app\app.component.ts file with the following code:
import { Component, ViewEncapsulation } from '@angular/core'; import { CommonModule } from '@angular/common'; import { RouterOutlet } from '@angular/router'; import { DxReportViewerModule } from 'devexpress-reporting-angular'; @Component({ selector: 'app-root', encapsulation: ViewEncapsulation.None, standalone: true, imports: [ CommonModule, RouterOutlet, DxReportViewerModule ], templateUrl: './app.component.html', styleUrls: [ "../../node_modules/devextreme/dist/css/dx.light.css", "../../node_modules/@devexpress/analytics-core/dist/css/dx-analytics.common.css", "../../node_modules/@devexpress/analytics-core/dist/css/dx-analytics.light.css", "../../node_modules/devexpress-reporting/dist/css/dx-webdocumentviewer.css" ] }) export class AppComponent { title = 'DXReportViewerSample'; reportUrl: string = 'TestReport'; hostUrl: string = 'https://localhost:5001/'; // Use this line if you use an ASP.NET MVC backend //invokeAction: string = "/WebDocumentViewer/Invoke"; // Use this line if you use an ASP.NET Core backend invokeAction: string = '/DXXRDV'; }
For more information on component options, review the following help topic: Document Viewer Client-Side Configuration in an Angular Application.
Replace the content of the angular-document-viewer\src\app\app.component.html file with the following code that inserts the dx-report-viewer Angular component:
<dx-report-viewer [reportUrl]="reportUrl" height="800px"> <dxrv-request-options [invokeAction]="invokeAction" [host]="hostUrl"></dxrv-request-options> </dx-report-viewer>
Run the server-side project.
Use the command below to build and serve the application. You should run this command each time you make changes to the source files:
npm start
Note
For recent Angular versions (16 and later) you may have to compile the Angular project with the
skipLibCheck
option set totrue
, and increase theinitial
budget setting in theangular.json
file up to 5mb to avoid compilation errors.Open the http://localhost:4200/ location in the browser to see the test report.
Troubleshooting
When you start the application, you may encounter the following problems:
Page is blank
The Document Viewer page is blank. The following error message is displayed at the bottom of the page: The page is blank because the Document Viewer failed to load the report. Consult the developer for assistance. Use development mode for detailed information. If the page does not display the Document Viewer, press F12 to invoke DevTools and inspect the console messages.
Check the following:
- The backend application is up and running.
- The backend application runs on the port specified in the host setting of the Document Viewer component in your Angular project. Review the following help topic for more information: Determine the Host URL.
- The Angular application’s URI satisfies the CORS policy specified in your backend application. Review the following help topic for more information: Configure the Application.
- The Angular component’s reportName value matches an existing report. For the backend application it means that the Reports folder contains the reportName.repx file or the ReportsFactory.Reports dictionary contains the reportName entry (if backend application originated from the DevExpress template).
- The version of the DevExpress scripts (npm packages) should match the version of the server-side libraries (NuGet packages). You can enable Development Mode to check for library version mismatch on every request to the server. For more information, review the following help topic: Server-Side Libraries Version.
Review the following help topic for more information: Troubleshooting.
For information on how to identify the cause of an issue, refer to the following topic: Reporting Application Diagnostics.
What’s Next
Now that you’ve created a basic Angular app with a Document Viewer, you can continue to extend your application with the following features:
- Client-Side Configuration
- Integrate the Document Viewer for Web into your Angular-based application.
- Specify Parameter Values
- Set parameter values in the Document Viewer
- Customize Toolbar and Tab Panel
- Customize available Document Viewer elements.