Support for multi-size page PDFs

This commit is contained in:
notmasteryet 2011-06-26 09:15:33 -05:00
parent 8c2804145c
commit 8a044058f3
5 changed files with 45 additions and 20 deletions

View File

@ -26,41 +26,53 @@ var PDFViewer = {
scale: 1.0, scale: 1.0,
pageWidth: function() { pageWidth: function(page) {
return 816 * PDFViewer.scale; return page.mediaBox[2] * PDFViewer.scale;
}, },
pageHeight: function() { pageHeight: function(page) {
return 1056 * PDFViewer.scale; return page.mediaBox[3] * PDFViewer.scale;
}, },
lastPagesDrawn: [], lastPagesDrawn: [],
visiblePages: function() { visiblePages: function() {
var pageHeight = PDFViewer.pageHeight() + 20; // Add 20 for the margins. const pageBottomMargin = 20;
var windowTop = window.pageYOffset; var windowTop = window.pageYOffset;
var windowBottom = window.pageYOffset + window.innerHeight; var windowBottom = window.pageYOffset + window.innerHeight;
var pageStartIndex = Math.floor(windowTop / pageHeight);
var pageStopIndex = Math.ceil(windowBottom / pageHeight); var pageHeight, page;
var i, n = PDFViewer.numberOfPages, currentHeight = 0;
for (i = 1; i <= n; i++) {
var page = PDFViewer.pdf.getPage(i);
pageHeight = PDFViewer.pageHeight(page) + pageBottomMargin;
if (currentHeight + pageHeight > windowTop)
break;
currentHeight += pageHeight;
}
var pages = []; var pages = [];
for (; i <= n && currentHeight < windowBottom; i++) {
for (var i = pageStartIndex; i <= pageStopIndex; i++) { var page = PDFViewer.pdf.getPage(i);
pages.push(i + 1); pageHeight = PDFViewer.pageHeight(page) + pageBottomMargin;
currentHeight += pageHeight;
pages.push(i);
} }
return pages; return pages;
}, },
createPage: function(num) { createPage: function(num) {
var page = PDFViewer.pdf.getPage(num);
var anchor = document.createElement('a'); var anchor = document.createElement('a');
anchor.name = '' + num; anchor.name = '' + num;
var div = document.createElement('div'); var div = document.createElement('div');
div.id = 'pageContainer' + num; div.id = 'pageContainer' + num;
div.className = 'page'; div.className = 'page';
div.style.width = PDFViewer.pageWidth() + 'px'; div.style.width = PDFViewer.pageWidth(page) + 'px';
div.style.height = PDFViewer.pageHeight() + 'px'; div.style.height = PDFViewer.pageHeight(page) + 'px';
PDFViewer.element.appendChild(anchor); PDFViewer.element.appendChild(anchor);
PDFViewer.element.appendChild(div); PDFViewer.element.appendChild(div);
@ -91,8 +103,8 @@ var PDFViewer = {
// Canvas dimensions must be specified in CSS pixels. CSS pixels // Canvas dimensions must be specified in CSS pixels. CSS pixels
// are always 96 dpi. These dimensions are 8.5in x 11in at 96dpi. // are always 96 dpi. These dimensions are 8.5in x 11in at 96dpi.
canvas.width = PDFViewer.pageWidth(); canvas.width = PDFViewer.pageWidth(page);
canvas.height = PDFViewer.pageHeight(); canvas.height = PDFViewer.pageHeight(page);
div.appendChild(canvas); div.appendChild(canvas);
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d');

BIN
test/pdfs/sizes.pdf Normal file

Binary file not shown.

View File

@ -25,5 +25,10 @@
"link": true, "link": true,
"rounds": 1, "rounds": 1,
"type": "load" "type": "load"
},
{ "id": "sizes",
"file": "pdfs/sizes.pdf",
"rounds": 1,
"type": "eq"
} }
] ]

View File

@ -25,9 +25,6 @@ function load() {
manifestFile = params.manifestFile; manifestFile = params.manifestFile;
canvas = document.createElement("canvas"); canvas = document.createElement("canvas");
// 8.5x11in @ 100% ... XXX need something better here
canvas.width = 816;
canvas.height = 1056;
canvas.mozOpaque = true; canvas.mozOpaque = true;
stdout = document.getElementById("stdout"); stdout = document.getElementById("stdout");
@ -93,7 +90,6 @@ function nextPage() {
log(" loading page "+ currentTask.pageNum +"... "); log(" loading page "+ currentTask.pageNum +"... ");
var ctx = canvas.getContext("2d"); var ctx = canvas.getContext("2d");
clear(ctx);
var fonts = []; var fonts = [];
var gfx = null; var gfx = null;
@ -105,6 +101,15 @@ function nextPage() {
failure = 'compile: '+ e.toString(); failure = 'compile: '+ e.toString();
} }
try {
// using mediaBox for the canvas size
canvas.width = currentPage.mediaBox[2];
canvas.height = currentPage.mediaBox[3];
clear(ctx);
} catch(e) {
failure = 'page setup: '+ e.toString();
}
var fontLoaderTimer = null; var fontLoaderTimer = null;
function checkFontsLoaded() { function checkFontsLoaded() {
try { try {
@ -193,7 +198,6 @@ function sendTaskResult(snapshot) {
} }
function clear(ctx) { function clear(ctx) {
var ctx = canvas.getContext("2d");
ctx.save(); ctx.save();
ctx.fillStyle = "rgb(255, 255, 255)"; ctx.fillStyle = "rgb(255, 255, 255)";
ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillRect(0, 0, canvas.width, canvas.height);

View File

@ -60,6 +60,10 @@ function displayPage(num) {
var page = pdfDocument.getPage(pageNum = num); var page = pdfDocument.getPage(pageNum = num);
// scale canvas by 2
canvas.width = 2 * page.mediaBox[2];
canvas.hieght = 2 * page.mediaBox[3];
var t1 = Date.now(); var t1 = Date.now();
var ctx = canvas.getContext("2d"); var ctx = canvas.getContext("2d");
ctx.save(); ctx.save();