An AngularJS directive
ng-pdf
to display PDF files with PDFJS.
Overview [demo]
Integrate PDF files right into web pages.
###Features
- next / previous page
- zoom in / out
- rotate clockwise
- jump to a page number
- when scrolling, the pdf controls will get fixed position at the top
- define the view template
- define the path to pdf with scope variable
##Requirements
- AngularJS - get the latest angular.min.js
- PDFJS - build the files
pdf.js
andpdf.worker.js
-
install or copy over the file
dist/angular-pdf.min.js
ordist/angular-pdf.js
bower install angular-pdf
-
include the path to the direcitve file in
index.html
<script src="js/vendor/angular-pdf/dist/angular-pdf.js"></script>
-
include the directive as a dependency when defining the angular app:
var app = angular.module('App', ['pdf']);
-
include the directive with the 3 attribute:
template-url
: path to the partial under a controllercanvasid
:id
of thecanvas
tag you intend put in the next step 5scale
: initial scale of the pdf
<div class="wrapper" ng-controller="DocCtrl"> <ng-pdf template-url="/partials/viewer.html" canvasid="pdf-canvas" scale="1"></ng-pdf> </div>
-
include the
canvas
element to display the pdf in the template-url file<canvas id="pdf-canvas"></canvas>
-
include the path to the pdf file in the controller
app.controller('DocCtrl', function($scope) { $scope.pdfUrl = '/pdf/relativity.pdf'; });
###Options
-
Next / Previous page: Include the controls in the view file as defined in the attribute
template-url
<button ng-click="goPrevious()"><</span></button> <button ng-click="goNext()">></span></button>
-
Zoom in / out: Include the controls in the view file as defined in the attribute
template-url
<button ng-click="zoomIn()">+</span></button> <button ng-click="zoomOut()">-</span></button>
-
Rotate clockwise: Include the controls in the view file as defined in the attribute
template-url
and the initial classrotate0
<button ng-click="rotate()">90</span></button> ... <canvas id="pdf-canvas" class="rotate0"></canvas>
include the css styles:
.rotate0 {-webkit-transform: rotate(0deg); transform: rotate(0deg); } .rotate90 {-webkit-transform: rotate(90deg); transform: rotate(90deg); } .rotate180 {-webkit-transform: rotate(180deg); transform: rotate(180deg); } .rotate270 {-webkit-transform: rotate(270deg); transform: rotate(270deg); }
-
Jump to page number: Include the controls in the view file as defined in the attribute
template-url
<span>Page: </span><input type="text" min=1 ng-model="pageNum"><span> / {{pageCount}}</span>
-
Fixed pdf controls upon scrolling: Wrap the controls in the view file as defined in the attribute
template-url
with a tagnav
with anng-class
. Amend the scroll amount as required.<nav ng-class="getNavStyle(scroll)"> ... </nav>
Declare the
ng-class
logic in the controllerjs/controllers/docCtrl.js
$scope.getNavStyle = function(scroll) { if(scroll > 100) return 'pdf-controls fixed'; else return 'pdf-controls'; }
And include the relevant css styles as required:
.pdf-controls { width: 100%; display: block; background: #eee; padding: 1em;} .fixed { position: fixed; top: 0; left: calc(50% - 480px); z-index: 100; width: 100%; padding: 1em; background: rgba(238, 238, 238,.9); width: 960px; }
-
open the file
index.html
with a web server
##Similar projects
##Credit
PDF example used is Relativity: The Special and General Theory by Albert Einstein as kindly organized and made available free by Project Gutenberg.
(C) Sayanee Basu 2014, released under an MIT license