1.1.0

建構參數

最後更新


本頁列出 new pdf2image(options) 的所有參數、預設值與正規化規則。

簽章


  1. new pdf2image(options?: {
  2. file?: ArrayBuffer;
  3. filename?: string;
  4. scale?: number;
  5. type?: "png" | "jpg" | "webp";
  6. })

建構子只儲存參數,不讀取檔案、不觸發任何網路請求。

參數


參數型別預設值正規化說明
fileArrayBufferundefined無PDF 原始資料;轉換後會被 pdf.js 轉移而失效,見 轉換流程
filenamestring""trim() 後移除第一個 .pdfZIP 與圖片的基礎檔名,支援 yyyy/MM/DD/hh/mm,見 ZIP 下載
scalenumber1.5falsy 值(含 0)回退為 1.5pdf.js viewport 縮放倍率;輸出像素 = 頁面 pt × scale
typestring"jpg"非 png/jpg/webp 回退為 "jpg"輸出格式;"jpg" 實際產生 PNG,見 轉換流程

正規化範例


  1. <script>
  2. const size = src => new Promise(r => {
  3. const img = new Image();
  4. img.onload = () => r(img.width + "x" + img.height);
  5. img.src = src;
  6. });
  7. addEventListener("load", async () => {
  8. const options = [{}, { scale: 0 }, { type: "gif" }, { type: "PNG" }];
  9. for (const extra of options) {
  10. const buffer = await (await fetch("/assets/sample.pdf")).arrayBuffer();
  11. const [first] = await new pdf2image({ file: buffer, ...extra }).convert();
  12. console.log(JSON.stringify(extra), "->", first.slice(5, first.indexOf(";")), await size(first));
  13. }
  14. });
  15. </script>
即時預覽 · @pardnchiu/pdf2image@1.1.2

type 比對區分大小寫,"PNG" 也會回退為 "jpg"。
filename 只能是字串或省略:建構子直接對它呼叫 .trim(),傳入非字串的 truthy 值(例如 123)會在 new pdf2image() 時拋出 TypeError。

相關頁面


EN