pdfium-native 0.2.4 → 0.3.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/CHANGELOG.md +193 -0
- package/README.md +36 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/types.d.ts +18 -1
- package/dist/types.d.ts.map +1 -1
- package/lib/index.ts +1 -0
- package/lib/types.ts +17 -1
- package/package.json +5 -2
- package/src/objects_worker.h +15 -2
- package/src/page.h +58 -4
- package/src/render_image_worker.h +258 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/), and this project adheres to [Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [0.3.0] - 2026-03-31
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `ImagePageObject.render()` — extract and render embedded PDF images as PNG, JPEG, or raw stream bytes
|
|
12
|
+
- Three render modes: intrinsic bitmap, rendered bitmap (with mask/matrix), and raw encoded stream
|
|
13
|
+
- File output support for image rendering via `output` option
|
|
14
|
+
- `objects()` async generator on `PDFiumPage` for iterating all page objects
|
|
15
|
+
- Image dimension cap (`MAX_IMAGE_PIXELS = 256 MP`) to prevent allocation failures on huge embedded images
|
|
16
|
+
- Negative object index guard in `RenderImageWorker`
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- **Breaking:** Default render format changed from JPEG to PNG for both `page.render()` and `image.render()` — use `{ format: 'jpeg' }` to restore previous behavior
|
|
21
|
+
- Image `render()` binding moved from TypeScript layer to native C++ (`GetObjectWorker::OnOK`) for cleaner API
|
|
22
|
+
- `PDFiumPage.getObject()` is now a direct passthrough to the native addon
|
|
23
|
+
|
|
24
|
+
## [0.3.0] - 2026-03-31
|
|
25
|
+
|
|
26
|
+
### Added
|
|
27
|
+
|
|
28
|
+
- `ImagePageObject.render()` — extract and render embedded PDF images as PNG, JPEG, or raw stream bytes
|
|
29
|
+
- Three render modes: intrinsic bitmap, rendered bitmap (with mask/matrix), and raw encoded stream
|
|
30
|
+
- File output support for image rendering via `output` option
|
|
31
|
+
- `objects()` async generator on `PDFiumPage` for iterating all page objects
|
|
32
|
+
- Image dimension cap (`MAX_IMAGE_PIXELS = 256 MP`) to prevent allocation failures on huge embedded images
|
|
33
|
+
- Negative object index guard in `RenderImageWorker`
|
|
34
|
+
|
|
35
|
+
### Changed
|
|
36
|
+
|
|
37
|
+
- **Breaking:** Default render format changed from JPEG to PNG for both `page.render()` and `image.render()` — use `{ format: 'jpeg' }` to restore previous behavior
|
|
38
|
+
- Image `render()` binding moved from TypeScript layer to native C++ (`GetObjectWorker::OnOK`) for cleaner API
|
|
39
|
+
- `PDFiumPage.getObject()` is now a direct passthrough to the native addon
|
|
40
|
+
|
|
41
|
+
## [0.2.4] - 2026-03-30
|
|
42
|
+
|
|
43
|
+
### Fixed
|
|
44
|
+
|
|
45
|
+
- Bumped minimum Node.js version to 20.11.0 (`import.meta.dirname` requirement)
|
|
46
|
+
- Wrapped web `ReadableStream` with `Readable.fromWeb()` in install script
|
|
47
|
+
- Updated README Node version range from 18–24 to 20–24
|
|
48
|
+
- Used static import for `Readable` instead of dynamic `import()`
|
|
49
|
+
|
|
50
|
+
### Added
|
|
51
|
+
|
|
52
|
+
- npm publish provenance (`--provenance` flag)
|
|
53
|
+
- Build provenance attestation for prebuilt binaries via `actions/attest-build-provenance`
|
|
54
|
+
|
|
55
|
+
## [0.2.3] - 2026-03-28
|
|
56
|
+
|
|
57
|
+
### Added
|
|
58
|
+
|
|
59
|
+
- `getFormFields()` for reading PDF form fields (text fields, checkboxes, radio buttons, combo boxes, list boxes, signatures)
|
|
60
|
+
- Bookmark sibling count limit (`MAX_SIBLINGS_PER_LEVEL = 10000`) to prevent DoS from malicious PDFs
|
|
61
|
+
|
|
62
|
+
### Changed
|
|
63
|
+
|
|
64
|
+
- Extracted shared UTF-16 read/write helpers (`ReadU16`, `SetU16`, `SetU16IfPresent`, `ToNapiString`, `ActionTypeString`) to `napi_helpers.h`, reducing boilerplate across all workers
|
|
65
|
+
- Split monolithic `page_workers.h` (1500 lines) into 7 individual worker files
|
|
66
|
+
- Moved `CHECK_ALIVE` macro to `napi_helpers.h`
|
|
67
|
+
|
|
68
|
+
### Fixed
|
|
69
|
+
|
|
70
|
+
- `ReadU16` now rejects odd byte lengths to prevent buffer overflow on malformed PDFs
|
|
71
|
+
- `SearchWorker` and `GetFormFieldsWorker` now properly reject (instead of silently resolving empty) when PDFium fails to load text page or initialize form environment
|
|
72
|
+
- Form fill environment cleanup is now guarded against null handle
|
|
73
|
+
- Render worker validates bitmap buffer and stride before pixel conversion
|
|
74
|
+
- Render worker pixel buffer allocation casts both dimensions to `size_t` to prevent integer overflow
|
|
75
|
+
- `stb_write_callback` guards against negative size parameter
|
|
76
|
+
- `LoadDocumentWorker` rejects buffers larger than `INT_MAX`
|
|
77
|
+
- Annotation handles are now closed before `push_back` to prevent resource leaks on allocation failure
|
|
78
|
+
- Text and search workers guard against `INT_MAX` overflow in buffer sizing
|
|
79
|
+
- Removed unused `#include <algorithm>` from `page.h`
|
|
80
|
+
|
|
81
|
+
## [0.2.2] - 2026-03-28
|
|
82
|
+
|
|
83
|
+
### Changed
|
|
84
|
+
|
|
85
|
+
- README overhaul: benefit-driven headline, Quick Start moved above features, grouped "Why" section (Performance / Developer experience / Reliability), use cases section, competitor comparison table with actual packages (@hyzyla/pdfium, pdfjs-dist, pdf-parse, pdf2json)
|
|
86
|
+
- Added npm badges (version, node, license, platform)
|
|
87
|
+
- Expanded npm keywords for discoverability
|
|
88
|
+
- Added homepage and bugs URLs to package.json
|
|
89
|
+
|
|
90
|
+
## [0.2.1] - 2026-03-28
|
|
91
|
+
|
|
92
|
+
### Changed
|
|
93
|
+
|
|
94
|
+
- Cross-compile `darwin-x64` on `macos-latest` (arm64) and `win32-arm64` on `windows-latest` (x64), eliminating dedicated runners
|
|
95
|
+
- Moved `linux-arm64` prebuild from native `ubuntu-24.04-arm` runner to QEMU Docker cross-compile
|
|
96
|
+
- Added `linux-musl-x64` and `linux-musl-arm64` prebuilds via Alpine Docker
|
|
97
|
+
|
|
98
|
+
### Fixed
|
|
99
|
+
|
|
100
|
+
- `install.mjs` now detects musl libc and downloads the correct prebuilt tarball on Alpine/musl systems
|
|
101
|
+
- Cache page dimensions in C++ to avoid reading them without mutex in `Render()`
|
|
102
|
+
- `g_initialized` is now `std::atomic<bool>` for thread safety
|
|
103
|
+
- Consistent alive flag ordering in `PDFiumPage::CleanUp()` (set under mutex, matching `PDFiumDocument`)
|
|
104
|
+
|
|
105
|
+
### Performance
|
|
106
|
+
|
|
107
|
+
- Binary size reduced ~37% via LTO, `-Os`, `-fvisibility=hidden`, dead code stripping, and link-time symbol stripping
|
|
108
|
+
|
|
109
|
+
## [0.2.0] - 2026-03-28
|
|
110
|
+
|
|
111
|
+
### Changed
|
|
112
|
+
|
|
113
|
+
- **BREAKING**: `getText()`, `getObject()`, `getLinks()`, `search()`, `getAnnotations()`, and `getBookmarks()` now return Promises instead of synchronous values
|
|
114
|
+
- `objects()` generator is now an async generator (`AsyncGenerator<PageObject>`)
|
|
115
|
+
- GetPage bounds check moved off the main thread into the async worker
|
|
116
|
+
- Alive flags in `destroy()`/`close()` are now set under the global mutex
|
|
117
|
+
|
|
118
|
+
### Added
|
|
119
|
+
|
|
120
|
+
- GC Finalize callbacks on `PDFiumPage` and `PDFiumDocument` as safety net for unreleased resources
|
|
121
|
+
- `FPDF_DestroyLibrary` cleanup hook on environment teardown
|
|
122
|
+
- `page_workers.h` with dedicated AsyncWorker classes for all page/document operations
|
|
123
|
+
|
|
124
|
+
### Fixed
|
|
125
|
+
|
|
126
|
+
- SSRF vulnerability in install script (CodeQL critical)
|
|
127
|
+
- Missing `permissions: contents: read` in CI/release workflow jobs (CodeQL medium)
|
|
128
|
+
|
|
129
|
+
### Removed
|
|
130
|
+
|
|
131
|
+
- `ia32` platform support (macOS, Linux musl, Windows)
|
|
132
|
+
|
|
133
|
+
## [0.1.7] - 2026-03-28
|
|
134
|
+
|
|
135
|
+
### Fixed
|
|
136
|
+
|
|
137
|
+
- MSVC ARM64 build: compile `stb_image_write` as C++ to fix const initializer error
|
|
138
|
+
|
|
139
|
+
## [0.1.6] - 2026-03-28
|
|
140
|
+
|
|
141
|
+
### Fixed
|
|
142
|
+
|
|
143
|
+
- Windows prebuild: use bash shell for tarball creation
|
|
144
|
+
|
|
145
|
+
## [0.1.5] - 2026-03-28
|
|
146
|
+
|
|
147
|
+
### Changed
|
|
148
|
+
|
|
149
|
+
- Merged publish and prebuild into single `release.yml` workflow
|
|
150
|
+
|
|
151
|
+
## [0.1.4] - 2026-03-28
|
|
152
|
+
|
|
153
|
+
### Changed
|
|
154
|
+
|
|
155
|
+
- Release triggered by tag push (`v*`) instead of GitHub release event
|
|
156
|
+
- Auto-create GitHub release with prebuilt tarballs
|
|
157
|
+
|
|
158
|
+
## [0.1.3] - 2026-03-28
|
|
159
|
+
|
|
160
|
+
### Fixed
|
|
161
|
+
|
|
162
|
+
- npm publish authentication with OIDC trusted publishing (Node 24 / npm 11.5.1+)
|
|
163
|
+
|
|
164
|
+
## [0.1.2] - 2026-03-28
|
|
165
|
+
|
|
166
|
+
### Fixed
|
|
167
|
+
|
|
168
|
+
- Prebuild tarball naming and `contents: write` permission for release uploads
|
|
169
|
+
|
|
170
|
+
## [0.1.1] - 2026-03-28
|
|
171
|
+
|
|
172
|
+
### Fixed
|
|
173
|
+
|
|
174
|
+
- Prebuild workflow failures
|
|
175
|
+
- Added `@types/node` dev dependency
|
|
176
|
+
|
|
177
|
+
## [0.1.0] - 2026-03-27
|
|
178
|
+
|
|
179
|
+
### Added
|
|
180
|
+
|
|
181
|
+
- Async document loading from `Buffer` or file path (with optional password)
|
|
182
|
+
- Page rendering to JPEG/PNG (`Buffer` or file output) with scale/quality options
|
|
183
|
+
- Text extraction via `getText()`
|
|
184
|
+
- Text search with bounding rectangles via `search()`
|
|
185
|
+
- Page object inspection with discriminated union types (text/image/path/etc.)
|
|
186
|
+
- Document metadata as a cached readonly property
|
|
187
|
+
- Bookmark tree traversal via `getBookmarks()`
|
|
188
|
+
- Link enumeration via `getLinks()`
|
|
189
|
+
- Annotation reading via `getAnnotations()`
|
|
190
|
+
- Support for macOS (arm64, x64), Linux glibc (x64, arm64, arm, ia32, ppc64), Linux musl (x64, arm64, ia32), and Windows (x64, arm64, ia32)
|
|
191
|
+
- ESLint + Prettier configuration
|
|
192
|
+
- GitHub Actions publish workflow with test gate
|
|
193
|
+
- TypeScript type declarations for JS consumers
|
package/README.md
CHANGED
|
@@ -173,7 +173,7 @@ interface PageRenderOptions {
|
|
|
173
173
|
scale?: number; // default: 1 (72 DPI). Use 3–4 for print quality.
|
|
174
174
|
width?: number; // override render width in pixels
|
|
175
175
|
height?: number; // override render height in pixels
|
|
176
|
-
format?: 'jpeg' | 'png'; // default: '
|
|
176
|
+
format?: 'jpeg' | 'png'; // default: 'png'
|
|
177
177
|
quality?: number; // JPEG quality 1–100 (default: 100)
|
|
178
178
|
output?: string; // write to file instead of returning a Buffer
|
|
179
179
|
rotation?: 0 | 1 | 2 | 3; // 0=none, 1=90° CW, 2=180°, 3=270° CW
|
|
@@ -199,10 +199,44 @@ type PageObject = TextPageObject | ImagePageObject | OtherPageObject;
|
|
|
199
199
|
// type: 'text' adds: text, fontSize, fontName, fontWeight?, italicAngle?,
|
|
200
200
|
// renderMode?, fontFamily?, isEmbedded?, fontFlags?
|
|
201
201
|
// type: 'image' adds: imageWidth, imageHeight, horizontalDpi?, verticalDpi?,
|
|
202
|
-
// bitsPerPixel?, colorspace?, filters
|
|
202
|
+
// bitsPerPixel?, colorspace?, filters?, render()
|
|
203
203
|
// type: 'path' | 'shading' | 'form' | 'unknown'
|
|
204
204
|
```
|
|
205
205
|
|
|
206
|
+
**Image objects** have a `render()` method for extracting the embedded image:
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
const obj = await page.getObject(0);
|
|
210
|
+
if (obj.type === 'image') {
|
|
211
|
+
const png = await obj.render(); // PNG buffer (default)
|
|
212
|
+
const jpeg = await obj.render({ format: 'jpeg', quality: 80 });
|
|
213
|
+
const raw = await obj.render({ format: 'raw' }); // original encoded stream
|
|
214
|
+
await obj.render({ output: '/tmp/image.png' }); // write to file
|
|
215
|
+
await obj.render({ rendered: true }); // apply image mask and transformation matrix
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
interface ImageRenderOptions {
|
|
221
|
+
format?: 'jpeg' | 'png' | 'raw'; // default: 'png'. 'raw' returns original stream bytes
|
|
222
|
+
quality?: number; // JPEG quality 1–100 (default: 100)
|
|
223
|
+
output?: string; // write to file instead of returning a Buffer
|
|
224
|
+
rendered?: boolean; // apply image mask and transformation matrix (default: false)
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
#### `objects()`
|
|
229
|
+
|
|
230
|
+
Async generator that yields every page object. Convenience wrapper around `getObject()`.
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
for await (const obj of page.objects()) {
|
|
234
|
+
if (obj.type === 'image') {
|
|
235
|
+
await obj.render({ output: `image-${obj.imageWidth}x${obj.imageHeight}.png` });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
206
240
|
#### `getLinks()`
|
|
207
241
|
|
|
208
242
|
Returns all links on the page. Returns `Promise<Link[]>`.
|
package/dist/index.d.ts
CHANGED
|
@@ -7,5 +7,5 @@ export declare function loadDocument(input: PdfInput, password?: string): Promis
|
|
|
7
7
|
export { PDFiumDocument } from './document.js';
|
|
8
8
|
export { PDFiumError, PDFiumFileError, PDFiumFormatError, PDFiumPasswordError, PDFiumSecurityError, } from './errors.js';
|
|
9
9
|
export { PDFiumPage } from './page.js';
|
|
10
|
-
export type { Annotation, AnnotationBorder, AnnotationType, Bookmark, DocumentMetadata, DocumentPermissions, FormField, FormFieldOption, FormFieldType, ImageColorspace, ImagePageObject, Link, LinkActionType, OtherPageObject, PageObject, PageObjectBounds, PageRenderOptions, QuadPoints, RGBA, SearchMatch, SearchOptions, SearchRect, TextPageObject, TextRenderMode, } from './types.js';
|
|
10
|
+
export type { Annotation, AnnotationBorder, AnnotationType, Bookmark, DocumentMetadata, DocumentPermissions, FormField, FormFieldOption, FormFieldType, ImageColorspace, ImagePageObject, ImageRenderOptions, Link, LinkActionType, OtherPageObject, PageObject, PageObjectBounds, PageRenderOptions, QuadPoints, RGBA, SearchMatch, SearchOptions, SearchRect, TextPageObject, TextRenderMode, } from './types.js';
|
|
11
11
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAO/C,KAAK,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AAchC;;GAEG;AACH,wBAAsB,YAAY,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAM9F;AAED,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EACL,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,YAAY,EACV,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,QAAQ,EACR,gBAAgB,EAChB,mBAAmB,EACnB,SAAS,EACT,eAAe,EACf,aAAa,EACb,eAAe,EACf,eAAe,EACf,IAAI,EACJ,cAAc,EACd,eAAe,EACf,UAAU,EACV,gBAAgB,EAChB,iBAAiB,EACjB,UAAU,EACV,IAAI,EACJ,WAAW,EACX,aAAa,EACb,UAAU,EACV,cAAc,EACd,cAAc,GACf,MAAM,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAO/C,KAAK,QAAQ,GAAG,MAAM,GAAG,MAAM,CAAC;AAchC;;GAEG;AACH,wBAAsB,YAAY,CAAC,KAAK,EAAE,QAAQ,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAM9F;AAED,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EACL,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,YAAY,EACV,UAAU,EACV,gBAAgB,EAChB,cAAc,EACd,QAAQ,EACR,gBAAgB,EAChB,mBAAmB,EACnB,SAAS,EACT,eAAe,EACf,aAAa,EACb,eAAe,EACf,eAAe,EACf,kBAAkB,EAClB,IAAI,EACJ,cAAc,EACd,eAAe,EACf,UAAU,EACV,gBAAgB,EAChB,iBAAiB,EACjB,UAAU,EACV,IAAI,EACJ,WAAW,EACX,aAAa,EACb,UAAU,EACV,cAAc,EACd,cAAc,GACf,MAAM,YAAY,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export interface PageRenderOptions {
|
|
|
5
5
|
width?: number;
|
|
6
6
|
/** Override render height in pixels. */
|
|
7
7
|
height?: number;
|
|
8
|
-
/** Output format (default: '
|
|
8
|
+
/** Output format (default: 'png'). */
|
|
9
9
|
format?: 'jpeg' | 'png';
|
|
10
10
|
/** JPEG quality 1–100 (default: 100). Ignored for PNG. */
|
|
11
11
|
quality?: number;
|
|
@@ -73,6 +73,16 @@ export interface TextPageObject extends BasePageObject {
|
|
|
73
73
|
fontFlags?: number;
|
|
74
74
|
}
|
|
75
75
|
export type ImageColorspace = 'deviceGray' | 'deviceRGB' | 'deviceCMYK' | 'calGray' | 'calRGB' | 'lab' | 'iccBased' | 'separation' | 'deviceN' | 'indexed' | 'pattern' | 'unknown';
|
|
76
|
+
export interface ImageRenderOptions {
|
|
77
|
+
/** Output format (default: 'png'). Use 'raw' for original encoded stream bytes. */
|
|
78
|
+
format?: 'jpeg' | 'png' | 'raw';
|
|
79
|
+
/** JPEG quality 1–100 (default: 100). Ignored for PNG and raw. */
|
|
80
|
+
quality?: number;
|
|
81
|
+
/** Write to this file path instead of returning a Buffer. */
|
|
82
|
+
output?: string;
|
|
83
|
+
/** Use the rendered bitmap (applies image mask and transformation matrix). Default: false. Ignored when format is 'raw'. */
|
|
84
|
+
rendered?: boolean;
|
|
85
|
+
}
|
|
76
86
|
export interface ImagePageObject extends BasePageObject {
|
|
77
87
|
type: 'image';
|
|
78
88
|
/** Intrinsic image width in pixels. */
|
|
@@ -89,6 +99,12 @@ export interface ImagePageObject extends BasePageObject {
|
|
|
89
99
|
colorspace?: ImageColorspace;
|
|
90
100
|
/** Compression filters applied to the image (e.g. 'FlateDecode', 'DCTDecode'). */
|
|
91
101
|
filters?: string[];
|
|
102
|
+
/** Renders the image to a file. */
|
|
103
|
+
render(options: ImageRenderOptions & {
|
|
104
|
+
output: string;
|
|
105
|
+
}): Promise<void>;
|
|
106
|
+
/** Renders the image to an encoded buffer (JPEG, PNG, or raw stream bytes). */
|
|
107
|
+
render(options?: ImageRenderOptions): Promise<Buffer>;
|
|
92
108
|
}
|
|
93
109
|
export interface OtherPageObject extends BasePageObject {
|
|
94
110
|
type: 'path' | 'shading' | 'form' | 'unknown';
|
|
@@ -306,6 +322,7 @@ export interface NativePage {
|
|
|
306
322
|
getText(): Promise<string>;
|
|
307
323
|
render(options?: PageRenderOptions): Promise<Buffer | void>;
|
|
308
324
|
getObject(index: number): Promise<PageObject>;
|
|
325
|
+
renderImage(index: number, options?: ImageRenderOptions): Promise<Buffer | void>;
|
|
309
326
|
getLinks(): Promise<Link[]>;
|
|
310
327
|
search(text: string, options?: SearchOptions): Promise<SearchMatch[]>;
|
|
311
328
|
getAnnotations(): Promise<Annotation[]>;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../lib/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,iBAAiB;IAChC,4EAA4E;IAC5E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,uCAAuC;IACvC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,wCAAwC;IACxC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../lib/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,iBAAiB;IAChC,4EAA4E;IAC5E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,uCAAuC;IACvC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,wCAAwC;IACxC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,sCAAsC;IACtC,MAAM,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IACxB,0DAA0D;IAC1D,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6DAA6D;IAC7D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kEAAkE;IAClE,QAAQ,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACzB,sEAAsE;IACtE,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,qDAAqD;IACrD,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,2BAA2B;IAC3B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,qEAAqE;IACrE,OAAO,CAAC,EAAE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Q,CAAC,QAAQ,EAAE,gBAAgB,CAAC;IACpC,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC5C,YAAY,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;IACpC,OAAO,IAAI,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,WAAW;IAC1B,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;CAClF"}
|
package/lib/index.ts
CHANGED
package/lib/types.ts
CHANGED
|
@@ -5,7 +5,7 @@ export interface PageRenderOptions {
|
|
|
5
5
|
width?: number;
|
|
6
6
|
/** Override render height in pixels. */
|
|
7
7
|
height?: number;
|
|
8
|
-
/** Output format (default: '
|
|
8
|
+
/** Output format (default: 'png'). */
|
|
9
9
|
format?: 'jpeg' | 'png';
|
|
10
10
|
/** JPEG quality 1–100 (default: 100). Ignored for PNG. */
|
|
11
11
|
quality?: number;
|
|
@@ -101,6 +101,17 @@ export type ImageColorspace =
|
|
|
101
101
|
| 'pattern'
|
|
102
102
|
| 'unknown';
|
|
103
103
|
|
|
104
|
+
export interface ImageRenderOptions {
|
|
105
|
+
/** Output format (default: 'png'). Use 'raw' for original encoded stream bytes. */
|
|
106
|
+
format?: 'jpeg' | 'png' | 'raw';
|
|
107
|
+
/** JPEG quality 1–100 (default: 100). Ignored for PNG and raw. */
|
|
108
|
+
quality?: number;
|
|
109
|
+
/** Write to this file path instead of returning a Buffer. */
|
|
110
|
+
output?: string;
|
|
111
|
+
/** Use the rendered bitmap (applies image mask and transformation matrix). Default: false. Ignored when format is 'raw'. */
|
|
112
|
+
rendered?: boolean;
|
|
113
|
+
}
|
|
114
|
+
|
|
104
115
|
export interface ImagePageObject extends BasePageObject {
|
|
105
116
|
type: 'image';
|
|
106
117
|
/** Intrinsic image width in pixels. */
|
|
@@ -117,6 +128,10 @@ export interface ImagePageObject extends BasePageObject {
|
|
|
117
128
|
colorspace?: ImageColorspace;
|
|
118
129
|
/** Compression filters applied to the image (e.g. 'FlateDecode', 'DCTDecode'). */
|
|
119
130
|
filters?: string[];
|
|
131
|
+
/** Renders the image to a file. */
|
|
132
|
+
render(options: ImageRenderOptions & { output: string }): Promise<void>;
|
|
133
|
+
/** Renders the image to an encoded buffer (JPEG, PNG, or raw stream bytes). */
|
|
134
|
+
render(options?: ImageRenderOptions): Promise<Buffer>;
|
|
120
135
|
}
|
|
121
136
|
|
|
122
137
|
export interface OtherPageObject extends BasePageObject {
|
|
@@ -383,6 +398,7 @@ export interface NativePage {
|
|
|
383
398
|
getText(): Promise<string>;
|
|
384
399
|
render(options?: PageRenderOptions): Promise<Buffer | void>;
|
|
385
400
|
getObject(index: number): Promise<PageObject>;
|
|
401
|
+
renderImage(index: number, options?: ImageRenderOptions): Promise<Buffer | void>;
|
|
386
402
|
getLinks(): Promise<Link[]>;
|
|
387
403
|
search(text: string, options?: SearchOptions): Promise<SearchMatch[]>;
|
|
388
404
|
getAnnotations(): Promise<Annotation[]>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdfium-native",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Native Node.js bindings for PDFium",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -39,7 +39,10 @@
|
|
|
39
39
|
"src",
|
|
40
40
|
"lib",
|
|
41
41
|
"scripts",
|
|
42
|
-
"binding.gyp"
|
|
42
|
+
"binding.gyp",
|
|
43
|
+
"README.md",
|
|
44
|
+
"CHANGELOG.md",
|
|
45
|
+
"LICENSE"
|
|
43
46
|
],
|
|
44
47
|
"repository": {
|
|
45
48
|
"type": "git",
|
package/src/objects_worker.h
CHANGED
|
@@ -45,10 +45,11 @@ class GetObjectWorker : public Napi::AsyncWorker {
|
|
|
45
45
|
public:
|
|
46
46
|
GetObjectWorker(Napi::Env env, FPDF_PAGE page, int index,
|
|
47
47
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
48
|
-
std::shared_ptr<std::atomic<bool>> docAlive
|
|
48
|
+
std::shared_ptr<std::atomic<bool>> docAlive,
|
|
49
|
+
Napi::Object pageObj)
|
|
49
50
|
: Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
50
51
|
page_(page), index_(index), pageAlive_(std::move(pageAlive)),
|
|
51
|
-
docAlive_(std::move(docAlive)) {}
|
|
52
|
+
docAlive_(std::move(docAlive)), pageRef_(Napi::Persistent(pageObj)) {}
|
|
52
53
|
|
|
53
54
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
54
55
|
|
|
@@ -303,6 +304,17 @@ protected:
|
|
|
303
304
|
fArr.Set(fi, Napi::String::New(env, data_.filters[fi]));
|
|
304
305
|
result.Set("filters", fArr);
|
|
305
306
|
}
|
|
307
|
+
|
|
308
|
+
// bind render: (opts?) => page.renderImage(index, opts)
|
|
309
|
+
Napi::Object pageObj = pageRef_.Value();
|
|
310
|
+
Napi::Value renderImageVal = pageObj.Get("renderImage");
|
|
311
|
+
if (renderImageVal.IsFunction()) {
|
|
312
|
+
Napi::Function renderImage = renderImageVal.As<Napi::Function>();
|
|
313
|
+
Napi::Function bind = renderImage.Get("bind").As<Napi::Function>();
|
|
314
|
+
result.Set(
|
|
315
|
+
"render",
|
|
316
|
+
bind.Call(renderImage, {pageObj, Napi::Number::New(env, index_)}));
|
|
317
|
+
}
|
|
306
318
|
}
|
|
307
319
|
|
|
308
320
|
deferred_.Resolve(result);
|
|
@@ -318,5 +330,6 @@ private:
|
|
|
318
330
|
int index_;
|
|
319
331
|
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
320
332
|
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
333
|
+
Napi::ObjectReference pageRef_;
|
|
321
334
|
PageObjectData data_;
|
|
322
335
|
};
|
package/src/page.h
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
#include "form_fields_worker.h"
|
|
5
5
|
#include "links_worker.h"
|
|
6
6
|
#include "objects_worker.h"
|
|
7
|
+
#include "render_image_worker.h"
|
|
7
8
|
#include "render_worker.h"
|
|
8
9
|
#include "search_worker.h"
|
|
9
10
|
#include "text_worker.h"
|
|
@@ -30,6 +31,7 @@ public:
|
|
|
30
31
|
InstanceMethod<&PDFiumPage::GetText>("getText"),
|
|
31
32
|
InstanceMethod<&PDFiumPage::Render>("render"),
|
|
32
33
|
InstanceMethod<&PDFiumPage::GetObject>("getObject"),
|
|
34
|
+
InstanceMethod<&PDFiumPage::RenderImage>("renderImage"),
|
|
33
35
|
InstanceMethod<&PDFiumPage::GetLinks>("getLinks"),
|
|
34
36
|
InstanceMethod<&PDFiumPage::Search>("search"),
|
|
35
37
|
InstanceMethod<&PDFiumPage::GetAnnotations>("getAnnotations"),
|
|
@@ -119,7 +121,7 @@ private:
|
|
|
119
121
|
double scale = 1.0;
|
|
120
122
|
int renderWidth = 0;
|
|
121
123
|
int renderHeight = 0;
|
|
122
|
-
int format =
|
|
124
|
+
int format = IMAGE_FORMAT_PNG;
|
|
123
125
|
int quality = 100;
|
|
124
126
|
int rotation = 0;
|
|
125
127
|
bool transparent = false;
|
|
@@ -143,8 +145,8 @@ private:
|
|
|
143
145
|
renderHeight = opts.Get("height").As<Napi::Number>().Int32Value();
|
|
144
146
|
if (opts.Has("format")) {
|
|
145
147
|
std::string fmt = opts.Get("format").As<Napi::String>().Utf8Value();
|
|
146
|
-
if (fmt == "
|
|
147
|
-
format =
|
|
148
|
+
if (fmt == "jpeg")
|
|
149
|
+
format = IMAGE_FORMAT_JPEG;
|
|
148
150
|
}
|
|
149
151
|
if (opts.Has("quality"))
|
|
150
152
|
quality = opts.Get("quality").As<Napi::Number>().Int32Value();
|
|
@@ -217,7 +219,59 @@ private:
|
|
|
217
219
|
}
|
|
218
220
|
|
|
219
221
|
int idx = info[0].As<Napi::Number>().Int32Value();
|
|
220
|
-
auto *worker = new GetObjectWorker(env, page_, idx, alive_, docAlive_
|
|
222
|
+
auto *worker = new GetObjectWorker(env, page_, idx, alive_, docAlive_,
|
|
223
|
+
info.This().As<Napi::Object>());
|
|
224
|
+
auto promise = worker->Promise();
|
|
225
|
+
worker->Queue();
|
|
226
|
+
return promise;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Renders an image page object (async). Returns a Promise<Buffer>.
|
|
231
|
+
*/
|
|
232
|
+
Napi::Value RenderImage(const Napi::CallbackInfo &info) {
|
|
233
|
+
Napi::Env env = info.Env();
|
|
234
|
+
EnsureOpen(env);
|
|
235
|
+
if (env.IsExceptionPending())
|
|
236
|
+
return env.Null();
|
|
237
|
+
|
|
238
|
+
if (info.Length() < 1 || !info[0].IsNumber()) {
|
|
239
|
+
Napi::TypeError::New(env, "Expected object index")
|
|
240
|
+
.ThrowAsJavaScriptException();
|
|
241
|
+
return env.Null();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
int idx = info[0].As<Napi::Number>().Int32Value();
|
|
245
|
+
int format = IMAGE_FORMAT_PNG;
|
|
246
|
+
int quality = 100;
|
|
247
|
+
int mode = RENDER_IMAGE_BITMAP;
|
|
248
|
+
std::string outputPath;
|
|
249
|
+
|
|
250
|
+
if (info.Length() > 1 && info[1].IsObject()) {
|
|
251
|
+
Napi::Object opts = info[1].As<Napi::Object>();
|
|
252
|
+
if (opts.Has("format")) {
|
|
253
|
+
std::string fmt = opts.Get("format").As<Napi::String>().Utf8Value();
|
|
254
|
+
if (fmt == "jpeg")
|
|
255
|
+
format = IMAGE_FORMAT_JPEG;
|
|
256
|
+
else if (fmt == "raw") {
|
|
257
|
+
mode = RENDER_IMAGE_RAW;
|
|
258
|
+
format = -1; // unused in raw mode
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (opts.Has("quality"))
|
|
262
|
+
quality = std::clamp(
|
|
263
|
+
opts.Get("quality").As<Napi::Number>().Int32Value(), 1, 100);
|
|
264
|
+
if (opts.Has("output"))
|
|
265
|
+
outputPath = opts.Get("output").As<Napi::String>().Utf8Value();
|
|
266
|
+
if (opts.Has("rendered") &&
|
|
267
|
+
opts.Get("rendered").As<Napi::Boolean>().Value() &&
|
|
268
|
+
mode != RENDER_IMAGE_RAW)
|
|
269
|
+
mode = RENDER_IMAGE_RENDERED;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
auto *worker =
|
|
273
|
+
new RenderImageWorker(env, page_, doc_, idx, format, quality,
|
|
274
|
+
std::move(outputPath), mode, alive_, docAlive_);
|
|
221
275
|
auto promise = worker->Promise();
|
|
222
276
|
worker->Queue();
|
|
223
277
|
return promise;
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "napi_helpers.h"
|
|
4
|
+
#include "render_worker.h" // stb_write_callback
|
|
5
|
+
|
|
6
|
+
#include <atomic>
|
|
7
|
+
#include <cstdio>
|
|
8
|
+
#include <filesystem>
|
|
9
|
+
#include <memory>
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
#include "fpdf_edit.h"
|
|
13
|
+
|
|
14
|
+
// max total pixels for image bitmap allocation (~256 MP, ~1 GB at 4 channels)
|
|
15
|
+
constexpr size_t MAX_IMAGE_PIXELS = 256 * 1024 * 1024;
|
|
16
|
+
|
|
17
|
+
// image render mode
|
|
18
|
+
enum RenderImageMode {
|
|
19
|
+
RENDER_IMAGE_BITMAP = 0, // FPDFImageObj_GetBitmap (intrinsic pixels)
|
|
20
|
+
RENDER_IMAGE_RENDERED =
|
|
21
|
+
1, // FPDFImageObj_GetRenderedBitmap (with mask/matrix)
|
|
22
|
+
RENDER_IMAGE_RAW = 2, // FPDFImageObj_GetImageDataRaw (original stream)
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
// RenderImageWorker — async image rendering from a page object
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
|
|
29
|
+
class RenderImageWorker : public Napi::AsyncWorker {
|
|
30
|
+
public:
|
|
31
|
+
RenderImageWorker(Napi::Env env, FPDF_PAGE page, FPDF_DOCUMENT doc,
|
|
32
|
+
int objectIndex, int format, int quality,
|
|
33
|
+
std::string outputPath, int mode,
|
|
34
|
+
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
35
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
36
|
+
: Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
37
|
+
page_(page), doc_(doc), objectIndex_(objectIndex), format_(format),
|
|
38
|
+
quality_(quality), outputPath_(std::move(outputPath)), mode_(mode),
|
|
39
|
+
pageAlive_(std::move(pageAlive)), docAlive_(std::move(docAlive)) {}
|
|
40
|
+
|
|
41
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
42
|
+
|
|
43
|
+
protected:
|
|
44
|
+
void Execute() override {
|
|
45
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
46
|
+
|
|
47
|
+
if (!pageAlive_->load() || (docAlive_ && !docAlive_->load())) {
|
|
48
|
+
SetError("Page or document was closed");
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (objectIndex_ < 0) {
|
|
53
|
+
SetError("Invalid object index");
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page_, objectIndex_);
|
|
58
|
+
if (!obj) {
|
|
59
|
+
SetError("Invalid object index");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (FPDFPageObj_GetType(obj) != FPDF_PAGEOBJ_IMAGE) {
|
|
63
|
+
SetError("Object at index " + std::to_string(objectIndex_) +
|
|
64
|
+
" is not an image");
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
bool ok;
|
|
69
|
+
if (mode_ == RENDER_IMAGE_RAW) {
|
|
70
|
+
ok = GetRawData(obj);
|
|
71
|
+
} else {
|
|
72
|
+
ok = RenderBitmap(obj);
|
|
73
|
+
}
|
|
74
|
+
if (!ok)
|
|
75
|
+
return;
|
|
76
|
+
|
|
77
|
+
// write to file if output path was specified
|
|
78
|
+
if (!outputPath_.empty()) {
|
|
79
|
+
if (!WriteToFile())
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
void OnOK() override {
|
|
85
|
+
Napi::Env env = Env();
|
|
86
|
+
if (outputPath_.empty()) {
|
|
87
|
+
auto data = Napi::Buffer<uint8_t>::Copy(env, encodedData_.data(),
|
|
88
|
+
encodedData_.size());
|
|
89
|
+
deferred_.Resolve(data);
|
|
90
|
+
} else {
|
|
91
|
+
deferred_.Resolve(env.Undefined());
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
void OnError(const Napi::Error &err) override {
|
|
96
|
+
deferred_.Reject(err.Value());
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private:
|
|
100
|
+
Napi::Promise::Deferred deferred_;
|
|
101
|
+
FPDF_PAGE page_;
|
|
102
|
+
FPDF_DOCUMENT doc_;
|
|
103
|
+
int objectIndex_;
|
|
104
|
+
int format_;
|
|
105
|
+
int quality_;
|
|
106
|
+
std::string outputPath_;
|
|
107
|
+
int mode_;
|
|
108
|
+
std::vector<uint8_t> encodedData_;
|
|
109
|
+
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
110
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
111
|
+
|
|
112
|
+
// get the raw encoded stream bytes (original JPEG, Flate, etc.)
|
|
113
|
+
bool GetRawData(FPDF_PAGEOBJECT obj) {
|
|
114
|
+
unsigned long len = FPDFImageObj_GetImageDataRaw(obj, nullptr, 0);
|
|
115
|
+
if (len == 0) {
|
|
116
|
+
SetError("Failed to get raw image data length");
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
encodedData_.resize(len);
|
|
120
|
+
unsigned long written =
|
|
121
|
+
FPDFImageObj_GetImageDataRaw(obj, encodedData_.data(), len);
|
|
122
|
+
if (written != len) {
|
|
123
|
+
SetError("Failed to read raw image data");
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// render via bitmap (either intrinsic or rendered) and encode
|
|
130
|
+
bool RenderBitmap(FPDF_PAGEOBJECT obj) {
|
|
131
|
+
FPDF_BITMAP bitmap = nullptr;
|
|
132
|
+
|
|
133
|
+
if (mode_ == RENDER_IMAGE_RENDERED) {
|
|
134
|
+
bitmap = FPDFImageObj_GetRenderedBitmap(doc_, page_, obj);
|
|
135
|
+
} else {
|
|
136
|
+
bitmap = FPDFImageObj_GetBitmap(obj);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (!bitmap) {
|
|
140
|
+
SetError("Failed to get image bitmap");
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
int width = FPDFBitmap_GetWidth(bitmap);
|
|
145
|
+
int height = FPDFBitmap_GetHeight(bitmap);
|
|
146
|
+
int stride = FPDFBitmap_GetStride(bitmap);
|
|
147
|
+
void *bufferData = FPDFBitmap_GetBuffer(bitmap);
|
|
148
|
+
int bitmapFormat = FPDFBitmap_GetFormat(bitmap);
|
|
149
|
+
|
|
150
|
+
if (!bufferData || width <= 0 || height <= 0) {
|
|
151
|
+
FPDFBitmap_Destroy(bitmap);
|
|
152
|
+
SetError("Failed to get bitmap buffer");
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (static_cast<size_t>(width) * static_cast<size_t>(height) >
|
|
157
|
+
MAX_IMAGE_PIXELS) {
|
|
158
|
+
FPDFBitmap_Destroy(bitmap);
|
|
159
|
+
SetError("Image dimensions too large (" + std::to_string(width) + "x" +
|
|
160
|
+
std::to_string(height) + ")");
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// determine source layout
|
|
165
|
+
int srcBytesPerPixel;
|
|
166
|
+
bool srcHasAlpha;
|
|
167
|
+
switch (bitmapFormat) {
|
|
168
|
+
case FPDFBitmap_Gray:
|
|
169
|
+
srcBytesPerPixel = 1;
|
|
170
|
+
srcHasAlpha = false;
|
|
171
|
+
break;
|
|
172
|
+
case FPDFBitmap_BGR:
|
|
173
|
+
srcBytesPerPixel = 3;
|
|
174
|
+
srcHasAlpha = false;
|
|
175
|
+
break;
|
|
176
|
+
case FPDFBitmap_BGRx:
|
|
177
|
+
srcBytesPerPixel = 4;
|
|
178
|
+
srcHasAlpha = false;
|
|
179
|
+
break;
|
|
180
|
+
case FPDFBitmap_BGRA:
|
|
181
|
+
srcBytesPerPixel = 4;
|
|
182
|
+
srcHasAlpha = true;
|
|
183
|
+
break;
|
|
184
|
+
default:
|
|
185
|
+
srcBytesPerPixel = 4;
|
|
186
|
+
srcHasAlpha = true;
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// output channels: PNG can carry alpha, JPEG cannot
|
|
191
|
+
bool keepAlpha = srcHasAlpha && format_ == IMAGE_FORMAT_PNG;
|
|
192
|
+
int outChannels =
|
|
193
|
+
(bitmapFormat == FPDFBitmap_Gray) ? 1 : (keepAlpha ? 4 : 3);
|
|
194
|
+
|
|
195
|
+
std::vector<uint8_t> pixels(static_cast<size_t>(width) *
|
|
196
|
+
static_cast<size_t>(height) * outChannels);
|
|
197
|
+
|
|
198
|
+
for (int y = 0; y < height; y++) {
|
|
199
|
+
auto *row = static_cast<uint8_t *>(bufferData) + y * stride;
|
|
200
|
+
for (int x = 0; x < width; x++) {
|
|
201
|
+
int dstIdx = (y * width + x) * outChannels;
|
|
202
|
+
|
|
203
|
+
if (bitmapFormat == FPDFBitmap_Gray) {
|
|
204
|
+
pixels[dstIdx] = row[x];
|
|
205
|
+
} else {
|
|
206
|
+
int srcIdx = x * srcBytesPerPixel;
|
|
207
|
+
pixels[dstIdx + 0] = row[srcIdx + 2]; // R ← B
|
|
208
|
+
pixels[dstIdx + 1] = row[srcIdx + 1]; // G
|
|
209
|
+
pixels[dstIdx + 2] = row[srcIdx + 0]; // B ← R
|
|
210
|
+
if (keepAlpha)
|
|
211
|
+
pixels[dstIdx + 3] = row[srcIdx + 3]; // A
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
FPDFBitmap_Destroy(bitmap);
|
|
217
|
+
|
|
218
|
+
// encode
|
|
219
|
+
int ok;
|
|
220
|
+
if (format_ == IMAGE_FORMAT_PNG) {
|
|
221
|
+
ok = stbi_write_png_to_func(stb_write_callback, &encodedData_, width,
|
|
222
|
+
height, outChannels, pixels.data(),
|
|
223
|
+
width * outChannels);
|
|
224
|
+
} else {
|
|
225
|
+
ok = stbi_write_jpg_to_func(stb_write_callback, &encodedData_, width,
|
|
226
|
+
height, outChannels, pixels.data(), quality_);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (!ok) {
|
|
230
|
+
SetError("Failed to encode image");
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
bool WriteToFile() {
|
|
237
|
+
std::filesystem::path outPath(outputPath_);
|
|
238
|
+
auto parentDir = outPath.parent_path();
|
|
239
|
+
if (!parentDir.empty() && !std::filesystem::is_directory(parentDir)) {
|
|
240
|
+
SetError("Parent directory does not exist: " + parentDir.string());
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
FILE *f = fopen(outputPath_.c_str(), "wb");
|
|
245
|
+
if (!f) {
|
|
246
|
+
SetError("Failed to open output file: " + outputPath_);
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
size_t written = fwrite(encodedData_.data(), 1, encodedData_.size(), f);
|
|
250
|
+
fclose(f);
|
|
251
|
+
if (written != encodedData_.size()) {
|
|
252
|
+
SetError("Failed to write output file: " + outputPath_);
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
encodedData_.clear();
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
};
|