Merge pull request #541 from arturadib/getpdf

Implemented getPdf(). Closes #516
This commit is contained in:
Chris Jones 2011-09-27 10:52:04 -07:00
commit a968187833
4 changed files with 42 additions and 59 deletions

View File

@ -7,30 +7,7 @@
'use strict';
//
// 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) {
getPdf('helloworld.pdf', function(data){
//
// Instantiate PDFDoc with PDF data
//

29
pdf.js
View File

@ -112,6 +112,35 @@ function stringToPDFString(str) {
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() {
function constructor(arrayBuffer, start, length, dict) {
this.bytes = new Uint8Array(arrayBuffer);

View File

@ -73,26 +73,16 @@ function nextTask() {
log('Loading file "' + task.file + '"\n');
var r = new XMLHttpRequest();
r.open('GET', task.file);
r.mozResponseType = r.responseType = 'arraybuffer';
r.onreadystatechange = function nextTaskOnreadystatechange() {
getPdf(task.file, function(data){
var failure;
if (r.readyState == 4) {
var data = r.mozResponseArrayBuffer || r.mozResponse ||
r.responseArrayBuffer || r.response;
try {
task.pdfDoc = new PDFDoc(data);
} catch (e) {
failure = 'load PDF doc : ' + e.toString();
}
task.pageNum = 1;
nextPage(task, failure);
try {
task.pdfDoc = new PDFDoc(data);
} catch (e) {
failure = 'load PDF doc : ' + e.toString();
}
};
r.send(null);
task.pageNum = 1;
nextPage(task, failure);
});
}
function isLastPage(task) {

View File

@ -107,23 +107,10 @@ var PDFView = {
document.title = url;
var xhr = new XMLHttpRequest();
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';
PDFView.load(data, scale);
}
};
xhr.send(null);
getPdf({url:url, progress:PDFView.progressLevel}, function(data) {
document.getElementById('loading').style.display = 'none';
PDFView.load(data, scale);
});
},
progressLevel: function(evt) {