| 1 |
<!DOCTYPE html> |
| 2 |
<title>MuPDF Simplest Demo</title> |
| 3 |
<meta charset="utf-8"> |
| 4 |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> |
| 5 |
|
| 6 |
<style> |
| 7 |
body { background-color: gainsboro; text-align: center; } |
| 8 |
img { margin: 12px; box-shadow: 1px 1px 4px #0008; } |
| 9 |
</style> |
| 10 |
|
| 11 |
<script type="module"> |
| 12 |
|
| 13 |
"use strict" |
| 14 |
|
| 15 |
import * as mupdf from "../../dist/mupdf.js" |
| 16 |
|
| 17 |
window.openFile = async function openFile(file) { |
| 18 |
console.log("OPEN DOCUMENT", file.name) |
| 19 |
|
| 20 |
window.pageRoot.replaceChildren() |
| 21 |
|
| 22 |
let pdf = mupdf.Document.openDocument(await file.arrayBuffer(), file.name) |
| 23 |
|
| 24 |
document.title = pdf.getMetaData(mupdf.Document.META_INFO_TITLE) || file.name |
| 25 |
|
| 26 |
// Fire off page renders on a timer to avoid blocking the browser. |
| 27 |
let n = pdf.countPages() |
| 28 |
for (let i = 0; i < n; ++i) |
| 29 |
setTimeout(() => renderPage(pdf, i), 0) |
| 30 |
} |
| 31 |
|
| 32 |
function renderPage(pdf, i) { |
| 33 |
console.log("RENDER PAGE", i+1) |
| 34 |
let z = window.devicePixelRatio * 96 / 72 |
| 35 |
let png = pdf.loadPage(i).toPixmap([z,0,0,z,0,0], mupdf.ColorSpace.DeviceRGB).asPNG() |
| 36 |
let img = new Image() |
| 37 |
img.src = URL.createObjectURL(new Blob([png], { type: "image/png" })) |
| 38 |
img.onload = function () { |
| 39 |
img.style.width = img.width / window.devicePixelRatio + "px" |
| 40 |
window.pageRoot.appendChild(img) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
</script> |
| 45 |
|
| 46 |
<body> |
| 47 |
<input type="file" accept=".pdf,.xps,application/pdf" onchange="openFile(event.target.files[0])"> |
| 48 |
<div id="pageRoot"> |
| 49 |
</div> |
| 50 |
</body> |
| 51 |
|
| 52 |
</html> |