officetopdf-js 0.0.1 → 0.0.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.
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # officetopdf-js
2
2
 
3
- Convert **DOCX**, **PPTX** and **XLSX** to PDF from JavaScript — in Node.js, Bun, Deno, Electron, and directly in the browser.
3
+ Convert **DOCX**, **PPTX** and **XLSX** to PDF from JavaScript — in Node.js, Bun, Deno and Electron.
4
4
 
5
- No LibreOffice. No Chromium. No Docker. No upload.
5
+ No LibreOffice. No Chromium. No Docker.
6
6
 
7
- Conversion is powered by [`office2pdf`](https://github.com/developer0hye/office2pdf), a pure-Rust converter built on [Typst](https://github.com/typst/typst). This package is the TypeScript distribution and API layer around it: the native binary on the server, the WebAssembly build in the browser, one API for both.
7
+ Conversion is powered by [`office2pdf`](https://github.com/developer0hye/office2pdf), a pure-Rust converter built on [Typst](https://github.com/typst/typst). This package is the TypeScript distribution and API layer around it.
8
8
 
9
9
  ## Install
10
10
 
@@ -22,7 +22,6 @@ Only the binary for your platform is downloaded, via optional dependencies (the
22
22
  | Linux x64 (musl) | `officetopdf-linux-x64-musl` |
23
23
  | Linux ARM64 | `officetopdf-linux-arm64` |
24
24
  | Windows x64 | `officetopdf-windows-x64` |
25
- | Browser (WebAssembly) | `officetopdf-wasm` |
26
25
 
27
26
  ## Usage
28
27
 
@@ -36,20 +35,6 @@ await Bun.write("report.pdf", pdf);
36
35
 
37
36
  Input may be a `Uint8Array`, `ArrayBuffer`, or `Blob`/`File`. Output is always a `Uint8Array`.
38
37
 
39
- ### Browser
40
-
41
- ```ts
42
- import { convert } from "officetopdf-js/browser";
43
-
44
- const pdf = await convert(file);
45
-
46
- const url = URL.createObjectURL(new Blob([pdf], { type: "application/pdf" }));
47
- ```
48
-
49
- The WebAssembly module lives in a separate `officetopdf-wasm` package and is loaded lazily on the first conversion, so it never lands in your initial bundle — and server-only installs never download it. It is roughly 47 MB uncompressed (about 20 MB over the wire): that is Typst plus its fallback fonts, and it is the price of converting without a server. Run conversions in a Web Worker so the download and the rendering stay off your main thread. Bundlers (Vite, Next.js, webpack, Rollup, esbuild) pick the browser build automatically through the `browser` export condition; `officetopdf-js/browser` and `officetopdf-js/node` select one explicitly.
50
-
51
- The `format` is inferred from `File.name` when present, and can always be passed explicitly.
52
-
53
38
  ### Shortcuts
54
39
 
55
40
  ```ts
@@ -85,8 +70,8 @@ await converter.dispose();
85
70
  | `paperSize` | `"a4" \| "letter" \| "legal"` | Paper size override. |
86
71
  | `landscape` | `boolean` | Force landscape orientation. |
87
72
  | `pdfA` | `boolean` | Produce PDF/A-2b archival output. |
88
- | `tagged` | `boolean` | Tag document structure so screen readers can navigate it. Node only. |
89
- | `pdfUa` | `boolean` | Produce PDF/UA-1 accessible output; implies `tagged`. Node only. |
73
+ | `tagged` | `boolean` | Tag document structure so screen readers can navigate it. |
74
+ | `pdfUa` | `boolean` | Produce PDF/UA-1 accessible output; implies `tagged`. |
90
75
 
91
76
  `pdfUa` enforces the full PDF/UA-1 standard and fails the conversion when the source cannot satisfy it — a document with no title raises `ConversionFailedError: PDF/UA-1 error: missing document title`. Use `tagged` when you want screen-reader structure without the strict compliance gate.
92
77
  | `sheets` | `string[]` | XLSX sheet filter; the only way to print a hidden sheet. |
@@ -99,10 +84,9 @@ await converter.dispose();
99
84
  | Option | Type | Description |
100
85
  |--------|------|-------------|
101
86
  | `fonts` | `(Uint8Array \| ArrayBuffer \| Blob)[]` | TTF/OTF/TTC faces to register. |
102
- | `fontPaths` | `string[]` | Additional font directories. Node only. |
103
- | `lastResortFontFamily` | `string` | Final fallback family. Browser only. |
104
- | `binaryPath` | `string` | Use a specific `office2pdf` binary. Node only. |
105
- | `timeoutMs` | `number` | Conversion timeout, default `120000`. Node only. |
87
+ | `fontPaths` | `string[]` | Additional font directories. |
88
+ | `binaryPath` | `string` | Use a specific `office2pdf` binary. |
89
+ | `timeoutMs` | `number` | Conversion timeout, default `120000`. |
106
90
 
107
91
  Set `OFFICE2PDF_BINARY` to point every conversion at a binary of your choosing.
108
92
 
@@ -117,7 +101,7 @@ const pdf = await convert(bytes, {
117
101
  });
118
102
  ```
119
103
 
120
- The browser build reports structured warnings from the WebAssembly module; the Node build forwards the CLI's diagnostics as `message`.
104
+ Warnings carry the converter's own diagnostics, such as a font falling back to a substitute.
121
105
 
122
106
  ## Errors
123
107
 
@@ -125,8 +109,7 @@ All failures are instances of `OfficeToPdfError`: `ConversionFailedError` (conve
125
109
 
126
110
  ## Notes
127
111
 
128
- - CJK text in the browser needs a font: register one through `fonts`, or build the WASM bundle with the upstream `wasm-cjk-font` feature (adds 3.3 MB).
129
- - Large documents block the thread they run on. In the browser, run conversions in a Web Worker.
112
+ - A conversion spawns the `office2pdf` binary; concurrent calls are independent processes, so a reusable converter does not serialise them.
130
113
  - Word's "Don't add space between paragraphs of the same style" (`w:contextualSpacing`) is currently ignored by the converter, so list-heavy DOCX files render with more vertical space than Word shows ([upstream #1684](https://github.com/developer0hye/office2pdf/issues/1684)).
131
114
 
132
115
  ## Development
@@ -138,15 +121,10 @@ bun run ci:check
138
121
 
139
122
  `bun test` runs the unit suite anywhere. The end-to-end suites under `tests/` skip themselves unless their runtime is present, so nothing fails on a bare checkout.
140
123
 
141
- Testing the Node path locally needs an `office2pdf` binary — point `OFFICE2PDF_BINARY` at one, or run `bun run binaries:fetch`. Testing the browser path needs Rust and `wasm-pack` (`brew install rustup wasm-pack`), then:
142
-
143
- ```bash
144
- bun run wasm:build
145
- cd npm/wasm && bun link && cd ../.. && bun link officetopdf-wasm
146
- ```
124
+ Testing locally needs an `office2pdf` binary — point `OFFICE2PDF_BINARY` at one, or run `bun run binaries:fetch`.
147
125
 
148
- `bun run wasm:build` compiles the upstream WebAssembly bundle into `npm/wasm/`, and `bun run binaries:fetch` stages the per-platform binary packages into `npm/`. Both track the latest upstream release by default; set `OFFICE2PDF_VERSION=v0.6.8` to pin one. Both run in CI on release.
126
+ `bun run binaries:fetch` stages the per-platform binary packages into `npm/`. It tracks the latest upstream release by default; set `OFFICE2PDF_VERSION=v0.6.8` to pin one. It runs in CI on release.
149
127
 
150
128
  ## License
151
129
 
152
- Apache-2.0, matching the bundled [`office2pdf`](https://github.com/developer0hye/office2pdf) binaries and WebAssembly module, © their respective authors.
130
+ Apache-2.0, matching the bundled [`office2pdf`](https://github.com/developer0hye/office2pdf) binaries, © their respective authors.
package/dist/index.js CHANGED
@@ -1,5 +1,3 @@
1
- import { createFormatShortcuts, resolveFormat, toBytes, ConversionFailedError, UnsupportedPlatformError } from './chunk-A2DPXSQU.js';
2
- export { ConversionFailedError, OfficeToPdfError, UnsupportedPlatformError } from './chunk-A2DPXSQU.js';
3
1
  import { execFile } from 'child_process';
4
2
  import { mkdtemp, writeFile, readFile, rm } from 'fs/promises';
5
3
  import { tmpdir } from 'os';
@@ -8,6 +6,32 @@ import { promisify } from 'util';
8
6
  import { createRequire } from 'module';
9
7
  import process from 'process';
10
8
 
9
+ // src/node.ts
10
+
11
+ // src/errors/OfficeToPdfError.ts
12
+ var OfficeToPdfError = class extends Error {
13
+ constructor(message, options) {
14
+ super(message, options);
15
+ this.name = "OfficeToPdfError";
16
+ }
17
+ };
18
+ var UnsupportedPlatformError = class extends OfficeToPdfError {
19
+ constructor(platform, arch, options) {
20
+ super(
21
+ `No office2pdf binary is available for ${platform}-${arch}. Install the matching platform package, or set OFFICE2PDF_BINARY to a local binary.`,
22
+ options
23
+ );
24
+ this.name = "UnsupportedPlatformError";
25
+ }
26
+ };
27
+ var ConversionFailedError = class extends OfficeToPdfError {
28
+ constructor(message, detail) {
29
+ super(detail ? `${message}: ${detail}` : message);
30
+ this.detail = detail;
31
+ this.name = "ConversionFailedError";
32
+ }
33
+ detail;
34
+ };
11
35
  var require2 = createRequire(import.meta.url);
12
36
  var PLATFORM_PACKAGES = {
13
37
  "darwin-arm64": "officetopdf-darwin-arm64",
@@ -66,6 +90,44 @@ function fontExtension(bytes) {
66
90
  return "ttf";
67
91
  }
68
92
 
93
+ // src/internal/convenience.ts
94
+ function shortcut(convert2, format) {
95
+ return (input, options) => convert2(input, { ...options, format });
96
+ }
97
+ function createFormatShortcuts(convert2) {
98
+ return {
99
+ docxToPdf: shortcut(convert2, "docx"),
100
+ pptxToPdf: shortcut(convert2, "pptx"),
101
+ xlsxToPdf: shortcut(convert2, "xlsx")
102
+ };
103
+ }
104
+
105
+ // src/internal/shared.ts
106
+ var FORMATS = ["docx", "pptx", "xlsx"];
107
+ async function toBytes(input) {
108
+ if (input instanceof Uint8Array) return input;
109
+ if (input instanceof ArrayBuffer) return new Uint8Array(input);
110
+ if (typeof Blob !== "undefined" && input instanceof Blob) {
111
+ return new Uint8Array(await input.arrayBuffer());
112
+ }
113
+ throw new OfficeToPdfError("Input must be a Uint8Array, ArrayBuffer, or Blob.");
114
+ }
115
+ function resolveFormat(input, options) {
116
+ if (options.format) {
117
+ if (!FORMATS.includes(options.format)) {
118
+ throw new OfficeToPdfError(`Unsupported format "${options.format}".`);
119
+ }
120
+ return options.format;
121
+ }
122
+ const name = input.name;
123
+ const extension = typeof name === "string" ? name.split(".").pop()?.toLowerCase() : void 0;
124
+ const inferred = FORMATS.find((format) => format === extension);
125
+ if (inferred) return inferred;
126
+ throw new OfficeToPdfError(
127
+ 'Unable to infer the input format. Pass { format: "docx" | "pptx" | "xlsx" }.'
128
+ );
129
+ }
130
+
69
131
  // src/node.ts
70
132
  var execFileAsync = promisify(execFile);
71
133
  var DEFAULT_TIMEOUT_MS = 12e4;
@@ -150,4 +212,4 @@ async function convert(input, options) {
150
212
  }
151
213
  var { docxToPdf, pptxToPdf, xlsxToPdf } = createFormatShortcuts(convert);
152
214
 
153
- export { convert, createConverter, docxToPdf, pptxToPdf, xlsxToPdf };
215
+ export { ConversionFailedError, OfficeToPdfError, UnsupportedPlatformError, convert, createConverter, docxToPdf, pptxToPdf, xlsxToPdf };
package/dist/types.d.ts CHANGED
@@ -26,7 +26,6 @@ export interface ConvertOptions {
26
26
  export interface ConverterOptions {
27
27
  fonts?: ConvertInput[];
28
28
  fontPaths?: string[];
29
- lastResortFontFamily?: string;
30
29
  binaryPath?: string;
31
30
  timeoutMs?: number;
32
31
  }
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;AAEpD,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,QAAQ,GAAG,OAAO,CAAC;AAElD,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG,WAAW,GAAG,IAAI,CAAC;AAE3D,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,IAAI,CAAC;CAClD;AAED,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,EAAE,YAAY,EAAE,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,SAAS;IACxB,OAAO,CAAC,KAAK,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC5E,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;AAEpD,MAAM,MAAM,SAAS,GAAG,IAAI,GAAG,QAAQ,GAAG,OAAO,CAAC;AAElD,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG,WAAW,GAAG,IAAI,CAAC;AAE3D,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,IAAI,CAAC;CAClD;AAED,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,EAAE,YAAY,EAAE,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,SAAS;IACxB,OAAO,CAAC,KAAK,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC5E,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "officetopdf-js",
3
- "version": "0.0.1",
4
- "description": "Convert DOCX, PPTX and XLSX to PDF in Node.js, Bun and the browser. No LibreOffice, no Chromium.",
3
+ "version": "0.0.2",
4
+ "description": "Convert DOCX, PPTX and XLSX to PDF in Node.js and Bun. No LibreOffice, no Chromium, no Docker.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",
@@ -18,26 +18,11 @@
18
18
  },
19
19
  "exports": {
20
20
  ".": {
21
- "browser": {
22
- "types": "./dist/browser.d.ts",
23
- "import": "./dist/browser.js",
24
- "require": "./dist/browser.cjs"
25
- },
26
21
  "types": "./dist/index.d.ts",
27
22
  "import": "./dist/index.js",
28
23
  "require": "./dist/index.cjs",
29
24
  "default": "./dist/index.js"
30
25
  },
31
- "./browser": {
32
- "types": "./dist/browser.d.ts",
33
- "import": "./dist/browser.js",
34
- "require": "./dist/browser.cjs"
35
- },
36
- "./node": {
37
- "types": "./dist/index.d.ts",
38
- "import": "./dist/index.js",
39
- "require": "./dist/index.cjs"
40
- },
41
26
  "./package.json": "./package.json"
42
27
  },
43
28
  "bugs": {
@@ -60,7 +45,6 @@
60
45
  "pdf",
61
46
  "office",
62
47
  "convert",
63
- "wasm",
64
48
  "typst"
65
49
  ],
66
50
  "license": "Apache-2.0",
@@ -75,18 +59,15 @@
75
59
  "ci:check": "bun run lint && bun run typecheck && bun run test && bun run build",
76
60
  "prepublishOnly": "bun run ci:check",
77
61
  "publish:npm": "npm publish --access public",
78
- "binaries:fetch": "bun run scripts/fetch-binaries.ts",
79
- "wasm:build": "bash scripts/build-wasm.sh"
62
+ "binaries:fetch": "bun run scripts/fetch-binaries.ts"
80
63
  },
81
64
  "sideEffects": false,
82
- "browser": "./dist/browser.js",
83
65
  "optionalDependencies": {
84
- "officetopdf-darwin-arm64": "0.0.1",
85
- "officetopdf-darwin-x64": "0.0.1",
86
- "officetopdf-linux-arm64": "0.0.1",
87
- "officetopdf-linux-x64": "0.0.1",
88
- "officetopdf-linux-x64-musl": "0.0.1",
89
- "officetopdf-wasm": "0.0.1",
90
- "officetopdf-windows-x64": "0.0.1"
66
+ "officetopdf-darwin-arm64": "0.0.2",
67
+ "officetopdf-darwin-x64": "0.0.2",
68
+ "officetopdf-linux-arm64": "0.0.2",
69
+ "officetopdf-linux-x64": "0.0.2",
70
+ "officetopdf-linux-x64-musl": "0.0.2",
71
+ "officetopdf-windows-x64": "0.0.2"
91
72
  }
92
73
  }
package/src/types.ts CHANGED
@@ -31,7 +31,6 @@ export interface ConvertOptions {
31
31
  export interface ConverterOptions {
32
32
  fonts?: ConvertInput[];
33
33
  fontPaths?: string[];
34
- lastResortFontFamily?: string;
35
34
  binaryPath?: string;
36
35
  timeoutMs?: number;
37
36
  }
package/dist/browser.cjs DELETED
@@ -1,155 +0,0 @@
1
- 'use strict';
2
-
3
- // src/errors/OfficeToPdfError.ts
4
- var OfficeToPdfError = class extends Error {
5
- constructor(message, options) {
6
- super(message, options);
7
- this.name = "OfficeToPdfError";
8
- }
9
- };
10
- var UnsupportedPlatformError = class extends OfficeToPdfError {
11
- constructor(platform, arch, options) {
12
- super(
13
- `No office2pdf binary is available for ${platform}-${arch}. Install the matching platform package, or set OFFICE2PDF_BINARY to a local binary.`,
14
- options
15
- );
16
- this.name = "UnsupportedPlatformError";
17
- }
18
- };
19
- var ConversionFailedError = class extends OfficeToPdfError {
20
- constructor(message, detail) {
21
- super(detail ? `${message}: ${detail}` : message);
22
- this.detail = detail;
23
- this.name = "ConversionFailedError";
24
- }
25
- detail;
26
- };
27
-
28
- // src/internal/convenience.ts
29
- function shortcut(convert2, format) {
30
- return (input, options) => convert2(input, { ...options, format });
31
- }
32
- function createFormatShortcuts(convert2) {
33
- return {
34
- docxToPdf: shortcut(convert2, "docx"),
35
- pptxToPdf: shortcut(convert2, "pptx"),
36
- xlsxToPdf: shortcut(convert2, "xlsx")
37
- };
38
- }
39
-
40
- // src/internal/shared.ts
41
- var FORMATS = ["docx", "pptx", "xlsx"];
42
- async function toBytes(input) {
43
- if (input instanceof Uint8Array) return input;
44
- if (input instanceof ArrayBuffer) return new Uint8Array(input);
45
- if (typeof Blob !== "undefined" && input instanceof Blob) {
46
- return new Uint8Array(await input.arrayBuffer());
47
- }
48
- throw new OfficeToPdfError("Input must be a Uint8Array, ArrayBuffer, or Blob.");
49
- }
50
- function resolveFormat(input, options) {
51
- if (options.format) {
52
- if (!FORMATS.includes(options.format)) {
53
- throw new OfficeToPdfError(`Unsupported format "${options.format}".`);
54
- }
55
- return options.format;
56
- }
57
- const name = input.name;
58
- const extension = typeof name === "string" ? name.split(".").pop()?.toLowerCase() : void 0;
59
- const inferred = FORMATS.find((format) => format === extension);
60
- if (inferred) return inferred;
61
- throw new OfficeToPdfError(
62
- 'Unable to infer the input format. Pass { format: "docx" | "pptx" | "xlsx" }.'
63
- );
64
- }
65
-
66
- // src/browser.ts
67
- var wasmModule;
68
- async function loadWasm() {
69
- wasmModule ??= import('officetopdf-wasm').then(async (module) => {
70
- await module.default();
71
- return module;
72
- }).catch((cause) => {
73
- wasmModule = void 0;
74
- throw new OfficeToPdfError(
75
- "Failed to load the office2pdf WebAssembly module. Install the optional `officetopdf-wasm` package to convert in the browser.",
76
- { cause }
77
- );
78
- });
79
- return wasmModule;
80
- }
81
- function toWarning(warning) {
82
- const { kind, format, message, from, to, element, detail, reason } = warning;
83
- warning.free();
84
- return { kind, format, message, from, to, element, detail, reason };
85
- }
86
- function reportWarnings(result, onWarning) {
87
- for (let index = 0; index < result.warningCount; index += 1) {
88
- const warning = result.warningAt(index);
89
- if (warning) onWarning?.(toWarning(warning));
90
- }
91
- }
92
- var BrowserConverter = class {
93
- constructor(options = {}) {
94
- this.options = options;
95
- }
96
- options;
97
- instance;
98
- async convert(input, options = {}) {
99
- const format = resolveFormat(input, options);
100
- const bytes = await toBytes(input);
101
- const converter = await this.getInstance();
102
- try {
103
- const result = converter.convertToPdf(bytes, format);
104
- try {
105
- reportWarnings(result, options.onWarning);
106
- return result.pdf;
107
- } finally {
108
- result.free();
109
- }
110
- } catch (cause) {
111
- throw new ConversionFailedError(
112
- `Converting ${format} to PDF failed`,
113
- cause instanceof Error ? cause.message : String(cause)
114
- );
115
- }
116
- }
117
- async dispose() {
118
- const instance = this.instance;
119
- this.instance = void 0;
120
- (await instance)?.free();
121
- }
122
- getInstance() {
123
- this.instance ??= (async () => {
124
- const { Office2PdfConverter } = await loadWasm();
125
- const converter = new Office2PdfConverter();
126
- for (const font of this.options.fonts ?? []) converter.registerFont(await toBytes(font));
127
- if (this.options.lastResortFontFamily) {
128
- converter.setLastResortFontFamily(this.options.lastResortFontFamily);
129
- }
130
- return converter;
131
- })();
132
- return this.instance;
133
- }
134
- };
135
- function createConverter(options) {
136
- return new BrowserConverter(options);
137
- }
138
- async function convert(input, options) {
139
- const converter = new BrowserConverter();
140
- try {
141
- return await converter.convert(input, options);
142
- } finally {
143
- await converter.dispose();
144
- }
145
- }
146
- var { docxToPdf, pptxToPdf, xlsxToPdf } = createFormatShortcuts(convert);
147
-
148
- exports.ConversionFailedError = ConversionFailedError;
149
- exports.OfficeToPdfError = OfficeToPdfError;
150
- exports.UnsupportedPlatformError = UnsupportedPlatformError;
151
- exports.convert = convert;
152
- exports.createConverter = createConverter;
153
- exports.docxToPdf = docxToPdf;
154
- exports.pptxToPdf = pptxToPdf;
155
- exports.xlsxToPdf = xlsxToPdf;
package/dist/browser.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import type { Converter, ConverterOptions, ConvertInput, ConvertOptions } from "./types.js";
2
- export declare function createConverter(options?: ConverterOptions): Converter;
3
- export declare function convert(input: ConvertInput, options?: ConvertOptions): Promise<Uint8Array>;
4
- export declare const docxToPdf: import("./internal/convenience.js").FormatShortcut, pptxToPdf: import("./internal/convenience.js").FormatShortcut, xlsxToPdf: import("./internal/convenience.js").FormatShortcut;
5
- export { ConversionFailedError, OfficeToPdfError, UnsupportedPlatformError, } from "./errors/OfficeToPdfError.js";
6
- export type { FormatOptions, FormatShortcut } from "./internal/convenience.js";
7
- export type { ConversionWarning, Converter, ConverterOptions, ConvertInput, ConvertOptions, OfficeFormat, PaperSize, } from "./types.js";
8
- //# sourceMappingURL=browser.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../src/browser.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAEV,SAAS,EACT,gBAAgB,EAChB,YAAY,EACZ,cAAc,EACf,MAAM,YAAY,CAAC;AAsFpB,wBAAgB,eAAe,CAAC,OAAO,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAErE;AAED,wBAAsB,OAAO,CAAC,KAAK,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,UAAU,CAAC,CAOhG;AAED,eAAO,MAAQ,SAAS,sDAAE,SAAS,sDAAE,SAAS,oDAAmC,CAAC;AAElF,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EAChB,wBAAwB,GACzB,MAAM,8BAA8B,CAAC;AACtC,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC/E,YAAY,EACV,iBAAiB,EACjB,SAAS,EACT,gBAAgB,EAChB,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,SAAS,GACV,MAAM,YAAY,CAAC"}
package/dist/browser.js DELETED
@@ -1,86 +0,0 @@
1
- import { createFormatShortcuts, resolveFormat, toBytes, ConversionFailedError, OfficeToPdfError } from './chunk-A2DPXSQU.js';
2
- export { ConversionFailedError, OfficeToPdfError, UnsupportedPlatformError } from './chunk-A2DPXSQU.js';
3
-
4
- // src/browser.ts
5
- var wasmModule;
6
- async function loadWasm() {
7
- wasmModule ??= import('officetopdf-wasm').then(async (module) => {
8
- await module.default();
9
- return module;
10
- }).catch((cause) => {
11
- wasmModule = void 0;
12
- throw new OfficeToPdfError(
13
- "Failed to load the office2pdf WebAssembly module. Install the optional `officetopdf-wasm` package to convert in the browser.",
14
- { cause }
15
- );
16
- });
17
- return wasmModule;
18
- }
19
- function toWarning(warning) {
20
- const { kind, format, message, from, to, element, detail, reason } = warning;
21
- warning.free();
22
- return { kind, format, message, from, to, element, detail, reason };
23
- }
24
- function reportWarnings(result, onWarning) {
25
- for (let index = 0; index < result.warningCount; index += 1) {
26
- const warning = result.warningAt(index);
27
- if (warning) onWarning?.(toWarning(warning));
28
- }
29
- }
30
- var BrowserConverter = class {
31
- constructor(options = {}) {
32
- this.options = options;
33
- }
34
- options;
35
- instance;
36
- async convert(input, options = {}) {
37
- const format = resolveFormat(input, options);
38
- const bytes = await toBytes(input);
39
- const converter = await this.getInstance();
40
- try {
41
- const result = converter.convertToPdf(bytes, format);
42
- try {
43
- reportWarnings(result, options.onWarning);
44
- return result.pdf;
45
- } finally {
46
- result.free();
47
- }
48
- } catch (cause) {
49
- throw new ConversionFailedError(
50
- `Converting ${format} to PDF failed`,
51
- cause instanceof Error ? cause.message : String(cause)
52
- );
53
- }
54
- }
55
- async dispose() {
56
- const instance = this.instance;
57
- this.instance = void 0;
58
- (await instance)?.free();
59
- }
60
- getInstance() {
61
- this.instance ??= (async () => {
62
- const { Office2PdfConverter } = await loadWasm();
63
- const converter = new Office2PdfConverter();
64
- for (const font of this.options.fonts ?? []) converter.registerFont(await toBytes(font));
65
- if (this.options.lastResortFontFamily) {
66
- converter.setLastResortFontFamily(this.options.lastResortFontFamily);
67
- }
68
- return converter;
69
- })();
70
- return this.instance;
71
- }
72
- };
73
- function createConverter(options) {
74
- return new BrowserConverter(options);
75
- }
76
- async function convert(input, options) {
77
- const converter = new BrowserConverter();
78
- try {
79
- return await converter.convert(input, options);
80
- } finally {
81
- await converter.dispose();
82
- }
83
- }
84
- var { docxToPdf, pptxToPdf, xlsxToPdf } = createFormatShortcuts(convert);
85
-
86
- export { convert, createConverter, docxToPdf, pptxToPdf, xlsxToPdf };
@@ -1,64 +0,0 @@
1
- // src/errors/OfficeToPdfError.ts
2
- var OfficeToPdfError = class extends Error {
3
- constructor(message, options) {
4
- super(message, options);
5
- this.name = "OfficeToPdfError";
6
- }
7
- };
8
- var UnsupportedPlatformError = class extends OfficeToPdfError {
9
- constructor(platform, arch, options) {
10
- super(
11
- `No office2pdf binary is available for ${platform}-${arch}. Install the matching platform package, or set OFFICE2PDF_BINARY to a local binary.`,
12
- options
13
- );
14
- this.name = "UnsupportedPlatformError";
15
- }
16
- };
17
- var ConversionFailedError = class extends OfficeToPdfError {
18
- constructor(message, detail) {
19
- super(detail ? `${message}: ${detail}` : message);
20
- this.detail = detail;
21
- this.name = "ConversionFailedError";
22
- }
23
- detail;
24
- };
25
-
26
- // src/internal/convenience.ts
27
- function shortcut(convert, format) {
28
- return (input, options) => convert(input, { ...options, format });
29
- }
30
- function createFormatShortcuts(convert) {
31
- return {
32
- docxToPdf: shortcut(convert, "docx"),
33
- pptxToPdf: shortcut(convert, "pptx"),
34
- xlsxToPdf: shortcut(convert, "xlsx")
35
- };
36
- }
37
-
38
- // src/internal/shared.ts
39
- var FORMATS = ["docx", "pptx", "xlsx"];
40
- async function toBytes(input) {
41
- if (input instanceof Uint8Array) return input;
42
- if (input instanceof ArrayBuffer) return new Uint8Array(input);
43
- if (typeof Blob !== "undefined" && input instanceof Blob) {
44
- return new Uint8Array(await input.arrayBuffer());
45
- }
46
- throw new OfficeToPdfError("Input must be a Uint8Array, ArrayBuffer, or Blob.");
47
- }
48
- function resolveFormat(input, options) {
49
- if (options.format) {
50
- if (!FORMATS.includes(options.format)) {
51
- throw new OfficeToPdfError(`Unsupported format "${options.format}".`);
52
- }
53
- return options.format;
54
- }
55
- const name = input.name;
56
- const extension = typeof name === "string" ? name.split(".").pop()?.toLowerCase() : void 0;
57
- const inferred = FORMATS.find((format) => format === extension);
58
- if (inferred) return inferred;
59
- throw new OfficeToPdfError(
60
- 'Unable to infer the input format. Pass { format: "docx" | "pptx" | "xlsx" }.'
61
- );
62
- }
63
-
64
- export { ConversionFailedError, OfficeToPdfError, UnsupportedPlatformError, createFormatShortcuts, resolveFormat, toBytes };
package/src/browser.ts DELETED
@@ -1,125 +0,0 @@
1
- import { ConversionFailedError, OfficeToPdfError } from "./errors/OfficeToPdfError.js";
2
- import { createFormatShortcuts } from "./internal/convenience.js";
3
- import { resolveFormat, toBytes } from "./internal/shared.js";
4
- import type {
5
- ConversionWarning,
6
- Converter,
7
- ConverterOptions,
8
- ConvertInput,
9
- ConvertOptions,
10
- } from "./types.js";
11
-
12
- type WasmModule = typeof import("officetopdf-wasm");
13
- type ConversionResult = InstanceType<WasmModule["ConversionResult"]>;
14
- type WasmWarning = InstanceType<WasmModule["ConversionWarning"]>;
15
- type WasmConverter = InstanceType<WasmModule["Office2PdfConverter"]>;
16
-
17
- let wasmModule: Promise<WasmModule> | undefined;
18
-
19
- async function loadWasm(): Promise<WasmModule> {
20
- wasmModule ??= import("officetopdf-wasm")
21
- .then(async (module) => {
22
- await module.default();
23
- return module;
24
- })
25
- .catch((cause) => {
26
- wasmModule = undefined;
27
- throw new OfficeToPdfError(
28
- "Failed to load the office2pdf WebAssembly module. Install the optional " +
29
- "`officetopdf-wasm` package to convert in the browser.",
30
- { cause },
31
- );
32
- });
33
- return wasmModule;
34
- }
35
-
36
- function toWarning(warning: WasmWarning): ConversionWarning {
37
- const { kind, format, message, from, to, element, detail, reason } = warning;
38
- warning.free();
39
- return { kind, format, message, from, to, element, detail, reason };
40
- }
41
-
42
- function reportWarnings(result: ConversionResult, onWarning: ConvertOptions["onWarning"]) {
43
- for (let index = 0; index < result.warningCount; index += 1) {
44
- const warning = result.warningAt(index);
45
- if (warning) onWarning?.(toWarning(warning));
46
- }
47
- }
48
-
49
- class BrowserConverter implements Converter {
50
- private instance?: Promise<WasmConverter>;
51
-
52
- constructor(private readonly options: ConverterOptions = {}) {}
53
-
54
- async convert(input: ConvertInput, options: ConvertOptions = {}): Promise<Uint8Array> {
55
- const format = resolveFormat(input, options);
56
- const bytes = await toBytes(input);
57
- const converter = await this.getInstance();
58
-
59
- try {
60
- const result = converter.convertToPdf(bytes, format);
61
- try {
62
- reportWarnings(result, options.onWarning);
63
- return result.pdf;
64
- } finally {
65
- result.free();
66
- }
67
- } catch (cause) {
68
- throw new ConversionFailedError(
69
- `Converting ${format} to PDF failed`,
70
- cause instanceof Error ? cause.message : String(cause),
71
- );
72
- }
73
- }
74
-
75
- async dispose(): Promise<void> {
76
- const instance = this.instance;
77
- this.instance = undefined;
78
- (await instance)?.free();
79
- }
80
-
81
- private getInstance(): Promise<WasmConverter> {
82
- this.instance ??= (async () => {
83
- const { Office2PdfConverter } = await loadWasm();
84
- const converter = new Office2PdfConverter();
85
-
86
- for (const font of this.options.fonts ?? []) converter.registerFont(await toBytes(font));
87
- if (this.options.lastResortFontFamily) {
88
- converter.setLastResortFontFamily(this.options.lastResortFontFamily);
89
- }
90
- return converter;
91
- })();
92
- return this.instance;
93
- }
94
- }
95
-
96
- export function createConverter(options?: ConverterOptions): Converter {
97
- return new BrowserConverter(options);
98
- }
99
-
100
- export async function convert(input: ConvertInput, options?: ConvertOptions): Promise<Uint8Array> {
101
- const converter = new BrowserConverter();
102
- try {
103
- return await converter.convert(input, options);
104
- } finally {
105
- await converter.dispose();
106
- }
107
- }
108
-
109
- export const { docxToPdf, pptxToPdf, xlsxToPdf } = createFormatShortcuts(convert);
110
-
111
- export {
112
- ConversionFailedError,
113
- OfficeToPdfError,
114
- UnsupportedPlatformError,
115
- } from "./errors/OfficeToPdfError.js";
116
- export type { FormatOptions, FormatShortcut } from "./internal/convenience.js";
117
- export type {
118
- ConversionWarning,
119
- Converter,
120
- ConverterOptions,
121
- ConvertInput,
122
- ConvertOptions,
123
- OfficeFormat,
124
- PaperSize,
125
- } from "./types.js";
@@ -1,37 +0,0 @@
1
- declare module "officetopdf-wasm" {
2
- export class ConversionWarning {
3
- readonly kind: string;
4
- readonly format: string;
5
- readonly message: string;
6
- readonly from: string | undefined;
7
- readonly to: string | undefined;
8
- readonly element: string | undefined;
9
- readonly detail: string | undefined;
10
- readonly reason: string | undefined;
11
- free(): void;
12
- }
13
-
14
- export class ConversionResult {
15
- readonly pdf: Uint8Array;
16
- readonly warningCount: number;
17
- warningAt(index: number): ConversionWarning | undefined;
18
- free(): void;
19
- }
20
-
21
- export class Office2PdfConverter {
22
- registerFont(data: Uint8Array): void;
23
- clearFonts(): void;
24
- setLastResortFontFamily(family: string): void;
25
- clearLastResortFontFamily(): void;
26
- convertToPdf(data: Uint8Array, format: string): ConversionResult;
27
- convertDocxToPdf(data: Uint8Array): ConversionResult;
28
- convertPptxToPdf(data: Uint8Array): ConversionResult;
29
- convertXlsxToPdf(data: Uint8Array): ConversionResult;
30
- free(): void;
31
- }
32
-
33
- export function convertToPdf(data: Uint8Array, format: string): Uint8Array;
34
- export function convertToPdfWithResult(data: Uint8Array, format: string): ConversionResult;
35
-
36
- export default function init(input?: unknown): Promise<unknown>;
37
- }