qpdf-compress 0.1.0
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 +176 -0
- package/README.md +168 -0
- package/binding.gyp +111 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +39 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/lib/index.ts +60 -0
- package/lib/types.ts +15 -0
- package/package.json +91 -0
- package/scripts/bundle-lib.mjs +35 -0
- package/scripts/download-qpdf.mjs +205 -0
- package/scripts/install.mjs +112 -0
- package/src/qpdf_addon.cc +610 -0
- package/src/stb_image_write.h +1724 -0
- package/src/stb_impl.cc +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding any notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# qpdf-compress
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/qpdf-compress)
|
|
4
|
+
[](https://nodejs.org)
|
|
5
|
+
[](https://github.com/xonaman/nodejs-qpdf-compress/blob/main/LICENSE)
|
|
6
|
+
[]()
|
|
7
|
+
|
|
8
|
+
Native PDF compression for Node.js — powered by [QPDF](https://qpdf.sourceforge.io/), the industry-standard PDF transformation library. Built as a C++ addon with N-API for ABI stability across Node.js versions.
|
|
9
|
+
|
|
10
|
+
> Designed for server-side workloads. Non-blocking, fast, and production-ready.
|
|
11
|
+
|
|
12
|
+
## 🚀 Quick Start
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { compress } from 'qpdf-compress';
|
|
16
|
+
|
|
17
|
+
// lossless — optimize without touching image quality
|
|
18
|
+
const optimized = await compress(pdfBuffer, { mode: 'lossless' });
|
|
19
|
+
|
|
20
|
+
// lossy — recompress images as JPEG for maximum savings
|
|
21
|
+
const smaller = await compress(pdfBuffer, { mode: 'lossy', quality: 50 });
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 💡 Why qpdf-compress?
|
|
25
|
+
|
|
26
|
+
**⚡ Performance**
|
|
27
|
+
|
|
28
|
+
- Native C++ — no WASM overhead, no shell-out to CLI tools
|
|
29
|
+
- Non-blocking — all operations run off the main thread via N-API AsyncWorker
|
|
30
|
+
- Multi-pass optimization — image dedup, JPEG Huffman optimization, Flate level 9
|
|
31
|
+
|
|
32
|
+
**🛠️ Developer experience**
|
|
33
|
+
|
|
34
|
+
- Single function API — `compress()` handles everything including repair
|
|
35
|
+
- Prebuilt binaries — no compile step on supported platforms
|
|
36
|
+
- Full TypeScript support with types included
|
|
37
|
+
- File path input/output to avoid unnecessary memory copies
|
|
38
|
+
|
|
39
|
+
**🔒 Reliability**
|
|
40
|
+
|
|
41
|
+
- Built on QPDF — the most widely used PDF structural transformation library
|
|
42
|
+
- ABI-stable via N-API — works across Node.js 18–24 without recompilation
|
|
43
|
+
- Automatic repair — damaged PDFs are reconstructed during compression
|
|
44
|
+
- Apache-2.0 licensed — no AGPL/copyleft concerns
|
|
45
|
+
|
|
46
|
+
### 🎯 Use cases
|
|
47
|
+
|
|
48
|
+
- 📉 Reduce PDF storage costs in S3/cloud storage
|
|
49
|
+
- 📤 Shrink PDFs before email delivery or API responses
|
|
50
|
+
- 🔧 Repair damaged PDFs from third-party sources
|
|
51
|
+
- ⚙️ Build server-side document processing pipelines
|
|
52
|
+
|
|
53
|
+
### 📊 How it compares
|
|
54
|
+
|
|
55
|
+
| | **qpdf-compress** | qpdf CLI | Ghostscript |
|
|
56
|
+
| ------------------------- | ----------------------- | ----------------- | ----------------- |
|
|
57
|
+
| Integration | Native Node.js addon | Shell exec | Shell exec |
|
|
58
|
+
| Async I/O | ✅ Non-blocking | ❌ Blocks on exec | ❌ Blocks on exec |
|
|
59
|
+
| Image deduplication | ✅ | ❌ | ❌ |
|
|
60
|
+
| JPEG Huffman optimization | ✅ Lossless (libjpeg) | ❌ | ❌ |
|
|
61
|
+
| Lossy image compression | ✅ Configurable quality | ❌ | ✅ |
|
|
62
|
+
| PDF repair | ✅ Automatic | ✅ Manual flag | ⚠️ Partial |
|
|
63
|
+
| License | Apache-2.0 | Apache-2.0 | AGPL-3.0 ⚠️ |
|
|
64
|
+
| Dependencies | None¹ | System binary | System binary |
|
|
65
|
+
|
|
66
|
+
¹ QPDF is statically linked — no runtime dependencies. Prebuilt binaries downloaded at install.
|
|
67
|
+
|
|
68
|
+
## 📦 Install
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
npm install qpdf-compress
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Prebuilt binaries are available for all [supported platforms](#-supported-platforms) — most installs require no compiler. If no prebuilt is available, the package falls back to compiling from source.
|
|
75
|
+
|
|
76
|
+
### Build prerequisites (source fallback)
|
|
77
|
+
|
|
78
|
+
- CMake ≥ 3.16
|
|
79
|
+
- C++20 compiler (GCC 10+, Clang 13+, MSVC 2019+)
|
|
80
|
+
- zlib development headers
|
|
81
|
+
- libjpeg-turbo (or libjpeg) development headers
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
# macOS
|
|
85
|
+
brew install cmake jpeg-turbo
|
|
86
|
+
|
|
87
|
+
# Ubuntu / Debian
|
|
88
|
+
sudo apt install cmake g++ zlib1g-dev libjpeg-turbo8-dev
|
|
89
|
+
|
|
90
|
+
# Amazon Linux / RHEL
|
|
91
|
+
sudo yum install cmake3 gcc-c++ zlib-devel libjpeg-turbo-devel
|
|
92
|
+
|
|
93
|
+
# Windows (using vcpkg)
|
|
94
|
+
vcpkg install zlib libjpeg-turbo --triplet x64-windows-static
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## 🌍 Supported Platforms
|
|
98
|
+
|
|
99
|
+
| OS | Architectures |
|
|
100
|
+
| ------------- | --------------- |
|
|
101
|
+
| macOS | arm64, x64 |
|
|
102
|
+
| Linux (glibc) | x64, arm64, arm |
|
|
103
|
+
| Linux (musl) | x64, arm64 |
|
|
104
|
+
| Windows | x64, arm64 |
|
|
105
|
+
|
|
106
|
+
## 📚 Usage
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { compress } from 'qpdf-compress';
|
|
110
|
+
|
|
111
|
+
// lossless — optimize streams without touching image quality
|
|
112
|
+
const optimized = await compress(pdfBuffer, { mode: 'lossless' });
|
|
113
|
+
|
|
114
|
+
// lossy — recompress images as JPEG (default quality: 75)
|
|
115
|
+
const smaller = await compress(pdfBuffer, { mode: 'lossy' });
|
|
116
|
+
|
|
117
|
+
// lossy with custom quality (1–100)
|
|
118
|
+
const tiny = await compress(pdfBuffer, { mode: 'lossy', quality: 50 });
|
|
119
|
+
|
|
120
|
+
// file path input (avoids copying into memory twice)
|
|
121
|
+
const result = await compress('/path/to/file.pdf', { mode: 'lossless' });
|
|
122
|
+
|
|
123
|
+
// write directly to file instead of returning a Buffer
|
|
124
|
+
await compress(pdfBuffer, { mode: 'lossless', output: '/path/to/output.pdf' });
|
|
125
|
+
|
|
126
|
+
// damaged PDFs are automatically repaired during compression
|
|
127
|
+
const fixed = await compress(damagedBuffer, { mode: 'lossless' });
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## 📖 API
|
|
131
|
+
|
|
132
|
+
### `compress(input, options): Promise<Buffer>`
|
|
133
|
+
|
|
134
|
+
### `compress(input, options & { output: string }): Promise<void>`
|
|
135
|
+
|
|
136
|
+
Compresses a PDF document. Automatically repairs damaged PDFs.
|
|
137
|
+
|
|
138
|
+
| Parameter | Type | Description |
|
|
139
|
+
| ----------------- | ----------------------- | -------------------------------------------------- |
|
|
140
|
+
| `input` | `Buffer \| string` | PDF data or file path |
|
|
141
|
+
| `options.mode` | `'lossy' \| 'lossless'` | Compression mode |
|
|
142
|
+
| `options.quality` | `number` | JPEG quality 1–100 (lossy only, default: 75) |
|
|
143
|
+
| `options.output` | `string` | Write to file path instead of returning a `Buffer` |
|
|
144
|
+
|
|
145
|
+
**Lossless mode:**
|
|
146
|
+
|
|
147
|
+
- Deduplicates identical images across pages
|
|
148
|
+
- Optimizes embedded JPEG Huffman tables (2–15% savings, zero quality loss)
|
|
149
|
+
- Recompresses all decodable streams with Flate level 9
|
|
150
|
+
- Generates object streams for smaller metadata overhead
|
|
151
|
+
- Removes unreferenced objects
|
|
152
|
+
|
|
153
|
+
**Lossy mode** (in addition to lossless optimizations):
|
|
154
|
+
|
|
155
|
+
- Extracts 8-bit RGB and grayscale images
|
|
156
|
+
- Recompresses as JPEG at the specified quality
|
|
157
|
+
- Only replaces images where JPEG is actually smaller
|
|
158
|
+
- Skips tiny images (< 50×50 px), CMYK, and indexed color
|
|
159
|
+
|
|
160
|
+
## ⚙️ How it works
|
|
161
|
+
|
|
162
|
+
This package embeds [QPDF](https://github.com/qpdf/qpdf) (v12.3.2) as a statically linked C++ library, exposed to Node.js via N-API. Lossless JPEG optimization uses [libjpeg-turbo](https://libjpeg-turbo.org/) at the DCT coefficient level. Image recompression in lossy mode uses [stb_image_write](https://github.com/nothings/stb) for JPEG encoding.
|
|
163
|
+
|
|
164
|
+
All operations run in a background thread via `Napi::AsyncWorker`, so the event loop is never blocked.
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
|
|
168
|
+
Apache-2.0
|
package/binding.gyp
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
{
|
|
2
|
+
"targets": [
|
|
3
|
+
{
|
|
4
|
+
"target_name": "qpdf_compress",
|
|
5
|
+
"sources": [
|
|
6
|
+
"src/qpdf_addon.cc",
|
|
7
|
+
"src/stb_impl.cc"
|
|
8
|
+
],
|
|
9
|
+
"include_dirs": [
|
|
10
|
+
"<!@(node -p \"require('node-addon-api').include\")",
|
|
11
|
+
"deps/qpdf/include",
|
|
12
|
+
"src"
|
|
13
|
+
],
|
|
14
|
+
"defines": [
|
|
15
|
+
"NAPI_VERSION=8"
|
|
16
|
+
],
|
|
17
|
+
"cflags!": [
|
|
18
|
+
"-fno-exceptions"
|
|
19
|
+
],
|
|
20
|
+
"cflags": [
|
|
21
|
+
"-Os",
|
|
22
|
+
"-flto",
|
|
23
|
+
"-ffunction-sections",
|
|
24
|
+
"-fdata-sections"
|
|
25
|
+
],
|
|
26
|
+
"cflags_cc!": [
|
|
27
|
+
"-fno-exceptions"
|
|
28
|
+
],
|
|
29
|
+
"cflags_cc": [
|
|
30
|
+
"-std=c++20",
|
|
31
|
+
"-fvisibility=hidden"
|
|
32
|
+
],
|
|
33
|
+
"conditions": [
|
|
34
|
+
[
|
|
35
|
+
"OS=='mac'",
|
|
36
|
+
{
|
|
37
|
+
"include_dirs": [
|
|
38
|
+
"/opt/homebrew/include",
|
|
39
|
+
"/usr/local/include"
|
|
40
|
+
],
|
|
41
|
+
"xcode_settings": {
|
|
42
|
+
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
|
|
43
|
+
"CLANG_CXX_LANGUAGE_STANDARD": "c++20",
|
|
44
|
+
"GCC_SYMBOLS_PRIVATE_EXTERN": "YES",
|
|
45
|
+
"DEAD_CODE_STRIPPING": "YES",
|
|
46
|
+
"LLVM_LTO": "YES",
|
|
47
|
+
"OTHER_CPLUSPLUSFLAGS": [
|
|
48
|
+
"-Os",
|
|
49
|
+
"-flto",
|
|
50
|
+
"-ffunction-sections",
|
|
51
|
+
"-fdata-sections"
|
|
52
|
+
],
|
|
53
|
+
"OTHER_LDFLAGS": [
|
|
54
|
+
"<(module_root_dir)/deps/qpdf/lib/libqpdf.a",
|
|
55
|
+
"-L/opt/homebrew/lib",
|
|
56
|
+
"-L/usr/local/lib",
|
|
57
|
+
"-lz",
|
|
58
|
+
"-ljpeg",
|
|
59
|
+
"-flto",
|
|
60
|
+
"-Wl,-dead_strip",
|
|
61
|
+
"-Wl,-S"
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
],
|
|
66
|
+
[
|
|
67
|
+
"OS=='linux'",
|
|
68
|
+
{
|
|
69
|
+
"libraries": [
|
|
70
|
+
"<(module_root_dir)/deps/qpdf/lib/libqpdf.a",
|
|
71
|
+
"-lz",
|
|
72
|
+
"-ljpeg",
|
|
73
|
+
"-flto",
|
|
74
|
+
"-Wl,--gc-sections",
|
|
75
|
+
"-Wl,-S",
|
|
76
|
+
"-lpthread",
|
|
77
|
+
"-ldl"
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
],
|
|
81
|
+
[
|
|
82
|
+
"OS=='win'",
|
|
83
|
+
{
|
|
84
|
+
"defines": [
|
|
85
|
+
"_HAS_EXCEPTIONS=1"
|
|
86
|
+
],
|
|
87
|
+
"msvs_settings": {
|
|
88
|
+
"VCCLCompilerTool": {
|
|
89
|
+
"ExceptionHandling": 1,
|
|
90
|
+
"Optimization": 1,
|
|
91
|
+
"WholeProgramOptimization": "true",
|
|
92
|
+
"RuntimeLibrary": 0,
|
|
93
|
+
"AdditionalOptions": [
|
|
94
|
+
"/std:c++20"
|
|
95
|
+
]
|
|
96
|
+
},
|
|
97
|
+
"VCLinkerTool": {
|
|
98
|
+
"LinkTimeCodeGeneration": 1
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
"libraries": [
|
|
102
|
+
"<(module_root_dir)/deps/qpdf/lib/qpdf.lib",
|
|
103
|
+
"<(module_root_dir)/deps/qpdf/lib/zlib.lib",
|
|
104
|
+
"<(module_root_dir)/deps/qpdf/lib/jpeg.lib"
|
|
105
|
+
]
|
|
106
|
+
}
|
|
107
|
+
]
|
|
108
|
+
]
|
|
109
|
+
}
|
|
110
|
+
]
|
|
111
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CompressOptions } from './types.js';
|
|
2
|
+
type PdfInput = Buffer | string;
|
|
3
|
+
/**
|
|
4
|
+
* Compresses a PDF document. Automatically repairs damaged PDFs.
|
|
5
|
+
*
|
|
6
|
+
* In lossless mode, deduplicates images, optimizes embedded JPEG Huffman
|
|
7
|
+
* tables, recompresses all streams with Flate level 9, generates object
|
|
8
|
+
* streams, and removes unreferenced objects.
|
|
9
|
+
*
|
|
10
|
+
* In lossy mode, additionally recompresses embedded images as JPEG at the
|
|
11
|
+
* specified quality. Text, vectors, and fonts are preserved.
|
|
12
|
+
*/
|
|
13
|
+
export declare function compress(input: PdfInput, options: CompressOptions & {
|
|
14
|
+
output: string;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
export declare function compress(input: PdfInput, options: CompressOptions): Promise<Buffer>;
|
|
17
|
+
export type { CompressOptions } from './types.js';
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAe,MAAM,YAAY,CAAC;AAY/D,KAAK,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AAEhC;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CACtB,KAAK,EAAE,QAAQ,EACf,OAAO,EAAE,eAAe,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,GAC5C,OAAO,CAAC,IAAI,CAAC,CAAC;AACjB,wBAAgB,QAAQ,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AA4BrF,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { dirname, resolve } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
5
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
const addonDir = resolve(__dirname, '..', 'build', 'Release');
|
|
7
|
+
if (process.platform === 'win32') {
|
|
8
|
+
process.env.PATH = `${addonDir};${process.env.PATH ?? ''}`;
|
|
9
|
+
}
|
|
10
|
+
const addon = require('../build/Release/qpdf_compress.node');
|
|
11
|
+
export async function compress(input, options) {
|
|
12
|
+
if (Buffer.isBuffer(input)) {
|
|
13
|
+
if (input.length === 0) {
|
|
14
|
+
throw new TypeError('Input buffer cannot be empty');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
else if (typeof input === 'string') {
|
|
18
|
+
if (input.length === 0) {
|
|
19
|
+
throw new TypeError('Input path cannot be empty');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
throw new TypeError('Input must be a Buffer or file path string');
|
|
24
|
+
}
|
|
25
|
+
const mode = options.mode;
|
|
26
|
+
if (mode !== 'lossy' && mode !== 'lossless') {
|
|
27
|
+
throw new TypeError("Mode must be 'lossy' or 'lossless'");
|
|
28
|
+
}
|
|
29
|
+
const quality = options.quality ?? 75;
|
|
30
|
+
if (quality < 1 || quality > 100) {
|
|
31
|
+
throw new RangeError('Quality must be between 1 and 100');
|
|
32
|
+
}
|
|
33
|
+
return addon.compress(input, {
|
|
34
|
+
mode,
|
|
35
|
+
quality,
|
|
36
|
+
...(options.output ? { output: options.output } : {}),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAGzC,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/C,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAE1D,MAAM,QAAQ,GAAG,OAAO,CAAC,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;AAC9D,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;IACjC,OAAO,CAAC,GAAG,CAAC,IAAI,GAAG,GAAG,QAAQ,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,EAAE,EAAE,CAAC;AAC7D,CAAC;AAED,MAAM,KAAK,GAAgB,OAAO,CAAC,qCAAqC,CAAC,CAAC;AAmB1E,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,KAAe,EAAE,OAAwB;IACtE,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,SAAS,CAAC,8BAA8B,CAAC,CAAC;QACtD,CAAC;IACH,CAAC;SAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACrC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACvB,MAAM,IAAI,SAAS,CAAC,4BAA4B,CAAC,CAAC;QACpD,CAAC;IACH,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,SAAS,CAAC,4CAA4C,CAAC,CAAC;IACpE,CAAC;IACD,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAC1B,IAAI,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,UAAU,EAAE,CAAC;QAC5C,MAAM,IAAI,SAAS,CAAC,oCAAoC,CAAC,CAAC;IAC5D,CAAC;IACD,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC;IACtC,IAAI,OAAO,GAAG,CAAC,IAAI,OAAO,GAAG,GAAG,EAAE,CAAC;QACjC,MAAM,IAAI,UAAU,CAAC,mCAAmC,CAAC,CAAC;IAC5D,CAAC;IACD,OAAO,KAAK,CAAC,QAAQ,CAAC,KAAK,EAAE;QAC3B,IAAI;QACJ,OAAO;QACP,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACtD,CAA2B,CAAC;AAC/B,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface CompressOptions {
|
|
2
|
+
/** Compression mode. */
|
|
3
|
+
readonly mode: 'lossy' | 'lossless';
|
|
4
|
+
/** JPEG quality for lossy mode (1–100). Default: 75. */
|
|
5
|
+
readonly quality?: number;
|
|
6
|
+
/** Write to this file path instead of returning a Buffer. */
|
|
7
|
+
readonly output?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface NativeAddon {
|
|
10
|
+
compress(input: Buffer | string, options: {
|
|
11
|
+
mode: 'lossy' | 'lossless';
|
|
12
|
+
quality: number;
|
|
13
|
+
output?: string;
|
|
14
|
+
}): Promise<Buffer | undefined>;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../lib/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe;IAC9B,wBAAwB;IACxB,QAAQ,CAAC,IAAI,EAAE,OAAO,GAAG,UAAU,CAAC;IACpC,wDAAwD;IACxD,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,6DAA6D;IAC7D,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,CACN,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,OAAO,EAAE;QAAE,IAAI,EAAE,OAAO,GAAG,UAAU,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,GACxE,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;CAChC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../lib/types.ts"],"names":[],"mappings":""}
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { dirname, resolve } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import type { CompressOptions, NativeAddon } from './types.js';
|
|
5
|
+
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
|
|
9
|
+
const addonDir = resolve(__dirname, '..', 'build', 'Release');
|
|
10
|
+
if (process.platform === 'win32') {
|
|
11
|
+
process.env.PATH = `${addonDir};${process.env.PATH ?? ''}`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const addon: NativeAddon = require('../build/Release/qpdf_compress.node');
|
|
15
|
+
|
|
16
|
+
type PdfInput = Buffer | string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Compresses a PDF document. Automatically repairs damaged PDFs.
|
|
20
|
+
*
|
|
21
|
+
* In lossless mode, deduplicates images, optimizes embedded JPEG Huffman
|
|
22
|
+
* tables, recompresses all streams with Flate level 9, generates object
|
|
23
|
+
* streams, and removes unreferenced objects.
|
|
24
|
+
*
|
|
25
|
+
* In lossy mode, additionally recompresses embedded images as JPEG at the
|
|
26
|
+
* specified quality. Text, vectors, and fonts are preserved.
|
|
27
|
+
*/
|
|
28
|
+
export function compress(
|
|
29
|
+
input: PdfInput,
|
|
30
|
+
options: CompressOptions & { output: string },
|
|
31
|
+
): Promise<void>;
|
|
32
|
+
export function compress(input: PdfInput, options: CompressOptions): Promise<Buffer>;
|
|
33
|
+
export async function compress(input: PdfInput, options: CompressOptions): Promise<Buffer | void> {
|
|
34
|
+
if (Buffer.isBuffer(input)) {
|
|
35
|
+
if (input.length === 0) {
|
|
36
|
+
throw new TypeError('Input buffer cannot be empty');
|
|
37
|
+
}
|
|
38
|
+
} else if (typeof input === 'string') {
|
|
39
|
+
if (input.length === 0) {
|
|
40
|
+
throw new TypeError('Input path cannot be empty');
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
throw new TypeError('Input must be a Buffer or file path string');
|
|
44
|
+
}
|
|
45
|
+
const mode = options.mode;
|
|
46
|
+
if (mode !== 'lossy' && mode !== 'lossless') {
|
|
47
|
+
throw new TypeError("Mode must be 'lossy' or 'lossless'");
|
|
48
|
+
}
|
|
49
|
+
const quality = options.quality ?? 75;
|
|
50
|
+
if (quality < 1 || quality > 100) {
|
|
51
|
+
throw new RangeError('Quality must be between 1 and 100');
|
|
52
|
+
}
|
|
53
|
+
return addon.compress(input, {
|
|
54
|
+
mode,
|
|
55
|
+
quality,
|
|
56
|
+
...(options.output ? { output: options.output } : {}),
|
|
57
|
+
}) as Promise<Buffer | void>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type { CompressOptions } from './types.js';
|
package/lib/types.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface CompressOptions {
|
|
2
|
+
/** Compression mode. */
|
|
3
|
+
readonly mode: 'lossy' | 'lossless';
|
|
4
|
+
/** JPEG quality for lossy mode (1–100). Default: 75. */
|
|
5
|
+
readonly quality?: number;
|
|
6
|
+
/** Write to this file path instead of returning a Buffer. */
|
|
7
|
+
readonly output?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface NativeAddon {
|
|
11
|
+
compress(
|
|
12
|
+
input: Buffer | string,
|
|
13
|
+
options: { mode: 'lossy' | 'lossless'; quality: number; output?: string },
|
|
14
|
+
): Promise<Buffer | undefined>;
|
|
15
|
+
}
|