Search results

Download in JavaScript PDF Viewer control

31 Mar 2023 / 2 minutes to read

The PDF Viewer supports downloading the loaded PDF file. You can enable/disable the download using the following code snippet.

Copied to clipboard
<!DOCTYPE html>
<html lang="en">

    <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="" 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>

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

Copied to clipboard
import { PdfViewer, Toolbar,Magnification,Navigation, Annotation, LinkAnnotation,ThumbnailView,BookmarkView,
TextSelection, TextSearch, Print} from '@syncfusion/ej2-pdfviewer';

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

let pdfviewer: PdfViewer = new PdfViewer({enableDownload: true});
pdfviewer.serviceUrl = '';
pdfviewer.load('PDF_Succinctly.pdf', null);

Alt text

You can invoke download action using following code snippet.,

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 = '';
pdfviewer.load('PDF_Succinctly.pdf', null);;

How to get the base64 string while downloading the PDF document

The downloadEnd event of the PDF viewer allows you to get the downloaded document as a base64 string.

The following code illustrates how to get the downloaded document as a base64 string.

Copied to clipboard
<button id="download">Download</button>
<button id="load">Load</button>
Copied to clipboard
document.getElementById('download').addEventListener('click', function () {
    //API to perform download action.;
    viewer.downloadEnd = function (args) {
        pdfstream = args.downloadDocument;
        //Print the document as a base64 string in the console window.

document.getElementById('load').addEventListener('click', function () {
    //Load the base64 string in the viewer.
    viewer.load(pdfstream, null);

See also