• This repository has been archived on 25/Jun/2024
  • Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PDF.js viewer directive for AngularJS

PDF.js viewer Angular directive

Embed Mozilla's PDF.js viewer into your angular application, maintaining that look and feel of pdf's we all love. The directive embeds the full viewer, which allows you to scroll through the pdf.

Low maintenance

We're no longer using this library ourselves. We'll merge pull requests and create new releases, but not actively solve issues.

viewer-example

Installation

 npm install angular-pdfjs-viewer --save

Browser supperort

Chrome, FireFox, Safari and Egde.

Usage

Below you will find a basic example of how the directive can be used. Note that the order of the scripts matters. Stick to the order of dependencies as shown in the example below. Also note that images, translations and such are being loaded from the web folder.

View

<!DOCTYPE html>
<html ng-app="app" ng-controller="AppCtrl">
    <head>
        <title>Angular PDF.js demo</title>
        <script src="bower_components/pdf.js-viewer/pdf.js"></script>
        <link rel="stylesheet" href="bower_components/pdf.js-viewer/viewer.css">

        <script src="bower_components/angular/angular.js"></script>
        <script src="bower_components/angular-pdfjs-viewer/dist/angular-pdfjs-viewer.js"></script>
        <script src="app.js"></script>

        <style>
          html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
          .some-pdf-container { width: 100%; height: 100%; }
        </style>
    </head>
    <body>
        <div class="some-pdf-container">
            <pdfjs-viewer src="{{ pdf.src }}"></pdfjs-viewer>
        </div>
    </body>
</html>

Controller

angular.module('app', ['pdfjsViewer']);

angular.module('app').controller('AppCtrl', function($scope) {
    $scope.pdf = {
        src: 'example.pdf',
    };
});

Directive options

The <pdfjs-viewer> directive takes the following attributes.

src

The source should point to the URL of a publicly available pdf. Note that the src must be passed in as an interpolation string.

<pdfjs-viewer src="{{ pdf.src }}"></pdfjs-viewer>
$scope.pdf.src = "http://example.com/file.pdf";

data

In the case that you cannot simply use the URL of the pdf, you can pass in raw data as a Uint8Array object. The data attribute takes a scope variable as its argument.

<pdfjs-viewer data="pdf.data"></pdfjs-viewer>
$scope.data = null; // this is loaded async

$http.get("http://example.com/file.pdf", {
    responseType: 'arraybuffer'
}).then(function (response) {
    $scope.pdf.data = new Uint8Array(response.data);
});

scale

The scale attribute can be used to obtain the current scale (zoom level) of the PDF. The value will be stored in the variable specified. This is read only.

<pdfjs-viewer scale="scale"></pdfjs-viewer>

download, print, open

These buttons are by default all visible in the toolbar and can be hidden.

<pdfjs-viewer download="false" print="false" ... ></pdfjs-viewer>

on-init

The on-init function is called when PDF.JS is fully loaded.

<pdfjs-viewer on-init="onInit()"></pdfjs-viewer>
$scope.onInit = function () {
  // pdf.js is initialized
}

on-page-load

The on-page-load function is each time a page is loaded and will pass the page number. When the scale changes all pages are unloaded, so on-page-load will be called again for each page. If onPageLoad() returns false, the page will not be marked as loaded and onPageLoad will be called again for that page on the next (200ms) interval.

<pdfjs-viewer on-page-load="onPageLoad(page)"></pdfjs-viewer>
$scope.onPageLoad = function (page) {
    // page is loaded
};

Styling

The <pdfjs-viewer> directive automatically expands to the height and width of its first immediate parent, in the case of the example .some-pdf-container. If no parent container is given the html body will be used. Height and width are required to properly display the contents of the pdf.

Demo

You can test out a demo of this directive. You must run the node server first due to CORS. First make sure the dependencies are installed.

cd demo
npm install
bower install

Afterwards run the server like so.

node server.js

The server will be running on localhost:8080

Advanced configuration

By default the location of PDF.js assets are automatically determined. However if you place them on alternative locations they may not be found. If so, you can configure these locations.

You may disable using the Web Workers API. This is useful if you want to add pdf.worker.js to your concatenated JavaScript file. However this will have a negative effect on the runtime performance.

As part of your build process you might include all your library scripts within a concatenated Javascript file whilst excluding the pdf.worker.js file. Use setWorkerSrc() to point to the pdf.worker.js script.

angular.module('app').config(function(pdfjsViewerConfigProvider) {
  pdfjsViewerConfigProvider.setWorkerSrc("/assets/pdf.js-viewer/pdf.worker.js");
  pdfjsViewerConfigProvider.setCmapDir("/assets/pdf.js-viewer/cmaps");
  pdfjsViewerConfigProvider.setImageDir("/assets/pdf.js-viewer/images");
  
  pdfjsViewerConfigProvider.disableWorker();
  pdfjsViewerConfigProvider.setVerbosity("infos");  // "errors", "warnings" or "infos"
});

Note that a number of images used in the PDF.js viewer are loaded by the viewer.css. You can't configure these through JavaScript. Instead you need to compile the viewer.less file as

lessc --global-var='pdfjsImagePath=/assets/pdf.js-viewer/images' viewer.less viewer.css

More Repositories

1

angular-signature

HTML5 canvas based smooth signature drawing as angularJS directive (http://szimek.github.io/signature_pad/)
JavaScript
105
star
2

pdf.js-viewer

Compiled version of pdf.js viewer, modified to run embedded
JavaScript
45
star
3

lambda-pdf-thumbnail

A lambda function to make a pdf uploaded to s3 bucket
JavaScript
26
star
4

cloudwatch-logger-php

CloudWatch logger for PHP
PHP
9
star
5

authorizer

PHP library for authorization using public key cryptography
PHP
7
star
6

legalfling.io

LegalFling website
CSS
6
star
7

lambda-csv-parser

Parse CSV with AWS lambda
JavaScript
5
star
8

legalform-js

JavaScript library to create a form from a LegalForm definition
JavaScript
5
star
9

livecontracts.io

JavaScript
4
star
10

table-of-contents-json

Generate a table of contents represented in a JSON structure based on HTML
JavaScript
4
star
11

docker-apache-php

Docker images to run a php app in apache
Dockerfile
3
star
12

elastic-beanstalk-environment.js

Set Elastic Beanstalk environment variables
JavaScript
3
star
13

data-enricher

Enrich objects by processing special properties (PHP)
PHP
3
star
14

angular-onlyoffice

(ABANDONED) Angular directive for embedding ONLYOFFICE
JavaScript
2
star
15

lt-nest-starter

Starter project for NestJs based services
TypeScript
2
star
16

lambda-rollbar-deploy

Notify Rollbar after Elastic Beanstalk deployment
JavaScript
2
star
17

web3-server

Interact with Ethereum Smart Contracts over HTTP
TypeScript
2
star
18

waves-php

PHP library for working with Waves
1
star
19

php-code-quality

PHP coding standard and toolset
1
star
20

authz

Authorization and access control (PHP)
PHP
1
star
21

mongodb-session-handler

A PHP session handler that stores structured data in MongoDB
PHP
1
star
22

legalform-wp-plugin

WordPress plugin for displaying a LegalForm
PHP
1
star
23

registration-z24

Registration and Login pages
HTML
1
star
24

angular-template-injection

Simple Angular service to inject a template.
JavaScript
1
star
25

lambda-document-merger

Lambda function to merge documents
JavaScript
1
star
26

document-merger-service

Service to merge html documents
JavaScript
1
star
27

letsflow.io

LetsFlow website
CSS
1
star