officetopdf-js 0.0.1
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/LICENSE +216 -0
- package/README.md +152 -0
- package/dist/browser.cjs +155 -0
- package/dist/browser.d.ts +8 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +86 -0
- package/dist/chunk-A2DPXSQU.js +64 -0
- package/dist/errors/OfficeToPdfError.d.ts +11 -0
- package/dist/errors/OfficeToPdfError.d.ts.map +1 -0
- package/dist/index.cjs +229 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +153 -0
- package/dist/internal/binary.d.ts +2 -0
- package/dist/internal/binary.d.ts.map +1 -0
- package/dist/internal/cli-args.d.ts +4 -0
- package/dist/internal/cli-args.d.ts.map +1 -0
- package/dist/internal/convenience.d.ts +11 -0
- package/dist/internal/convenience.d.ts.map +1 -0
- package/dist/internal/shared.d.ts +4 -0
- package/dist/internal/shared.d.ts.map +1 -0
- package/dist/node.d.ts +8 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/types.d.ts +37 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +92 -0
- package/src/__tests__/binary.test.ts +53 -0
- package/src/__tests__/cli-args.test.ts +87 -0
- package/src/__tests__/convenience.test.ts +36 -0
- package/src/__tests__/shared.test.ts +43 -0
- package/src/browser.ts +125 -0
- package/src/errors/OfficeToPdfError.ts +27 -0
- package/src/index.ts +1 -0
- package/src/internal/binary.ts +49 -0
- package/src/internal/cli-args.ts +28 -0
- package/src/internal/convenience.ts +19 -0
- package/src/internal/shared.ts +31 -0
- package/src/node.ts +124 -0
- package/src/types/officetopdf-wasm.d.ts +37 -0
- package/src/types.ts +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
203
|
+
|
|
204
|
+
Copyright 2026 Nilesh Darji
|
|
205
|
+
|
|
206
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
207
|
+
you may not use this file except in compliance with the License.
|
|
208
|
+
You may obtain a copy of the License at
|
|
209
|
+
|
|
210
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
211
|
+
|
|
212
|
+
Unless required by applicable law or agreed to in writing, software
|
|
213
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
214
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
215
|
+
See the License for the specific language governing permissions and
|
|
216
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# officetopdf-js
|
|
2
|
+
|
|
3
|
+
Convert **DOCX**, **PPTX** and **XLSX** to PDF from JavaScript — in Node.js, Bun, Deno, Electron, and directly in the browser.
|
|
4
|
+
|
|
5
|
+
No LibreOffice. No Chromium. No Docker. No upload.
|
|
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.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bun add officetopdf-js
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Only the binary for your platform is downloaded, via optional dependencies (the same pattern as `esbuild` and `swc`).
|
|
16
|
+
|
|
17
|
+
| Platform | Package |
|
|
18
|
+
|----------|---------|
|
|
19
|
+
| macOS Apple Silicon | `officetopdf-darwin-arm64` |
|
|
20
|
+
| macOS Intel | `officetopdf-darwin-x64` |
|
|
21
|
+
| Linux x64 (glibc) | `officetopdf-linux-x64` |
|
|
22
|
+
| Linux x64 (musl) | `officetopdf-linux-x64-musl` |
|
|
23
|
+
| Linux ARM64 | `officetopdf-linux-arm64` |
|
|
24
|
+
| Windows x64 | `officetopdf-windows-x64` |
|
|
25
|
+
| Browser (WebAssembly) | `officetopdf-wasm` |
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { convert } from "officetopdf-js";
|
|
31
|
+
|
|
32
|
+
const pdf = await convert(await Bun.file("report.docx").bytes(), { format: "docx" });
|
|
33
|
+
|
|
34
|
+
await Bun.write("report.pdf", pdf);
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Input may be a `Uint8Array`, `ArrayBuffer`, or `Blob`/`File`. Output is always a `Uint8Array`.
|
|
38
|
+
|
|
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
|
+
### Shortcuts
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { docxToPdf, pptxToPdf, xlsxToPdf } from "officetopdf-js";
|
|
57
|
+
|
|
58
|
+
const pdf = await docxToPdf(bytes);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Reusable converter
|
|
62
|
+
|
|
63
|
+
Fonts are parsed once per converter instead of once per conversion.
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { createConverter } from "officetopdf-js";
|
|
67
|
+
|
|
68
|
+
const converter = createConverter({
|
|
69
|
+
fonts: [await Bun.file("fonts/Pretendard.ttf").bytes()],
|
|
70
|
+
fontPaths: ["/usr/share/fonts"],
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const pdf = await converter.convert(bytes, { format: "docx", pdfA: true });
|
|
74
|
+
|
|
75
|
+
await converter.dispose();
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Options
|
|
79
|
+
|
|
80
|
+
### `ConvertOptions`
|
|
81
|
+
|
|
82
|
+
| Option | Type | Description |
|
|
83
|
+
|--------|------|-------------|
|
|
84
|
+
| `format` | `"docx" \| "pptx" \| "xlsx"` | Input format. Inferred from `File.name` when omitted. |
|
|
85
|
+
| `paperSize` | `"a4" \| "letter" \| "legal"` | Paper size override. |
|
|
86
|
+
| `landscape` | `boolean` | Force landscape orientation. |
|
|
87
|
+
| `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. |
|
|
90
|
+
|
|
91
|
+
`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
|
+
| `sheets` | `string[]` | XLSX sheet filter; the only way to print a hidden sheet. |
|
|
93
|
+
| `slides` | `string` | PPTX slide range, e.g. `"1-5"`. |
|
|
94
|
+
| `signal` | `AbortSignal` | Cancel an in-flight conversion. |
|
|
95
|
+
| `onWarning` | `(warning: ConversionWarning) => void` | Non-fatal diagnostics, e.g. a missing font falling back. |
|
|
96
|
+
|
|
97
|
+
### `ConverterOptions`
|
|
98
|
+
|
|
99
|
+
| Option | Type | Description |
|
|
100
|
+
|--------|------|-------------|
|
|
101
|
+
| `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. |
|
|
106
|
+
|
|
107
|
+
Set `OFFICE2PDF_BINARY` to point every conversion at a binary of your choosing.
|
|
108
|
+
|
|
109
|
+
## Warnings
|
|
110
|
+
|
|
111
|
+
A conversion can succeed while silently substituting a missing font. Opt in to hear about it:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
const pdf = await convert(bytes, {
|
|
115
|
+
format: "docx",
|
|
116
|
+
onWarning: ({ kind, from, to, message }) => console.warn(kind, from, to, message),
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
The browser build reports structured warnings from the WebAssembly module; the Node build forwards the CLI's diagnostics as `message`.
|
|
121
|
+
|
|
122
|
+
## Errors
|
|
123
|
+
|
|
124
|
+
All failures are instances of `OfficeToPdfError`: `ConversionFailedError` (conversion or CLI failure, with upstream stderr in `detail`) and `UnsupportedPlatformError` (no binary for the current platform).
|
|
125
|
+
|
|
126
|
+
## Notes
|
|
127
|
+
|
|
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.
|
|
130
|
+
- 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
|
+
|
|
132
|
+
## Development
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
bun install
|
|
136
|
+
bun run ci:check
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`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
|
+
|
|
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
|
+
```
|
|
147
|
+
|
|
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.
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
Apache-2.0, matching the bundled [`office2pdf`](https://github.com/developer0hye/office2pdf) binaries and WebAssembly module, © their respective authors.
|
package/dist/browser.cjs
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
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;
|
|
@@ -0,0 +1,8 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
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 };
|