node-pdf2img 0.1.6 → 0.1.7

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 CHANGED
@@ -157,6 +157,32 @@ const result = await convert('./document.pdf', {
157
157
  });
158
158
  ```
159
159
 
160
+ ### pages 参数说明
161
+
162
+ `pages` 参数控制要转换的页面:
163
+
164
+ - **空数组 `[]` 或不传**:转换所有页面
165
+ - **指定页码数组**:只转换指定页面(1-based)
166
+
167
+ ```javascript
168
+ // 转换所有页面(推荐)
169
+ await convert('./document.pdf', { pages: [] });
170
+
171
+ // 只转换第 1、3、5 页
172
+ await convert('./document.pdf', { pages: [1, 3, 5] });
173
+ ```
174
+
175
+ > **最佳实践**:如果需要转换所有页面,直接使用 `convert()` 并传空 `pages` 数组,**不要**先调用 `getPageCount()` 获取页数再传入。这样可以避免 URL 输入时重复下载 PDF 文件。
176
+ >
177
+ > ```javascript
178
+ > // ❌ 不推荐:URL 会被下载两次
179
+ > const pageCount = await getPageCount(url);
180
+ > const result = await convert(url, { pages: Array.from({length: pageCount}, (_, i) => i + 1) });
181
+ >
182
+ > // ✅ 推荐:直接转换所有页面
183
+ > const result = await convert(url, { pages: [] });
184
+ > ```
185
+
160
186
  ### 自定义渲染宽度
161
187
 
162
188
  ```javascript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-pdf2img",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
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.7"
58
+ "node-pdf2img-native": "^1.1.8"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@types/node": "^20.0.0"
@@ -517,7 +517,7 @@ export async function convert(input, options = {}) {
517
517
  /**
518
518
  * 获取 PDF 页数(异步版本)
519
519
  *
520
- * @param {string|Buffer} input - PDF 输入(文件路径或 Buffer)
520
+ * @param {string|Buffer} input - PDF 输入(文件路径、URL Buffer)
521
521
  * @returns {Promise<number>} 页数
522
522
  */
523
523
  export async function getPageCount(input) {
@@ -530,6 +530,23 @@ export async function getPageCount(input) {
530
530
  }
531
531
 
532
532
  if (typeof input === 'string') {
533
+ // 检查是否是 URL
534
+ if (input.startsWith('http://') || input.startsWith('https://')) {
535
+ // URL 输入:下载到临时文件后获取页数
536
+ const tempFile = await downloadToTempFile(input);
537
+ try {
538
+ return nativeRenderer.getPageCountFromFile(tempFile);
539
+ } finally {
540
+ // 清理临时文件
541
+ try {
542
+ await fs.promises.unlink(tempFile);
543
+ } catch {
544
+ // 忽略清理错误
545
+ }
546
+ }
547
+ }
548
+
549
+ // 本地文件路径
533
550
  try {
534
551
  await fs.promises.access(input, fs.constants.R_OK);
535
552
  } catch {
@@ -538,7 +555,7 @@ export async function getPageCount(input) {
538
555
  return nativeRenderer.getPageCountFromFile(input);
539
556
  }
540
557
 
541
- throw new Error('Invalid input: must be a file path or Buffer');
558
+ throw new Error('Invalid input: must be a file path, URL, or Buffer');
542
559
  }
543
560
 
544
561
  /**
package/src/index.d.ts CHANGED
@@ -92,10 +92,10 @@ export function convert(input: string | Buffer, options?: ConvertOptions): Promi
92
92
  /**
93
93
  * 获取 PDF 页数
94
94
  *
95
- * @param input - PDF 文件路径或 Buffer
95
+ * @param input - PDF 文件路径、URL Buffer
96
96
  * @returns 页数
97
97
  */
98
- export function getPageCount(input: string | Buffer): number;
98
+ export function getPageCount(input: string | Buffer): Promise<number>;
99
99
 
100
100
  /**
101
101
  * 检查原生渲染器是否可用