Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Document Viewer Integration (npm or Yarn Package Managers)

  • 5 minutes to read

You can use the HTML5 Document Viewer in a JavaScript application. You should create two projects:

  • A server (backend) project that enables Cross-Origin Resource Sharing and retrieves a report from the storage
  • A client (frontend) part that includes all the necessary styles, scripts, and HTML-templates.

View Example

#Server (Backend) Part

#Use the DevExpress CLI Template

You can use DevExpress CLI Templates to create an ASP.NET Core back-end application. Begin with the steps below:

  1. Install DevExpress ASP.NET Core project templates from nuget.org:

    dotnet new install DevExpress.AspNetCore.ProjectTemplates
    
  2. Create a back-end Reporting application for a Document Viewer:

    console
    dotnet new dx.aspnetcore.reporting.backend -n ServerApp --add-designer false
    

    You can use the following parameters to see available command options: -? | -h | --help.

  3. 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 the UseRouting method and before any MVC-related code. Place the UseCors method before the UseMvc or UseEndpoints 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();
    
  4. To run the server-side application, run the following command:

    console
    cd ServerApp
    dotnet run
    

#Use Visual Studio Template

To create a back-end application from a Microsoft or DevExpress Template in Visual Studio, review the following help topics:

#Client (Frontend) Part

The following steps configure and host the client part:

  1. Create a new folder to store the client-side files (ClientSide in this example).

  2. Create a package.json file in the ClientSide folder with the following content:

    {
        "name": "web-document-viewer",
        "dependencies": {
            "devextreme-dist": "24.2-stable",
            "@devexpress/analytics-core": "24.2-stable",
            "devexpress-reporting": "24.2-stable"
        }
    }
    

    Note

    Frontend and backend applications should use the same version of DevExpress controls.

  3. Ensure that you have npm or Yarn package managers installed.

  4. Navigate to the client application root folder (ClientSide) and run the command in the command prompt:

    • If you use npm:

      console
      npm install
      
    • If you use yarn:

      console
      yarn install
      
  5. Create an index.html file. It is the View file in our model. Copy the following HTML code and insert it in this file:

    <!DOCTYPE html>
    <html xmlns="https://www.w3.org/1999/xhtml/">
    <head>
        <title></title>
    
        <script src="node_modules/jquery/dist/jquery.min.js"></script>
    
        <script src="node_modules/knockout/build/output/knockout-latest.js"></script>
    
        <!--Link DevExtreme resources-->
        <script src="node_modules/devextreme-dist/js/dx.all.js"></script>
        <link href="node_modules/devextreme-dist/css/dx.light.css" rel="stylesheet" />
    
    
        <!-- Link the Reporting resources -->
        <script src="node_modules/@devexpress/analytics-core/dist/js/dx-analytics-core.js"></script>
        <script src="node_modules/devexpress-reporting/dist/js/dx-webdocumentviewer.js"></script>
        <link href="node_modules/@devexpress/analytics-core/dist/css/dx-analytics.common.css" rel="stylesheet" />
        <link href="node_modules/@devexpress/analytics-core/dist/css/dx-analytics.light.css" rel="stylesheet" />
        <link href="node_modules/devexpress-reporting/dist/css/dx-webdocumentviewer.css" rel="stylesheet" />
    </head>
    <!-- ... -->
    </html>
    
  6. Create a example.js file to provide data to the View. The JavaScript code in this file creates a viewerOptions variable. Copy the following code and insert it in the example.js file:

    const host = 'http://localhost:5000/',
        reportUrl = "TestReport",
        viewerOptions = {
            reportUrl: reportUrl, // The URL of a report that the Document Viewer loads when the application starts.  
            requestOptions: { // Options for processing requests from the Document Viewer. 
                host: host, // URI of your backend project.
                // Use this line if you use an ASP.NET MVC backend
                // invokeAction = "/WebDocumentViewer/Invoke"
                // Uncomment this line if you use an ASP.NET Core backend
                invokeAction = "/DXXRDV"            
            }
        }
    
    new DevExpress.Reporting.Viewer.DxReportViewer(document.getElementById("viewer"), viewerOptions).render();    
    
  7. Modify the index.html file to specify the HTML template that uses the Document Viewer’s binding with the viewerOptions parameter. Add the following code to the body section:

    ...
    <body>
        <div style="width:100%; height: 1000px" id="viewer"></div>
        <script type="text/javascript" src="example.js"></script>
    </body>
    
  8. Host the client-side part on the web server. Start the Internet Information Services (IIS) Manager, right-click the Sites item in the Connections section, and select Add Website. In the invoked dialog, specify the site name, path to the client-side application root folder, and the website’s IP address and port.

    Host the Example with IIS Manager

  9. Run the backend project.

  10. Open the website created in step 8, in the browser. In this example, the address is http://localhost:1000.

#Troubleshooting

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:

Could not open report ‘TestReport’

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.
  • The application’s URI is compliant with the CORS policy specified in your back-end application.
  • The reportUrl setting value matches an existing report. For the backend application, ensure that either the Reports folder contains a reportUrl.repx file or the ReportsFactory.Reports dictionary contains the reportUrl entry (if the back-end application originated from the DevExpress template).
  • The version of DevExpress npm packages should match the version of NuGet packages. Enable Development Mode to check for a library version mismatch on every request to the server. For details, review the following help topic: Server-Side Libraries Version.

Refer to the following topic for more information: Troubleshooting.