Merge pull request #541 from arturadib/getpdf
Implemented getPdf(). Closes #516
This commit is contained in:
commit
a968187833
@ -7,30 +7,7 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
//
|
getPdf('helloworld.pdf', function(data){
|
||||||
// Ajax GET request, for binary files
|
|
||||||
// (like jQuery's $.get(), but supports the binary type ArrayBuffer)
|
|
||||||
//
|
|
||||||
var ajaxGet = function(url, callback) {
|
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
xhr.open('GET', url);
|
|
||||||
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
|
|
||||||
xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
|
|
||||||
xhr.onreadystatechange = function() {
|
|
||||||
if (xhr.readyState === 4 && xhr.status === xhr.expected) {
|
|
||||||
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
|
|
||||||
xhr.responseArrayBuffer || xhr.response);
|
|
||||||
|
|
||||||
callback(data);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
xhr.send(null);
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
|
||||||
// This is where the fun happens
|
|
||||||
//
|
|
||||||
ajaxGet('helloworld.pdf', function ajaxGetHelloWorld(data) {
|
|
||||||
//
|
//
|
||||||
// Instantiate PDFDoc with PDF data
|
// Instantiate PDFDoc with PDF data
|
||||||
//
|
//
|
||||||
|
29
pdf.js
29
pdf.js
@ -112,6 +112,35 @@ function stringToPDFString(str) {
|
|||||||
return str2;
|
return str2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// getPdf()
|
||||||
|
// Convenience function to perform binary Ajax GET
|
||||||
|
// Usage: getPdf('http://...', callback)
|
||||||
|
// getPdf({url:String [,progress:Function]}, callback)
|
||||||
|
//
|
||||||
|
function getPdf(arg, callback) {
|
||||||
|
var params = arg;
|
||||||
|
if (typeof arg === 'string') {
|
||||||
|
params = {url: arg};
|
||||||
|
}
|
||||||
|
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.open('GET', params.url);
|
||||||
|
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
|
||||||
|
xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
|
||||||
|
xhr.onprogress = params.progress || undefined;
|
||||||
|
|
||||||
|
xhr.onreadystatechange = function() {
|
||||||
|
var data;
|
||||||
|
if (xhr.readyState === 4 && xhr.status === xhr.expected) {
|
||||||
|
data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
|
||||||
|
xhr.responseArrayBuffer || xhr.response);
|
||||||
|
callback(data);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhr.send(null);
|
||||||
|
}
|
||||||
|
|
||||||
var Stream = (function streamStream() {
|
var Stream = (function streamStream() {
|
||||||
function constructor(arrayBuffer, start, length, dict) {
|
function constructor(arrayBuffer, start, length, dict) {
|
||||||
this.bytes = new Uint8Array(arrayBuffer);
|
this.bytes = new Uint8Array(arrayBuffer);
|
||||||
|
@ -73,26 +73,16 @@ function nextTask() {
|
|||||||
|
|
||||||
log('Loading file "' + task.file + '"\n');
|
log('Loading file "' + task.file + '"\n');
|
||||||
|
|
||||||
var r = new XMLHttpRequest();
|
getPdf(task.file, function(data){
|
||||||
r.open('GET', task.file);
|
|
||||||
r.mozResponseType = r.responseType = 'arraybuffer';
|
|
||||||
r.onreadystatechange = function nextTaskOnreadystatechange() {
|
|
||||||
var failure;
|
var failure;
|
||||||
if (r.readyState == 4) {
|
|
||||||
var data = r.mozResponseArrayBuffer || r.mozResponse ||
|
|
||||||
r.responseArrayBuffer || r.response;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
task.pdfDoc = new PDFDoc(data);
|
task.pdfDoc = new PDFDoc(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
failure = 'load PDF doc : ' + e.toString();
|
failure = 'load PDF doc : ' + e.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
task.pageNum = 1;
|
task.pageNum = 1;
|
||||||
nextPage(task, failure);
|
nextPage(task, failure);
|
||||||
}
|
});
|
||||||
};
|
|
||||||
r.send(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isLastPage(task) {
|
function isLastPage(task) {
|
||||||
|
@ -107,23 +107,10 @@ var PDFView = {
|
|||||||
|
|
||||||
document.title = url;
|
document.title = url;
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
getPdf({url:url, progress:PDFView.progressLevel}, function(data) {
|
||||||
xhr.open('GET', url);
|
|
||||||
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
|
|
||||||
xhr.expected = (document.URL.indexOf('file:') === 0) ? 0 : 200;
|
|
||||||
xhr.onprogress = PDFView.progressLevel;
|
|
||||||
|
|
||||||
xhr.onreadystatechange = function() {
|
|
||||||
if (xhr.readyState === 4 && xhr.status === xhr.expected) {
|
|
||||||
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
|
|
||||||
xhr.responseArrayBuffer || xhr.response);
|
|
||||||
|
|
||||||
document.getElementById('loading').style.display = 'none';
|
document.getElementById('loading').style.display = 'none';
|
||||||
PDFView.load(data, scale);
|
PDFView.load(data, scale);
|
||||||
}
|
});
|
||||||
};
|
|
||||||
|
|
||||||
xhr.send(null);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
progressLevel: function(evt) {
|
progressLevel: function(evt) {
|
||||||
|
Loading…
Reference in New Issue
Block a user