Example T848267
Visible to All Users

Reporting for Vue - Integrate a Web Document Viewer into a Vue App

This example incorporates the Web Document Viewer into a client-side app built with Vue. The example consists of two parts:

Quick Start

Server

In the ServerSideApp folder, run the following command:

Code
dotnet run

The server starts at http://localhost:5000. To debug the server, run the application in Visual Studio.

Client

In the vue-document-viewer folder, run the following commands:

Code
npm install npm run dev

Open your browser and navigate to the URL specified in the command output to see the result.The application displays the Web Document Viewer.

Web Document Viewer

Files to Review

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

vue-document-viewer/src/App.vue
Code
<template> <div ref="viewer" style="position: absolute; left: 0; right: 0; top: 0; bottom: 0"></div> </template> <script> import 'devexpress-reporting/dx-webdocumentviewer'; import {DxReportViewer} from 'devexpress-reporting/dx-webdocumentviewer'; import * as ko from 'knockout'; export default { name: "WebDocumentViewer", mounted() { const reportUrl = ko.observable("TestReport"); const viewerRef = this.$refs.viewer; const requestOptions = { host: " http://localhost:5000/", invokeAction: "DXXRDV" }; const viewer = new DxReportViewer(viewerRef, { reportUrl, requestOptions }); viewer.render(); }, beforeUnmount() { ko.cleanNode(this.$refs.viewer); } }; </script>
vue-document-viewer/src/main.js
JavaScript
import "devextreme/dist/css/dx.light.css"; import "@devexpress/analytics-core/dist/css/dx-analytics.common.css"; import "@devexpress/analytics-core/dist/css/dx-analytics.light.css"; import "devexpress-reporting/dist/css/dx-webdocumentviewer.css"; import { createApp } from 'vue' import App from './App.vue' createApp(App).mount('#app')
ServerSideApp/Controllers/HomeController.cs
C#
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace ServerSideApp.Controllers { public class HomeController : Controller { public IActionResult Index() { return View(); } public IActionResult Error() { ViewData["RequestId"] = Activity.Current?.Id ?? HttpContext.TraceIdentifier; return View(); } } }
ServerSideApp/Services/CustomReportStorageWebExtension.cs
C#
using System.Collections.Generic; using System.IO; using System.Linq; using DevExpress.XtraReports.UI; using ServerSideApp.PredefinedReports; using ServerSideApp.Data; namespace ServerSideApp.Services { public class CustomReportStorageWebExtension : DevExpress.XtraReports.Web.Extensions.ReportStorageWebExtension { protected ReportDbContext DbContext { get; set; } public CustomReportStorageWebExtension(ReportDbContext dbContext) { this.DbContext = dbContext; } public override bool CanSetData(string url) { // Determines whether a report with the specified URL can be saved. // Add custom logic that returns **false** for reports that should be read-only. // Return **true** if no valdation is required. // This method is called only for valid URLs (if the **IsValidUrl** method returns **true**). return true; } public override bool IsValidUrl(string url) { // Determines whether the URL passed to the current report storage is valid. // Implement your own logic to prohibit URLs that contain spaces or other specific characters. // Return **true** if no validation is required. return true; } public override byte[] GetData(string url) { // Uses a specified URL to return report layout data stored within a report storage medium. // This method is called if the **IsValidUrl** method returns **true**. // You can use the **GetData** method to process report parameters sent from the client // if the parameters are included in the report URL's query string. var reportData = DbContext.Reports.FirstOrDefault(x => x.Name == url); if(reportData != null) return reportData.LayoutData; if(ReportsFactory.Reports.ContainsKey(url)) { using var ms = new MemoryStream(); using XtraReport report = ReportsFactory.Reports[url](); report.SaveLayoutToXml(ms); return ms.ToArray(); } throw new DevExpress.XtraReports.Web.ClientControls.FaultException(string.Format("Could not find report '{0}'.", url)); } public override Dictionary<string, string> GetUrls() { // Returns a dictionary that contains the report names (URLs) and display names. // The Report Designer uses this method to populate the Open Report and Save Report dialogs. return DbContext.Reports .ToList() .Select(x => x.Name) .Union(ReportsFactory.Reports.Select(x => x.Key)) .ToDictionary<string, string>(x => x); } public override void SetData(XtraReport report, string url) { // Saves the specified report to the report storage with the specified name // (saves existing reports only). using var stream = new MemoryStream(); report.SaveLayoutToXml(stream); var reportData = DbContext.Reports.FirstOrDefault(x => x.Name == url); if(reportData == null) { DbContext.Reports.Add(new ReportItem { Name = url, LayoutData = stream.ToArray() }); } else { reportData.LayoutData = stream.ToArray(); } DbContext.SaveChanges(); } public override string SetNewData(XtraReport report, string defaultUrl) { // Allows you to validate and correct the specified name (URL). // This method also allows you to return the resulting name (URL), // and to save your report to a storage. The method is called only for new reports. SetData(report, defaultUrl); return defaultUrl; } } }
ServerSideApp/Program.cs
C#
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using DevExpress.AspNetCore; using DevExpress.AspNetCore.Reporting; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using DevExpress.XtraReports.Web.Extensions; using DevExpress.Security.Resources; using Microsoft.EntityFrameworkCore; using ServerSideApp.Services; using ServerSideApp.Data; var builder = WebApplication.CreateBuilder(args); AppDomain.CurrentDomain.SetData("DataDirectory", builder.Environment.ContentRootPath); builder.Services.AddDevExpressControls(); builder.Services.AddScoped<ReportStorageWebExtension, CustomReportStorageWebExtension>(); builder.Services.AddMvc(); builder.Services.ConfigureReportingServices(configurator => { if(builder.Environment.IsDevelopment()) configurator.UseDevelopmentMode(); configurator.ConfigureReportDesigner(designerConfigurator => { designerConfigurator.RegisterDataSourceWizardConnectionStringsProvider<CustomSqlDataSourceWizardConnectionStringsProvider>(); designerConfigurator.RegisterDataSourceWizardJsonConnectionStorage<CustomDataSourceWizardJsonDataConnectionStorage>(true); }); configurator.ConfigureWebDocumentViewer(viewerConfigurator => { viewerConfigurator.UseCachedReportSourceBuilder(); viewerConfigurator.RegisterJsonDataConnectionProviderFactory<CustomJsonDataConnectionProviderFactory>(); viewerConfigurator.RegisterConnectionProviderFactory<CustomSqlDataConnectionProviderFactory>(); }); }); builder.Services.AddDbContext<ReportDbContext>(options => options.UseSqlite(builder.Configuration.GetConnectionString("ReportsDataConnectionString"))); 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(); using(var scope = app.Services.CreateScope()) { var services = scope.ServiceProvider; services.GetService<ReportDbContext>().InitializeDatabase(); } var contentDirectoryAllowRule = DirectoryAccessRule.Allow(new DirectoryInfo(Path.Combine(app.Environment.ContentRootPath, "Content")).FullName); AccessSettings.ReportingSpecificResources.TrySetRules(contentDirectoryAllowRule, UrlAccessRule.Allow()); DevExpress.XtraReports.Configuration.Settings.Default.UserDesignerOptions.DataBindingMode = DevExpress.XtraReports.UI.DataBindingMode.Expressions; app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseCors("AllowCorsPolicy"); app.UseDevExpressControls(); System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12; app.MapControllerRoute( name: "default", pattern: "{controller}/{action=Index}/{id?}"); app.Run();

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.