Getting started in EJ2 TypeScript PDF Viewer control
25 Mar 202510 minutes to read
This section briefly explains how to create PDF Viewer component and configure its available functionalities in TypeScript using the Essential JS 2 quickstart seed repository.
This application is integrated with the
webpack.config.js
configuration and uses the latest version of the webpack-cli. It requires nodev14.15.0
or higher. For more information about webpack and its features, refer to the webpack documentation.
Set up development environment
Open the command prompt from the required directory, and run the following command to clone the Syncfusion JavaScript (Essential JS 2) quickstart project from GitHub.
git clone https://github.com/SyncfusionExamples/ej2-quickstart-webpack- ej2-quickstart
After cloning the application in the ej2-quickstart
folder, run the following command line to navigate to the ej2-quickstart
folder.
cd ej2-quickstart
Add Syncfusion JavaScript packages
Syncfusion JavaScript (Essential JS 2) packages are available on the npmjs.com public registry. You can install all Syncfusion JavaScript (Essential JS 2) controls in a single @syncfusion/ej2 package or individual packages for each control.
The quickstart application is preconfigured with the dependent @syncfusion/ej2 package in the ~/package.json
file. Use the following command to install the dependent npm packages from the command prompt.
npm install
Import the Syncfusion CSS styles
Add the components CSS in the ~/src/styles/styles.css
file, as shown below:
@import '../../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-navigations/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-splitbuttons/styles/material.css';
@import "../../node_modules/@syncfusion/ej2-pdfviewer/styles/material.css";
@import "../../node_modules/@syncfusion/ej2-notifications/styles/material.css";
Adding PDF Viewer component
- Add the PDF Viewer component following code in the
app.ts
import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner} from '@syncfusion/ej2-pdfviewer';
PdfViewer.Inject(Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView, BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner);
let pdfviewer: PdfViewer = new PdfViewer();
pdfviewer.serviceUrl = 'https://services.syncfusion.com/js/production/api/pdfviewer';
pdfviewer.appendTo('#PdfViewer');
pdfviewer.load('https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf', null);
NOTE
From 23.1 version, it is must to call pdfviewer.dataBind(); before load function. Refer here for more details.
- Add an HTML div element to act as the PDF Viewer element
index.html
using the following code.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<meta name="description" content="Essential JS 2" />
<meta name="author" content="Syncfusion" />
<link rel="shortcut icon" href="resources/favicon.ico" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<!--Element which will render as PDF Viewer -->
<div id="PdfViewer"></div>
</body>
</html>
Run the application
The quickstart project is configured to compile and run the application in the browser. Use the following command to run the application.
npm start
Output will be displayed as follows.
import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView,
BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer} from '@syncfusion/ej2-pdfviewer';
PdfViewer.Inject( Toolbar, Magnification, Navigation, Annotation, LinkAnnotation, ThumbnailView,
BookmarkView, TextSelection, TextSearch, FormFields, FormDesigner, PageOrganizer );
let pdfviewer: PdfViewer = new PdfViewer();
pdfviewer.serviceUrl = 'https://services.syncfusion.com/js/production/api/pdfviewer';
pdfviewer.appendTo('#PdfViewer');
pdfviewer.load('https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf', null);
<!DOCTYPE html>
<html lang="en">
<head>
<title>EJ2 PDF Viewer</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript PDF Viewer Control" />
<meta name="author" content="Syncfusion" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-pdfviewer/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-lists/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-inputs/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-notifications/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='PdfViewer' style="height:500px;width:100%;"></div>
</div>
</body>
</html>
NOTE
We have provided the support to dynamically change the
serviceURL
. So, after changing theserviceURL
dynamically, you need invoke thepdfViewer.dataBind()
method to update theserviceURL
quickly. This will effectively change theserviceURL
dynamically. Ensure that this step is performed after version 23.1.36.
document.getElementById(‘load’).addEventListener(‘click’, function () {
pdfViewer.serviceUrl = “https://services.syncfusion.com/angular/production/api/pdfviewer”;
pdfViewer.documentPath = “https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf”;
pdfViewer.dataBind();
pdfViewer.load(pdfViewer.documentPath, null);
});
NOTE
The Web API hosted link https://services.syncfusion.com/js/production/api/pdfviewer utilized in the PDF viewer’s serviceUrl property is intended solely for demonstration and evaluation purposes. For production deployment, please host your own web service with your required server configurations. You can refer and reuse the GitHub Web Service example or Docker image for hosting your own web service and use for the serviceUrl property. We strongly recommend using the standalone mode.
Module injection
To create PDF Viewer with additional features, inject the required modules. The following modules are used to extend PdfViewer’s basic functionality:-
-
LinkAnnotation
:- Inject this module to use PDF Viewer link annotation. -
BookmarkView
:- Inject this module to use bookmark view of the PDF Viewer. -
Magnification
:- Inject this module to magnify the PDF Document. -
Navigation
:- Inject this module to use page navigation on PDF Document. -
TextSelection
:- Inject this module to use text selection with the PDF Document. -
ThumbnailView
:- Inject this module to use thumbnail view of the PDF Viewer -
Toolbar
:- Inject this module to enable the user interface for toolbar option in PDF Viewer. -
Print
:- Inject this module to use pdfviewer print feature. -
Annotation
:- Inject this module to use pdfviewer annotation feature. -
TextSearch
:- Inject this module to use pdfviewer text search feature. -
FormFields
:- Inject this module to use pdfviewer form fields feature. -
FormDesigner
:- Inject this module to use pdfviewer form designer feature.
These modules should be injected into the PDF Viewer using PdfViewer.Inject
method.
For PDF Viewer serviceUrl creation, follow the steps provided in the link
How to run the PDF Viewer web service
1.Download the sample from the Web service sample in GitHub link.
2.Navigate to the ASP.NET Core
folder and open it in the command prompt.
3.Use the below command to restore the required packages.
dotnet restore
4.Use the below command to run the web service.
dotnet run
5.You can see that the PDF Viewer server instance runs in the localhost with the port number localhost:5001
and navigate to the PDF Viewer Web control localhost:5001/pdfviewer
which returns the default get response method. We can bind the link to the serviceUrl property of PDF Viewer as below.
let pdfviewer: PdfViewer = new PdfViewer();
pdfviewer.serviceUrl = 'https://localhost:5001/pdfviewer';
pdfviewer.appendTo('#PdfViewer');
pdfviewer.load('PDF_Succinctly.pdf', null);
NOTE
When configuring the server-backed PDF viewer, it’s essential to understand that there is no need to include the pdfium.js and pdfium.wasm files. Unlike the standalone PDF viewer, which relies on these files for local rendering, the server-backed PDF viewer fetches and renders PDFs directly from the server. Consequently, you can exclude the copy command for deployment process, as they are not required to load and display PDFs in this context.
You can refer to our JavaScript PDF Viewer feature tour page for its groundbreaking feature representations. You can also explore our JavaScript PDF Viewer example to understand how to explains core features of PDF Viewer.
NOTE
For hosting the web service on the Linux platform, ensure to include the SkiaSharp.NativeAssets.Linux. Additionally, for AWS environments, utilize the following packages:
Amazon Web Services (AWS) | NuGet package name |
---|---|
AWS Lambda | SkiaSharp.NativeAssets.Linux |
AWS Elastic Beanstalk | SkiaSharp.NativeAssets.Linux.NoDependencies v2.88.6 |