node-pdf2img 0.1.7 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -9
- package/bin/cli.js +36 -20
- package/package.json +8 -3
- package/src/core/config.js +9 -0
- package/src/core/converter.js +78 -372
- package/src/core/downloader.js +98 -0
- package/src/core/output-handler.js +179 -0
- package/src/core/renderer.js +289 -0
- package/src/core/thread-pool.js +78 -0
- package/src/index.d.ts +182 -49
- package/src/index.js +18 -4
- package/src/renderers/pdfjs.js +867 -0
package/src/index.d.ts
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @tencent/pdf2img - 高性能 PDF 转图片工具
|
|
3
|
+
*
|
|
4
|
+
* 支持两种渲染器:
|
|
5
|
+
* - pdfium: PDFium 原生渲染器(默认,高性能)
|
|
6
|
+
* - pdfjs: PDF.js 渲染器(纯 JavaScript,无需原生依赖)
|
|
3
7
|
*/
|
|
4
8
|
|
|
9
|
+
/** 渲染器类型常量 */
|
|
10
|
+
export const RendererType: {
|
|
11
|
+
/** PDFium 原生渲染器(默认,高性能) */
|
|
12
|
+
PDFIUM: 'pdfium';
|
|
13
|
+
/** PDF.js 渲染器(纯 JavaScript,无需原生依赖) */
|
|
14
|
+
PDFJS: 'pdfjs';
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** 渲染器类型 */
|
|
18
|
+
export type RendererTypeValue = 'pdfium' | 'pdfjs';
|
|
19
|
+
|
|
5
20
|
export interface RenderOptions {
|
|
6
21
|
/** 目标渲染宽度(像素),默认:1280 */
|
|
7
22
|
targetWidth?: number;
|
|
@@ -9,10 +24,29 @@ export interface RenderOptions {
|
|
|
9
24
|
imageHeavyWidth?: number;
|
|
10
25
|
/** 最大渲染缩放比例,默认:4.0 */
|
|
11
26
|
maxScale?: number;
|
|
12
|
-
/** WebP 质量 0-100,默认:
|
|
27
|
+
/** WebP 质量 0-100,默认:80 */
|
|
13
28
|
webpQuality?: number;
|
|
14
29
|
/** 启用扫描件检测,默认:true */
|
|
15
30
|
detectScan?: boolean;
|
|
31
|
+
/** 渲染器:'pdfium'(默认)或 'pdfjs' */
|
|
32
|
+
renderer?: RendererTypeValue;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface WebpOptions {
|
|
36
|
+
/** WebP 质量 0-100,默认:80 */
|
|
37
|
+
quality?: number;
|
|
38
|
+
/** WebP 编码方法 0-6,默认:4,0最快6最慢 */
|
|
39
|
+
method?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface JpegOptions {
|
|
43
|
+
/** JPEG 质量 0-100,默认:85 */
|
|
44
|
+
quality?: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface PngOptions {
|
|
48
|
+
/** PNG 压缩级别 0-9,默认:6 */
|
|
49
|
+
compressionLevel?: number;
|
|
16
50
|
}
|
|
17
51
|
|
|
18
52
|
export interface CosConfig {
|
|
@@ -24,6 +58,12 @@ export interface CosConfig {
|
|
|
24
58
|
bucket: string;
|
|
25
59
|
/** COS 地域 */
|
|
26
60
|
region: string;
|
|
61
|
+
/** 协议,默认:'https:' */
|
|
62
|
+
protocol?: string;
|
|
63
|
+
/** 服务域名 */
|
|
64
|
+
serviceDomain?: string;
|
|
65
|
+
/** 自定义域名 */
|
|
66
|
+
domain?: string;
|
|
27
67
|
}
|
|
28
68
|
|
|
29
69
|
export interface ConvertOptions extends RenderOptions {
|
|
@@ -35,10 +75,22 @@ export interface ConvertOptions extends RenderOptions {
|
|
|
35
75
|
outputDir?: string;
|
|
36
76
|
/** 输出文件名前缀,默认:'page' */
|
|
37
77
|
prefix?: string;
|
|
78
|
+
/** 输出格式:'webp'、'png'、'jpg',默认:'webp' */
|
|
79
|
+
format?: 'webp' | 'png' | 'jpg' | 'jpeg';
|
|
80
|
+
/** 图片质量 0-100(用于 webp 和 jpg) */
|
|
81
|
+
quality?: number;
|
|
82
|
+
/** WebP 编码配置 */
|
|
83
|
+
webp?: WebpOptions;
|
|
84
|
+
/** JPEG 编码配置 */
|
|
85
|
+
jpeg?: JpegOptions;
|
|
86
|
+
/** PNG 编码配置 */
|
|
87
|
+
png?: PngOptions;
|
|
38
88
|
/** COS 配置(outputType 为 'cos' 时必需) */
|
|
39
89
|
cos?: CosConfig;
|
|
40
90
|
/** COS key 前缀 */
|
|
41
91
|
cosKeyPrefix?: string;
|
|
92
|
+
/** 文件/上传并发数 */
|
|
93
|
+
concurrency?: number;
|
|
42
94
|
}
|
|
43
95
|
|
|
44
96
|
export interface PageResult {
|
|
@@ -60,6 +112,25 @@ export interface PageResult {
|
|
|
60
112
|
size?: number;
|
|
61
113
|
/** 错误信息(失败时) */
|
|
62
114
|
error?: string;
|
|
115
|
+
/** 渲染耗时(毫秒) */
|
|
116
|
+
renderTime?: number;
|
|
117
|
+
/** 编码耗时(毫秒) */
|
|
118
|
+
encodeTime?: number;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface StreamStats {
|
|
122
|
+
/** 请求次数 */
|
|
123
|
+
requestCount?: number;
|
|
124
|
+
/** 总请求次数 */
|
|
125
|
+
totalRequests?: number;
|
|
126
|
+
/** 总下载字节数 */
|
|
127
|
+
totalBytes?: number;
|
|
128
|
+
/** 总下载大小(MB) */
|
|
129
|
+
totalBytesMB?: string;
|
|
130
|
+
/** 平均请求耗时(毫秒) */
|
|
131
|
+
avgRequestTime?: number;
|
|
132
|
+
/** 加载模式 */
|
|
133
|
+
mode?: string;
|
|
63
134
|
}
|
|
64
135
|
|
|
65
136
|
export interface ConvertResult {
|
|
@@ -69,15 +140,33 @@ export interface ConvertResult {
|
|
|
69
140
|
numPages: number;
|
|
70
141
|
/** 成功渲染的页数 */
|
|
71
142
|
renderedPages: number;
|
|
143
|
+
/** 输出格式 */
|
|
144
|
+
format: string;
|
|
145
|
+
/** 使用的渲染器 */
|
|
146
|
+
renderer: RendererTypeValue;
|
|
72
147
|
/** 页面结果数组 */
|
|
73
148
|
pages: PageResult[];
|
|
74
149
|
/** 耗时信息 */
|
|
75
150
|
timing: {
|
|
76
151
|
/** 总耗时(毫秒) */
|
|
77
152
|
total: number;
|
|
78
|
-
/**
|
|
79
|
-
|
|
153
|
+
/** 渲染耗时(毫秒) */
|
|
154
|
+
render: number;
|
|
155
|
+
/** 编码耗时(毫秒) */
|
|
156
|
+
encode: number;
|
|
80
157
|
};
|
|
158
|
+
/** 线程池信息 */
|
|
159
|
+
threadPool: {
|
|
160
|
+
/** 工作线程数 */
|
|
161
|
+
workers: number;
|
|
162
|
+
};
|
|
163
|
+
/** 流式渲染统计(仅 URL 输入时存在) */
|
|
164
|
+
streamStats?: StreamStats;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export interface GetPageCountOptions {
|
|
168
|
+
/** 渲染器:'pdfium'(默认)或 'pdfjs' */
|
|
169
|
+
renderer?: RendererTypeValue;
|
|
81
170
|
}
|
|
82
171
|
|
|
83
172
|
/**
|
|
@@ -86,6 +175,15 @@ export interface ConvertResult {
|
|
|
86
175
|
* @param input - PDF 文件路径、URL 或 Buffer
|
|
87
176
|
* @param options - 转换选项
|
|
88
177
|
* @returns 转换结果
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```javascript
|
|
181
|
+
* // 使用默认 pdfium 渲染器
|
|
182
|
+
* const result = await convert('./document.pdf');
|
|
183
|
+
*
|
|
184
|
+
* // 使用 PDF.js 渲染器
|
|
185
|
+
* const result = await convert('./document.pdf', { renderer: 'pdfjs' });
|
|
186
|
+
* ```
|
|
89
187
|
*/
|
|
90
188
|
export function convert(input: string | Buffer, options?: ConvertOptions): Promise<ConvertResult>;
|
|
91
189
|
|
|
@@ -93,19 +191,45 @@ export function convert(input: string | Buffer, options?: ConvertOptions): Promi
|
|
|
93
191
|
* 获取 PDF 页数
|
|
94
192
|
*
|
|
95
193
|
* @param input - PDF 文件路径、URL 或 Buffer
|
|
194
|
+
* @param options - 选项(可指定渲染器)
|
|
96
195
|
* @returns 页数
|
|
97
196
|
*/
|
|
98
|
-
export function getPageCount(input: string | Buffer): Promise<number>;
|
|
197
|
+
export function getPageCount(input: string | Buffer, options?: GetPageCountOptions): Promise<number>;
|
|
99
198
|
|
|
100
199
|
/**
|
|
101
|
-
*
|
|
200
|
+
* 获取 PDF 页数(同步版本)
|
|
201
|
+
*
|
|
202
|
+
* @deprecated 使用 getPageCount 的异步版本以获得更好的性能
|
|
203
|
+
* @param input - PDF 文件路径或 Buffer
|
|
204
|
+
* @returns 页数
|
|
102
205
|
*/
|
|
103
|
-
export function
|
|
206
|
+
export function getPageCountSync(input: string | Buffer): number;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 检查渲染器是否可用
|
|
210
|
+
*
|
|
211
|
+
* @param renderer - 渲染器类型(可选)。不传则检查是否有任何可用渲染器
|
|
212
|
+
* @returns 是否可用
|
|
213
|
+
*/
|
|
214
|
+
export function isAvailable(renderer?: RendererTypeValue): boolean;
|
|
104
215
|
|
|
105
216
|
/**
|
|
106
217
|
* 获取版本信息
|
|
218
|
+
*
|
|
219
|
+
* @param renderer - 渲染器类型(可选)
|
|
220
|
+
* @returns 版本信息
|
|
221
|
+
*/
|
|
222
|
+
export function getVersion(renderer?: RendererTypeValue): string;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 获取线程池统计信息
|
|
226
|
+
*/
|
|
227
|
+
export function getThreadPoolStats(): object | null;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 销毁线程池
|
|
107
231
|
*/
|
|
108
|
-
export function
|
|
232
|
+
export function destroyThreadPool(): Promise<void>;
|
|
109
233
|
|
|
110
234
|
/** 输入类型常量 */
|
|
111
235
|
export const InputType: {
|
|
@@ -126,7 +250,7 @@ export const RENDER_CONFIG: {
|
|
|
126
250
|
TARGET_RENDER_WIDTH: number;
|
|
127
251
|
IMAGE_HEAVY_TARGET_WIDTH: number;
|
|
128
252
|
MAX_RENDER_SCALE: number;
|
|
129
|
-
|
|
253
|
+
OUTPUT_FORMAT: string;
|
|
130
254
|
NATIVE_STREAM_THRESHOLD: number;
|
|
131
255
|
};
|
|
132
256
|
|
|
@@ -136,51 +260,60 @@ export const TIMEOUT_CONFIG: {
|
|
|
136
260
|
DOWNLOAD_TIMEOUT: number;
|
|
137
261
|
};
|
|
138
262
|
|
|
263
|
+
/** 默认渲染器 */
|
|
264
|
+
export const DEFAULT_RENDERER: RendererTypeValue;
|
|
265
|
+
|
|
266
|
+
// ==================== 原生渲染器导出 ====================
|
|
267
|
+
|
|
139
268
|
/** 检查原生渲染器是否可用 */
|
|
140
269
|
export function isNativeAvailable(): boolean;
|
|
141
270
|
|
|
142
|
-
/**
|
|
143
|
-
export function
|
|
144
|
-
|
|
145
|
-
|
|
271
|
+
/** 获取 PDF 页数(原生渲染器) */
|
|
272
|
+
export function getPageCountNative(pdfBuffer: Buffer): number;
|
|
273
|
+
|
|
274
|
+
/** 获取 PDF 页数(从文件路径,原生渲染器) */
|
|
275
|
+
export function getPageCountFromFile(filePath: string): number;
|
|
276
|
+
|
|
277
|
+
/** 渲染单页到原始位图 */
|
|
278
|
+
export function renderPageToRawBitmap(
|
|
279
|
+
filePath: string,
|
|
280
|
+
pageNum: number,
|
|
146
281
|
options?: RenderOptions
|
|
147
|
-
):
|
|
282
|
+
): {
|
|
148
283
|
success: boolean;
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
nativeTime: number;
|
|
162
|
-
}>;
|
|
163
|
-
|
|
164
|
-
/** 从流渲染 PDF(用于远程 URL) */
|
|
165
|
-
export function renderFromStream(
|
|
166
|
-
pdfUrl: string,
|
|
167
|
-
pdfSize: number,
|
|
168
|
-
pages?: number[],
|
|
284
|
+
buffer?: Buffer;
|
|
285
|
+
width?: number;
|
|
286
|
+
height?: number;
|
|
287
|
+
channels?: number;
|
|
288
|
+
renderTime?: number;
|
|
289
|
+
error?: string;
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
/** 从 Buffer 渲染单页到原始位图 */
|
|
293
|
+
export function renderPageToRawBitmapFromBuffer(
|
|
294
|
+
pdfBuffer: Buffer,
|
|
295
|
+
pageNum: number,
|
|
169
296
|
options?: RenderOptions
|
|
170
|
-
):
|
|
297
|
+
): {
|
|
171
298
|
success: boolean;
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
299
|
+
buffer?: Buffer;
|
|
300
|
+
width?: number;
|
|
301
|
+
height?: number;
|
|
302
|
+
channels?: number;
|
|
303
|
+
renderTime?: number;
|
|
304
|
+
error?: string;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// ==================== PDF.js 渲染器导出 ====================
|
|
308
|
+
|
|
309
|
+
/** 检查 PDF.js 渲染器是否可用 */
|
|
310
|
+
export function isPdfjsAvailable(): boolean;
|
|
311
|
+
|
|
312
|
+
/** 获取 PDF.js 版本信息 */
|
|
313
|
+
export function getPdfjsVersion(): string;
|
|
314
|
+
|
|
315
|
+
/** 获取 PDF 页数(PDF.js 渲染器) */
|
|
316
|
+
export function getPageCountPdfjs(pdfBuffer: Buffer): Promise<number>;
|
|
317
|
+
|
|
318
|
+
/** 获取 PDF 页数(从文件路径,PDF.js 渲染器) */
|
|
319
|
+
export function getPageCountFromFilePdfjs(filePath: string): Promise<number>;
|
package/src/index.js
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @tencent/pdf2img - 高性能 PDF 转图片工具
|
|
3
3
|
*
|
|
4
|
-
* 使用 PDFium
|
|
4
|
+
* 使用 PDFium 原生渲染器(默认)或 PDF.js 渲染器实现。
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* ```javascript
|
|
8
|
-
* import { convert, getPageCount, isAvailable } from '@tencent/pdf2img';
|
|
8
|
+
* import { convert, getPageCount, isAvailable, RendererType } from '@tencent/pdf2img';
|
|
9
9
|
*
|
|
10
|
-
* // 转换 PDF 为图片(返回 Buffer
|
|
10
|
+
* // 转换 PDF 为图片(返回 Buffer,使用默认 pdfium 渲染器)
|
|
11
11
|
* const result = await convert('./document.pdf');
|
|
12
12
|
* console.log(`转换了 ${result.renderedPages} 页`);
|
|
13
13
|
*
|
|
14
|
+
* // 使用 PDF.js 渲染器(无需原生依赖)
|
|
15
|
+
* const result = await convert('./document.pdf', {
|
|
16
|
+
* renderer: 'pdfjs', // 或 RendererType.PDFJS
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
14
19
|
* // 保存到文件
|
|
15
20
|
* const result = await convert('./document.pdf', {
|
|
16
21
|
* outputType: 'file',
|
|
@@ -44,9 +49,10 @@ export {
|
|
|
44
49
|
destroyThreadPool,
|
|
45
50
|
InputType,
|
|
46
51
|
OutputType,
|
|
52
|
+
RendererType,
|
|
47
53
|
} from './core/converter.js';
|
|
48
54
|
|
|
49
|
-
export { RENDER_CONFIG, TIMEOUT_CONFIG } from './core/config.js';
|
|
55
|
+
export { RENDER_CONFIG, TIMEOUT_CONFIG, DEFAULT_RENDERER } from './core/config.js';
|
|
50
56
|
|
|
51
57
|
// 导出原生渲染器工具供高级用法
|
|
52
58
|
export {
|
|
@@ -56,3 +62,11 @@ export {
|
|
|
56
62
|
renderPageToRawBitmap,
|
|
57
63
|
renderPageToRawBitmapFromBuffer,
|
|
58
64
|
} from './renderers/native.js';
|
|
65
|
+
|
|
66
|
+
// 导出 PDF.js 渲染器
|
|
67
|
+
export {
|
|
68
|
+
isPdfjsAvailable,
|
|
69
|
+
getPdfjsVersion,
|
|
70
|
+
getPageCount as getPageCountPdfjs,
|
|
71
|
+
getPageCountFromFile as getPageCountFromFilePdfjs,
|
|
72
|
+
} from './renderers/pdfjs.js';
|