pdf.js/test.html

108 lines
2.6 KiB
HTML

<html>
<head>
<title>Simple pdf.js page viewer</title>
<script type="text/javascript"
src="pdf.js"></script>
<style type"text/css">
body {
margin: 6px;
padding: 0px;
background-color: #c0bdb7;
}
#viewer {
margin: auto;
border: 1px solid black;
width: 8.5in;
height: 11in;
}
#pageNumber {
text-align: right;
}
</style>
<script type="text/javascript">
var canvas, numPages, pageDisplay, pageNum;
function load() {
canvas = document.getElementById("canvas");
canvas.mozOpaque = true;
pageDisplay = document.getElementById("pageNumber");
timeDisplay = document.getElementById("time");
req = new XMLHttpRequest();
req.open("GET", "uncompressed.tracemonkey-pldi-09.pdf");
req.mozResponseType = req.responseType = "arraybuffer";
req.expected = (document.URL.indexOf("file:") == 0) ? 0 : 200;
req.onreadystatechange = xhrstate;
req.send(null);
}
function xhrstate() {
if (req.readyState == 4 && req.status == req.expected) {
var data = req.mozResponseArrayBuffer ||
req.mozResponse ||
req.responseArrayBuffer ||
req.response;
pdf = new PDFDoc(new Stream(data));
numPages = pdf.numPages;
displayPage(1);
}
}
function displayPage(num) {
pageDisplay.value = num;
var t0 = Date.now();
var page = pdf.getPage(pageNum = num);
var t1 = Date.now();
var ctx = canvas.getContext("2d");
ctx.save();
ctx.fillStyle = "rgb(255, 255, 255)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore();
var gfx = new CanvasGraphics(ctx);
page.display(gfx);
var t2 = Date.now();
timeDisplay.innerHTML = (t1 - t0) + "/" + (t2 - t1) + " ms";
}
function nextPage() {
if (pageNum < numPages)
++pageNum;
displayPage(pageNum);
}
function prevPage() {
if (pageNum > 1)
--pageNum;
displayPage(pageNum);
}
function gotoPage(num) {
if (0 <= num && num <= numPages)
pageNum = num;
displayPage(pageNum);
}
</script>
</head>
<body onload="load();">
<div>
<button onclick="prevPage();">Previous</button>
<button onclick="nextPage();">Next</button>
<input type="text" id="pageNumber" onchange="gotoPage(this.value);"
value="1" size="5"></input>
Time to render: <span id="time"></span>
<div id="viewer">
<!-- Canvas dimensions must be specified in CSS pixels. CSS pixels
-- are always 96 dpi. These dimensions are 8.5x11in at 96dpi. -->
<canvas id="canvas" width="816" height="1056"></canvas>
</div>
</body>
</html>