index.html 1.4 KB

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