@makolabs/ripple 3.9.1 → 3.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
type FitMode
|
|
14
14
|
} from '../../helper/pdf-safety.js';
|
|
15
15
|
import { MAX_PAGES } from '../../helper/resource-limits.js';
|
|
16
|
+
import { PdfBinaryDataFactory } from '../../helper/pdf-assets.js';
|
|
16
17
|
import Spinner from '../spinner/Spinner.svelte';
|
|
17
18
|
import Button from '../../button/Button.svelte';
|
|
18
19
|
import { Color, Size } from '../../variants.js';
|
|
@@ -193,6 +194,10 @@
|
|
|
193
194
|
|
|
194
195
|
const loadingTask = (currentLoadingTask = pdfjsLib.getDocument({
|
|
195
196
|
url,
|
|
197
|
+
// Serve pdfjs's image-codec wasm (JPEG2000/JPX, JBIG2) from
|
|
198
|
+
// bundler-emitted asset URLs. Without this, JPX-encoded pages render
|
|
199
|
+
// blank because pdfjs can't load `openjpeg.wasm`. See pdf-assets.ts.
|
|
200
|
+
BinaryDataFactory: PdfBinaryDataFactory,
|
|
196
201
|
...PDFJS_DOCUMENT_OPTIONS
|
|
197
202
|
}));
|
|
198
203
|
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pdfjs ships its image codecs (JPEG2000/JPX, JBIG2) and colour management as
|
|
3
|
+
* separate WebAssembly modules that live in `pdfjs-dist/wasm/`. Since pdfjs v4+
|
|
4
|
+
* the JPX decoder is one of these wasm modules, so a page whose images are
|
|
5
|
+
* JPEG2000-encoded (`/Filter /JPXDecode`) renders BLANK unless pdfjs can load
|
|
6
|
+
* `openjpeg.wasm` at runtime. pdfjs's own `wasmUrl` option expects a directory
|
|
7
|
+
* URL and then fetches `<wasmUrl>openjpeg.wasm` by its literal name — but a
|
|
8
|
+
* bundler (Vite/webpack) content-hashes emitted assets and can't emit a bare
|
|
9
|
+
* directory, so a plain `wasmUrl` can't be resolved reliably from a library.
|
|
10
|
+
*
|
|
11
|
+
* Instead we resolve each wasm file individually with the same bundler-agnostic
|
|
12
|
+
* `new URL('<file>', import.meta.url)` mechanism used for the pdf worker (every
|
|
13
|
+
* major bundler rewrites it to the emitted asset URL), fetch the bytes, and hand
|
|
14
|
+
* them straight to pdfjs through a custom `BinaryDataFactory`. This sidesteps
|
|
15
|
+
* both the directory requirement and asset hashing: we already hold the correct
|
|
16
|
+
* (hashed) URL for each file, so the exact emitted name never has to be guessed.
|
|
17
|
+
*
|
|
18
|
+
* pdfjs only reaches this factory for image-codec wasm (JPX/JBIG2); ICC colour
|
|
19
|
+
* (`qcms_bg.wasm`) is loaded by pdfjs via a separate synchronous path that still
|
|
20
|
+
* needs `wasmUrl` and is left unconfigured (non-fatal — ICC profiles are rare in
|
|
21
|
+
* scanned docs and pdfjs simply warns and skips them). Predefined CMaps and
|
|
22
|
+
* standard fonts are likewise unconfigured, matching the prior behaviour.
|
|
23
|
+
*/
|
|
24
|
+
/** Argument shape pdfjs passes to `BinaryDataFactory#fetch`. */
|
|
25
|
+
type FetchRequest = {
|
|
26
|
+
kind: string;
|
|
27
|
+
filename: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* A pdfjs `BinaryDataFactory` that serves the image-codec wasm modules from
|
|
31
|
+
* bundler-emitted asset URLs. Pass the class (not an instance) as the
|
|
32
|
+
* `BinaryDataFactory` option to `getDocument` — pdfjs instantiates it itself.
|
|
33
|
+
*
|
|
34
|
+
* For any request it doesn't recognise (predefined CMaps, standard fonts) it
|
|
35
|
+
* throws the same "API parameter not provided" error pdfjs's default factory
|
|
36
|
+
* would, so behaviour for those unconfigured assets is unchanged.
|
|
37
|
+
*/
|
|
38
|
+
export declare class PdfBinaryDataFactory {
|
|
39
|
+
constructor(_options?: unknown);
|
|
40
|
+
fetch({ kind, filename }: FetchRequest): Promise<Uint8Array>;
|
|
41
|
+
}
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pdfjs ships its image codecs (JPEG2000/JPX, JBIG2) and colour management as
|
|
3
|
+
* separate WebAssembly modules that live in `pdfjs-dist/wasm/`. Since pdfjs v4+
|
|
4
|
+
* the JPX decoder is one of these wasm modules, so a page whose images are
|
|
5
|
+
* JPEG2000-encoded (`/Filter /JPXDecode`) renders BLANK unless pdfjs can load
|
|
6
|
+
* `openjpeg.wasm` at runtime. pdfjs's own `wasmUrl` option expects a directory
|
|
7
|
+
* URL and then fetches `<wasmUrl>openjpeg.wasm` by its literal name — but a
|
|
8
|
+
* bundler (Vite/webpack) content-hashes emitted assets and can't emit a bare
|
|
9
|
+
* directory, so a plain `wasmUrl` can't be resolved reliably from a library.
|
|
10
|
+
*
|
|
11
|
+
* Instead we resolve each wasm file individually with the same bundler-agnostic
|
|
12
|
+
* `new URL('<file>', import.meta.url)` mechanism used for the pdf worker (every
|
|
13
|
+
* major bundler rewrites it to the emitted asset URL), fetch the bytes, and hand
|
|
14
|
+
* them straight to pdfjs through a custom `BinaryDataFactory`. This sidesteps
|
|
15
|
+
* both the directory requirement and asset hashing: we already hold the correct
|
|
16
|
+
* (hashed) URL for each file, so the exact emitted name never has to be guessed.
|
|
17
|
+
*
|
|
18
|
+
* pdfjs only reaches this factory for image-codec wasm (JPX/JBIG2); ICC colour
|
|
19
|
+
* (`qcms_bg.wasm`) is loaded by pdfjs via a separate synchronous path that still
|
|
20
|
+
* needs `wasmUrl` and is left unconfigured (non-fatal — ICC profiles are rare in
|
|
21
|
+
* scanned docs and pdfjs simply warns and skips them). Predefined CMaps and
|
|
22
|
+
* standard fonts are likewise unconfigured, matching the prior behaviour.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Bundler-resolved URLs for the wasm image codecs. The `new URL(<literal>,
|
|
26
|
+
* import.meta.url)` calls must use string literals so Vite/webpack can statically
|
|
27
|
+
* detect them and emit the assets. Keyed by the exact filename pdfjs requests.
|
|
28
|
+
*/
|
|
29
|
+
const WASM_ASSET_URLS = {
|
|
30
|
+
'openjpeg.wasm': new URL('pdfjs-dist/wasm/openjpeg.wasm', import.meta.url),
|
|
31
|
+
'jbig2.wasm': new URL('pdfjs-dist/wasm/jbig2.wasm', import.meta.url)
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* A pdfjs `BinaryDataFactory` that serves the image-codec wasm modules from
|
|
35
|
+
* bundler-emitted asset URLs. Pass the class (not an instance) as the
|
|
36
|
+
* `BinaryDataFactory` option to `getDocument` — pdfjs instantiates it itself.
|
|
37
|
+
*
|
|
38
|
+
* For any request it doesn't recognise (predefined CMaps, standard fonts) it
|
|
39
|
+
* throws the same "API parameter not provided" error pdfjs's default factory
|
|
40
|
+
* would, so behaviour for those unconfigured assets is unchanged.
|
|
41
|
+
*/
|
|
42
|
+
export class PdfBinaryDataFactory {
|
|
43
|
+
// pdfjs constructs this with { cMapUrl, standardFontDataUrl, wasmUrl }; we
|
|
44
|
+
// resolve wasm ourselves and ignore the (unset) URL options.
|
|
45
|
+
constructor(_options) { }
|
|
46
|
+
async fetch({ kind, filename }) {
|
|
47
|
+
const assetUrl = WASM_ASSET_URLS[filename];
|
|
48
|
+
if (!assetUrl) {
|
|
49
|
+
throw new Error(`Ensure that the \`${kind}\` API parameter is provided.`);
|
|
50
|
+
}
|
|
51
|
+
const response = await fetch(assetUrl);
|
|
52
|
+
if (!response.ok) {
|
|
53
|
+
throw new Error(`Unable to load pdf wasm asset "${filename}" (HTTP ${response.status})`);
|
|
54
|
+
}
|
|
55
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
56
|
+
}
|
|
57
|
+
}
|