[]
The easiest way to create a Svelte application is to use the SvelteKit tool. Run the following command from the command prompt or terminal:
npm create svelte@latest arjs-svelte-viewer-appIt will ask several questions and below the list of recommended answers
√ Which Svelte app template? » Skeleton project √ Add type checking with TypeScript? » Yes, using TypeScript syntax √ Add ESLint for code linting? » No √ Add Prettier for code formatting? » Yes √ Add Playwright for browser testing? » No √ Add Vitest for unit testing? » NoOnce the command run successfully, you could open the newly created arjs-svelte-viewer-app in your favorite IDE, such as Visual Studio Code.
The @grapecity/activereports npm package contains the pure ActiveReportsJS Report Viewer component that could be integrated into a Svelte application.
To install the latest version of this package, run the following command from the application's root folder.
npm install @grapecity/activereports@latest # or if you use yarn yarn add @grapecity/activereports@latest Svelte uses Vite.js under the hood to run applications in development mode and build them for production. To get ActiveReportsJS working with Vite.js, we need to adjust the Vite.js configuration. Create a new file called alias.js in the root of the application and add the following code:
import moment from "./node_modules/moment"; export const { fn, min, max, now, utc, unix, months, isDate, locale, invalid, duration, isMoment, weekdays, parseZone, localeData, isDuration, monthsShort, weekdaysMin, defineLocale, updateLocale, locales, weekdaysShort, normalizeUnits, relativeTimeRounding, relativeTimeThreshold, calendarFormat, ISO_8601 } = moment; export default moment;Then open the vite.config.js file in the root folder of the application and replace its content with the following:
import { sveltekit } from '@sveltejs/kit/vite'; import path from "path"; /** @type {import('vite').UserConfig} */ const config = { plugins: [sveltekit()], resolve: { alias: [ { find: /^moment$/, replacement: path.resolve(__dirname, "./alias.js"), }, { find: /^gc-dv$/, replacement: path.resolve( __dirname, "./node_modules/@grapecity/activereports/lib/node_modules/gc-dv.js" ), }, { find: /^\@grapecity\/ar-js-pagereport$/, replacement: path.resolve( __dirname, "./node_modules/@grapecity/activereports/lib/node_modules/@grapecity/ar-js-pagereport.js" ), }, { find: /^barcodejs$/, replacement: path.resolve( __dirname, "./node_modules/@grapecity/activereports/lib/node_modules/barcodejs.js" ), }, ], }, }; export default config;ActiveReportsJS uses the JSON format and the rdlx-json extension for report template files.
In the static folder of the application, create a new file called report.rdlx-json and insert the following content into it.
{ "Name": "Report", "Body": { "ReportItems": [ { "Type": "textbox", "Name": "TextBox1", "Value": "Hello, ActiveReportsJS Viewer", "Style": { "FontSize": "18pt", "PaddingLeft": "5pt", "PaddingTop": "5pt" }, "Width": "8.5in", "Height": "0.5in" } ] } }Replace the default content of the src\routes\+page.svelte file with the following code
<script lang="ts"> import "@grapecity/activereports/styles/ar-js-ui.css"; import "@grapecity/activereports/styles/ar-js-viewer.css"; import {Viewer} from "@grapecity/activereports/reportviewer"; import {PdfExport, HtmlExport, TabularDataExport} from "@grapecity/activereports"; import { onMount } from 'svelte'; onMount(() => { let viewer = new Viewer("#viewer-host"); viewer.open("report.rdlx-json"); }); </script> <div id="viewer-host"></div> <style> #viewer-host { width: 100%; height: 100vh; } </style>By default, SvelteKit will render any page first on the server and send it to the client as HTML. But ActiveReportsJS can only operate on the client-side. Hence, we need to disable the server-side rendering for the page that contains the Report Viewer. We can do that by adding the +page.js file with the following content alongside the +page.svelte
export const prerender = false; export const ssr = false;You can run the application by using the yarn run dev or npm run dev commands. By default, the project runs at http://localhost:5173/.
If you browse this URL, the ActiveReportsJS Report Viewer will appear on the page. The viewer will display the report that shows the Hello, ActiveReportsJS Viewer text. You can test the basic functionality by using the Report Viewer User Interface. The Report Viewer component exposes a rich API supplied with TypeScript declarations, so you can easily use it within a Svelte application.