node-pdf2img 0.1.5 → 0.1.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-pdf2img",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "High-performance PDF to image converter using PDFium native renderer",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -55,7 +55,7 @@
55
55
  "p-limit": "^7.2.0",
56
56
  "piscina": "^5.1.4",
57
57
  "sharp": "^0.33.0",
58
- "node-pdf2img-native": "^1.1.6"
58
+ "node-pdf2img-native": "^1.1.7"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@types/node": "^20.0.0"
@@ -424,6 +424,59 @@ export async function convert(input, options = {}) {
424
424
  // 使用线程池渲染页面
425
425
  const result = await renderPages(input, inputType, pages, encodeOptions);
426
426
 
427
+ // 恢复 Buffer 类型
428
+ // Piscina 跨线程传递时 Buffer 可能被序列化为普通对象 { type: 'Buffer', data: [...] }
429
+ const normalizedPages = result.pages.map(page => {
430
+ if (!page.success || !page.buffer) {
431
+ return {
432
+ pageNum: page.pageNum,
433
+ width: page.width,
434
+ height: page.height,
435
+ success: false,
436
+ buffer: null,
437
+ error: page.error || 'Render failed',
438
+ };
439
+ }
440
+
441
+ let buffer = page.buffer;
442
+ if (!Buffer.isBuffer(buffer)) {
443
+ try {
444
+ if (buffer && typeof buffer === 'object') {
445
+ if (buffer.type === 'Buffer' && Array.isArray(buffer.data)) {
446
+ buffer = Buffer.from(buffer.data);
447
+ } else if (buffer.data && ArrayBuffer.isView(buffer.data)) {
448
+ buffer = Buffer.from(buffer.data);
449
+ } else if (ArrayBuffer.isView(buffer)) {
450
+ buffer = Buffer.from(buffer);
451
+ } else {
452
+ buffer = Buffer.from(buffer);
453
+ }
454
+ } else {
455
+ throw new Error(`Cannot convert ${typeof buffer} to Buffer`);
456
+ }
457
+ } catch (e) {
458
+ logger.error(`Buffer type mismatch: ${typeof page.buffer}, conversion failed: ${e.message}`);
459
+ return {
460
+ pageNum: page.pageNum,
461
+ width: page.width,
462
+ height: page.height,
463
+ success: false,
464
+ buffer: null,
465
+ error: `Invalid buffer type returned from worker: ${e.message}`,
466
+ };
467
+ }
468
+ }
469
+
470
+ return {
471
+ pageNum: page.pageNum,
472
+ width: page.width,
473
+ height: page.height,
474
+ success: true,
475
+ buffer,
476
+ size: buffer.length,
477
+ };
478
+ });
479
+
427
480
  // 处理输出
428
481
  let outputResult;
429
482
 
@@ -431,72 +484,17 @@ export async function convert(input, options = {}) {
431
484
  if (!outputDir) {
432
485
  throw new Error('outputDir is required when outputType is "file"');
433
486
  }
434
- outputResult = await saveToFiles(result.pages, outputDir, prefix, normalizedFormat, concurrency);
487
+ outputResult = await saveToFiles(normalizedPages, outputDir, prefix, normalizedFormat, concurrency);
435
488
 
436
489
  } else if (outputType === OutputType.COS) {
437
490
  if (!cosConfig) {
438
491
  throw new Error('cos config is required when outputType is "cos"');
439
492
  }
440
- outputResult = await uploadToCos(result.pages, cosConfig, cosKeyPrefix, normalizedFormat, concurrency);
493
+ outputResult = await uploadToCos(normalizedPages, cosConfig, cosKeyPrefix, normalizedFormat, concurrency);
441
494
 
442
495
  } else {
443
496
  // 返回 Buffer
444
- outputResult = result.pages.map(page => {
445
- if (!page.success || !page.buffer) {
446
- return {
447
- pageNum: page.pageNum,
448
- width: page.width,
449
- height: page.height,
450
- success: false,
451
- buffer: null,
452
- error: page.error || 'Render failed',
453
- };
454
- }
455
-
456
- // 确保 buffer 是 Buffer 类型
457
- // Piscina 跨线程传递时 Buffer 可能被序列化为普通对象
458
- let buffer = page.buffer;
459
- if (!Buffer.isBuffer(buffer)) {
460
- // 尝试从序列化的对象恢复 Buffer
461
- try {
462
- if (buffer && typeof buffer === 'object') {
463
- // 可能是 { type: 'Buffer', data: [...] } 格式
464
- if (buffer.type === 'Buffer' && Array.isArray(buffer.data)) {
465
- buffer = Buffer.from(buffer.data);
466
- } else if (buffer.data && ArrayBuffer.isView(buffer.data)) {
467
- buffer = Buffer.from(buffer.data);
468
- } else if (ArrayBuffer.isView(buffer)) {
469
- // Uint8Array 等 TypedArray
470
- buffer = Buffer.from(buffer);
471
- } else {
472
- // 最后尝试直接转换
473
- buffer = Buffer.from(buffer);
474
- }
475
- } else {
476
- throw new Error(`Cannot convert ${typeof buffer} to Buffer`);
477
- }
478
- } catch (e) {
479
- logger.error(`Buffer type mismatch: ${typeof page.buffer}, conversion failed: ${e.message}`);
480
- return {
481
- pageNum: page.pageNum,
482
- width: page.width,
483
- height: page.height,
484
- success: false,
485
- buffer: null,
486
- error: `Invalid buffer type returned from worker: ${e.message}`,
487
- };
488
- }
489
- }
490
-
491
- return {
492
- pageNum: page.pageNum,
493
- width: page.width,
494
- height: page.height,
495
- success: true,
496
- buffer,
497
- size: buffer.length,
498
- };
499
- }).sort((a, b) => a.pageNum - b.pageNum);
497
+ outputResult = normalizedPages.sort((a, b) => a.pageNum - b.pageNum);
500
498
  }
501
499
 
502
500
  return {