Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/platform/wasm/examples/streams/fetch-stream.js @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 "use strict" | |
| 2 | |
| 3 import * as mupdf from "mupdf" | |
| 4 | |
| 5 class FetchStream { | |
| 6 constructor(url) { | |
| 7 fetch(url) | |
| 8 .then((response) => { | |
| 9 if (!response.ok) | |
| 10 throw new Error("HTTP " + response.status) | |
| 11 return response.arrayBuffer() | |
| 12 }) | |
| 13 .then((buffer) => { | |
| 14 this.buffer = new Uint8Array(buffer) | |
| 15 }) | |
| 16 } | |
| 17 fileSize() { | |
| 18 if (this.buffer) | |
| 19 return this.buffer.byteLength | |
| 20 return -1 // signal try later | |
| 21 } | |
| 22 read(memory, offset, size, position) { | |
| 23 if (this.buffer) { | |
| 24 size = Math.min(size, this.buffer.byteLength - position) | |
| 25 memory.set(this.buffer.subarray(position, position + size), offset) | |
| 26 return size | |
| 27 } | |
| 28 return -1 // signal try later | |
| 29 } | |
| 30 close() { | |
| 31 this.buffer = null | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 var stm = new mupdf.Stream(new FetchStream("https://mupdf.com/docs/mupdf_explored.pdf")) | |
| 36 function loop() { | |
| 37 try { | |
| 38 var doc = mupdf.Document.openDocument(stm, "application/pdf") | |
| 39 var n = doc.countPages() | |
| 40 console.log("Document has " + n + " pages!") | |
| 41 } catch (err) { | |
| 42 if (err === "TRYLATER") { | |
| 43 console.log("Waiting...") | |
| 44 setTimeout(loop, 1000) | |
| 45 } else { | |
| 46 throw err | |
| 47 } | |
| 48 } | |
| 49 } | |
| 50 loop() |
