pdf.js/src/api.js

142 lines
3.9 KiB
JavaScript
Raw Normal View History

2012-04-10 14:20:57 +09:00
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
(function pdfApiWrapper() {
function PdfPageWrapper(page) {
this.page = page;
}
PdfPageWrapper.prototype = {
get rotate() {
return this.page.rotate;
2012-04-10 14:20:57 +09:00
},
get stats() {
return this.page.stats;
},
get ref() {
return this.page.ref;
},
get view() {
return this.page.view;
},
getViewport: function(scale, rotate) {
2012-04-12 01:42:41 +09:00
if (arguments.length < 2)
rotate = this.rotate;
return new PDFJS.PageViewport(this.view, scale, rotate, 0, 0);
2012-04-10 14:20:57 +09:00
},
getAnnotations: function() {
var promise = new PDFJS.Promise();
var annotations = this.page.getAnnotations();
promise.resolve(annotations);
return promise;
},
render: function(renderContext) {
var promise = new PDFJS.Promise();
this.page.startRendering(renderContext.canvasContext,
renderContext.viewport,
2012-04-10 14:20:57 +09:00
function complete(error) {
if (error)
promise.reject(error);
else
promise.resolve();
},
renderContext.textLayer);
return promise;
},
getTextContent: function() {
var promise = new PDFJS.Promise();
var textContent = 'page text'; // not implemented
promise.resolve(textContent);
return promise;
},
getOperationList: function() {
var promise = new PDFJS.Promise();
var operationList = { // not implemented
dependencyFontsID: null,
operatorList: null
};
promise.resolve(operationList);
return promise;
}
};
function PdfDocumentWrapper(pdf) {
this.pdf = pdf;
}
PdfDocumentWrapper.prototype = {
get numPages() {
return this.pdf.numPages;
},
get fingerprint() {
2012-04-11 04:17:43 +09:00
return this.pdf.fingerprint;
2012-04-10 14:20:57 +09:00
},
getPage: function(number) {
var promise = new PDFJS.Promise();
var page = this.pdf.getPage(number);
promise.resolve(new PdfPageWrapper(page));
return promise;
},
getDestinations: function() {
var promise = new PDFJS.Promise();
var destinations = this.pdf.catalog.destinations;
promise.resolve(destinations);
return promise;
},
getOutline: function() {
var promise = new PDFJS.Promise();
var outline = this.pdf.catalog.documentOutline;
promise.resolve(outline);
return promise;
},
getMetadata: function() {
var promise = new PDFJS.Promise();
var info = this.pdf.info;
var metadata = this.pdf.catalog.metadata;
promise.resolve(info, metadata ? new PDFJS.Metadata(metadata) : null);
return promise;
}
};
PDFJS.getDocument = function getDocument(source) {
var promise = new PDFJS.Promise();
if (typeof source === 'string') {
// fetch url
PDFJS.getPdf(
{
url: source,
progress: function getPdfProgress(evt) {
if (evt.lengthComputable)
promise.progress({
loaded: evt.loaded,
total: evt.total
});
},
error: function getPdfError(e) {
promise.reject('Unexpected server response of ' +
e.target.status + '.');
}
},
function getPdfLoad(data) {
var pdf = null;
try {
pdf = new PDFJS.PDFDoc(data);
} catch (e) {
promise.reject('An error occurred while reading the PDF.', e);
}
if (pdf)
promise.resolve(new PdfDocumentWrapper(pdf));
});
} else {
// assuming the source is array, instantiating directly from it
var pdf = null;
try {
pdf = new PDFJS.PDFDoc(source);
} catch (e) {
promise.reject('An error occurred while reading the PDF.', e);
}
if (pdf)
promise.resolve(new PdfDocumentWrapper(pdf));
}
return promise;
};
})();