Search results

Getting started with JavaScript PDF Viewer control

24 Sep 2021 / 4 minutes to read

This section explains the steps to create a PDF Viewer within your application and demonstrate its basic usage.

Dependencies

The following are the list of minimum dependencies required to use the PDF Viewer:-

Copied to clipboard
|-- @syncfusion/ej2-pdfviewer
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-buttons
    |-- @syncfusion/ej2-dropdowns
    |-- @syncfusion/ej2-inputs
    |-- @syncfusion/ej2-lists
    |-- @syncfusion/ej2-navigations
    |-- @syncfusion/ej2-popups
    |-- @syncfusion/ej2-data
    |-- @syncfusion/ej2-notifications

Installation and configuration

To get started with PDF Viewer component, you can clone the Essential JS 2 quickstart project and install necessary packages by using the following commands.

Copied to clipboard
git clone https://github.com/syncfusion/ej2-quickstart.git quickstart
cd quickstart
npm install

Configuration system JS

Syncfusion PdfViewer packages should be mapped in the system.config.js configuration file.

Copied to clipboard
System.config({
    paths: {
        'syncfusion:': './node_modules/@syncfusion/',
    },
    map: {
        app: 'app',

        //Syncfusion packages mapping
        "@syncfusion/ej2-base": "syncfusion:ej2-base/dist/ej2-base.umd.min.js",
        "@syncfusion/ej2-buttons": "syncfusion:ej2-buttons/dist/ej2-buttons.umd.min.js",
        "@syncfusion/ej2-popups": "syncfusion:ej2-popups/dist/ej2-popups.umd.min.js",
        "@syncfusion/ej2-navigations": "syncfusion:ej2-navigations/dist/ej2-navigations.umd.min.js",
        "@syncfusion/ej2-inputs": "syncfusion:ej2-inputs/dist/ej2-inputs.umd.min.js",
        "@syncfusion/ej2-splitbuttons": "syncfusion:ej2-splitbuttons/dist/ej2-splitbuttons.umd.min.js",
        "@syncfusion/ej2-dropdowns": "syncfusion:ej2-dropdowns/dist/ej2-dropdowns.umd.min.js",
        "@syncfusion/ej2-lists": "syncfusion:ej2-lists/dist/ej2-lists.umd.min.js",
        "@syncfusion/ej2-data": "syncfusion:ej2-data/dist/ej2-data.umd.min.js",
        "@syncfusion/ej2-notifications": "syncfusion:ej2-notifications/dist/ej2-notifications.umd.min.js",
        "@syncfusion/ej2-pdfviewer": "syncfusion:ej2-pdfviewer/dist/ej2-pdfviewer.umd.min.js"

    },
    packages: {
        'app': { main: 'app', defaultExtension: 'js' }
    }
});

System.import('app');

Adding CSS reference

Combined CSS files are available in the Essential JS 2 package root folder. This can be referenced in your [src/styles/styles.css] using the following code.

Copied to clipboard
@import '../../node_modules/@syncfusion/ej2/material.css';

To learn about individual component CSS, please refer to Individual Component theme files section.

Adding PdfViewer component

You can add Essential JS 2 PDF Viewer component to the application. To get started, add the PdfViewer component in app.ts and index.html files using the following code.

Place the following code in the app.ts

Copied to clipboard
import { PdfViewer } from '@syncfusion/ej2-pdfviewer';

let pdfviewer: PdfViewer = new PdfViewer();
pdfviewer.serviceUrl = "http://localhost:62869/api/pdfviewer";
pdfviewer.enableAllModules();
pdfviewer.appendTo("#PdfViewer");

For PDF Viewer serviceUrl creation, follow the steps provided in the link

Now, add an HTML div element to act as the PDF Viewer element ‘index.html’ using the following code.

Copied to clipboard
<!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" />

    <!--style reference from app-->
    <link href="/styles/styles.css" rel="stylesheet" />

    <!--system js reference and configuration-->
    <script src="node_modules/systemjs/dist/system.src.js" type="text/javascript"></script>
    <script src="system.config.js" type="text/javascript"></script>
</head>

<body>
    <!--Element which will render as PdfViewer -->
    <div id="PdfViewer"></div>
</body>

</html>

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.

These modules should be injected into the PDF Viewer using PdfViewer.Inject method.

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.

Copied to clipboard
npm start

Output will be displayed as follows.

Source
Preview
index.ts
index.html
Copied to clipboard
import { PdfViewer, Toolbar, Magnification, Navigation, Annotation, LinkAnnotation,ThumbnailView,BookmarkView, TextSelection} from '@syncfusion/ej2-pdfviewer';

PdfViewer.Inject(Toolbar,Magnification,Navigation, Annotation, LinkAnnotation,ThumbnailView,BookmarkView, TextSelection);

let pdfviewer: PdfViewer = new PdfViewer();
pdfviewer.serviceUrl = 'https://ej2services.syncfusion.com/production/web-services/api/pdfviewer';
pdfviewer.appendTo('#PdfViewer');
pdfviewer.load('PDF_Succinctly.pdf', null);
Copied to clipboard
<!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="index.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />    
    <link href="//cdn.syncfusion.com/ej2/ej2-pdfviewer/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-dropdowns/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-lists/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet" />    
    <link href="//cdn.syncfusion.com/ej2/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/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>
</head>
<body>
    <div id='loader'>Loading....</div>
    <div id='container'>
        <div id='PdfViewer' style="height:500px;width:100%;"></div>        
    </div>
</body>
</html>

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.