PDF工具 如何在NodeJS中將PDF轉換為圖像 Curtis Chau 更新日期:6月 22, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 將 PDF 文件轉換為 PNG、JPG、GIF 等圖像格式在各種應用中都是一個有價值的功能,從文件管理系統到圖像處理軟件均適用。 在本文中,我們將學習如何使用 Node.js 將 PDF 轉換為圖像文件。 我們將利用一個名為 pdf-poppler 的熱門 npm(Node Package Manager)包來完成這項任務。 準備工作 首先,確保您的機器上已安裝 Node.js 和 npm (Node Package Manager)。您可以在命令提示符(cmd)中運行以下命令來檢查 Node 安裝情況: node --version npm --version node --version npm --version SHELL 如果未安裝,您需要從 Node.js 網站下載。 設置項目 要開始,為您的項目創建一個新目錄。 對於本教程,讓我們將此目錄命名為 NodeJS_PDFtoImage。接下來,在命令提示符中導航到該目錄並初始化一個新的 Node.js 項目,運行: npm init -y npm init -y SHELL 運行上述命令將生成一個 package.json 文件,這將允許我們安裝項目所需的依賴項。 安裝依賴項 我們將使用的依賴項是 pdf-poppler,這是一個提供簡單 API 以將 PDF 轉換為圖像的包。 通過在 Windows PowerShell 或命令提示符中運行以下命令來安裝它: npm install pdf-poppler npm install pdf-poppler SHELL 一切準備就緒! 讓我們寫出將 PDF 轉換為圖像的邏輯。 將 PDF 轉換為圖像文件 安裝完成後,創建一個新的文件,位於我們項目的根目錄,並將其命名為 pdfToImage.js。 在您首選的文本編輯器中打開該文件,並添加所需的模塊: const pdfPoppler = require('pdf-poppler'); 下面是顯示的一個28頁的PDF文件示例。 接下來,定義一個名為 convertPdfToImage 的函數,該函數接收 PDF 文件路徑(pdfPath)和輸出目錄路徑(outputPath)。 此函數將我們的示例 PDF 文檔轉換為圖像。 async function convertPdfToImage(pdfPath, outputPath) { const options = { format: 'jpeg', // You can choose other formats like png or tiff out_dir: outputPath, out_prefix: 'page', page: null // Specify the page number here to convert a specific page, otherwise null to convert all pages }; try { await pdfPoppler.convert(pdfPath, options); console.log('PDF converted to image successfully!'); } catch (error) { console.error('Error converting PDF to image:', error); } } 該函數使用 pdf-poppler 包將 PDF 轉換為 JPEG 圖像格式。 在這種情況下,我們將 format 選項設置為 'jpeg',但您可以選擇其他格式,如 'png' 或 'tiff'。 out_dir 選項指定輸出圖像將被保存的目錄,而 out_prefix 則為輸出圖像文件設置一個前綴。 page 選項允許您指定要轉換的特定頁面,或將其留空為空以轉換所有頁面。 要將 PDF 文件轉換為圖像,您可以使用適當的文件路徑調用 convertPdfToImage 函數。 例如: const pdfPath = '/path/to/input.pdf'; const outputPath = '/path/to/output/folder'; convertPdfToImage(pdfPath, outputPath); 注意: 將 pdfPath 值 "/path/to/input.pdf" 替換為輸入 PDF 文件的實際路徑,並將 "/path/to/output/folder" 替換為所需的輸出目錄路徑。 完整代碼如下: const pdfPoppler = require('pdf-poppler'); const pdfPath = 'C:\\Users\\hp\\Desktop\\NodeJS_PDFtoImage\\pdf_files\\input.pdf'; const outputDir = 'C:\\Users\\hp\\Desktop\\NodeJS_PDFtoImage\\pdf_images'; async function convertPdfToImage(pdfPath, outputPath) { const opts = { format: 'jpeg', // You can choose other formats like png or tiff out_dir: outputPath, out_prefix: 'page', page: null // Specify the page number here to convert a specific page, otherwise null to convert all pages }; try { await pdfPoppler.convert(pdfPath, opts); console.log('PDF converted to image successfully!'); } catch (error) { console.error('Error converting PDF to image:', error); } } convertPdfToImage(pdfPath, outputDir); 執行 Node.js 腳本 通過執行以下命令來運行 Node.js 腳本: node pdfToImage.js node pdfToImage.js SHELL 這將運行 Node.js 腳本並使用 pdf-poppler 將 PDF 轉換為圖像文件。 輸出文件夾 將 PDF 文件光柵化為 C# 圖像文件 IronPDF for C# .NET IronPDF 是一個多功能的 .NET 庫,允許 C# 開發者隨時處理 PDF 文檔。 它提供了全面的功能,用於創建、操作和轉換 C# 中的 PDF 文件。 IronPDF 提供了一種方便的方式,使用 C# 將 PDF 文檔轉換為圖像文件。 當需要程序化地從 PDF 文件中提取圖像或生成圖像縮略圖時,這一功能特別有用。 要使用 IronPDF 進行圖像轉換,您可以按照下面的代碼段中的步驟進行操作: using IronPdf; using IronSoftware.Drawing; var pdf = PdfDocument.FromFile("input.pdf"); // Extract all pages to a folder as image files pdf.RasterizeToImageFiles(@"C:\image\folder\*.png"); // Dimensions and page ranges may be specified pdf.RasterizeToImageFiles(@"C:\image\folder\example_pdf_image_*.jpg", 100, 80); // Extract all pages as AnyBitmap objects AnyBitmap [] pdfBitmaps = pdf.ToBitmap(); using IronPdf; using IronSoftware.Drawing; var pdf = PdfDocument.FromFile("input.pdf"); // Extract all pages to a folder as image files pdf.RasterizeToImageFiles(@"C:\image\folder\*.png"); // Dimensions and page ranges may be specified pdf.RasterizeToImageFiles(@"C:\image\folder\example_pdf_image_*.jpg", 100, 80); // Extract all pages as AnyBitmap objects AnyBitmap [] pdfBitmaps = pdf.ToBitmap(); Imports IronPdf Imports IronSoftware.Drawing Private pdf = PdfDocument.FromFile("input.pdf") ' Extract all pages to a folder as image files pdf.RasterizeToImageFiles("C:\image\folder\*.png") ' Dimensions and page ranges may be specified pdf.RasterizeToImageFiles("C:\image\folder\example_pdf_image_*.jpg", 100, 80) ' Extract all pages as AnyBitmap objects Dim pdfBitmaps() As AnyBitmap = pdf.ToBitmap() $vbLabelText $csharpLabel 這就是使用 IronPDF 將 PDF 轉換為圖像文件的簡單方法。 有關 PDF 到圖像轉換的更多詳細信息,請訪問此代碼示例頁面。 結論 在本文中,我們探討了如何在 Node.js 中使用 pdf-poppler 包將 PDF 文件轉換為圖像。 通過遵循所列步驟,您可以將 PDF 到圖像轉換功能集成到您的 Node.js 應用程序中,從而實現程序化處理和操作 PDF 文檔的廣泛可能性。 另一方面,IronPDF 是一個功能強大的 C# 庫,旨在促進 PDF 的操作和轉換任務。 其將 PDF 轉換為圖像的能力,提供了一種方便的方法,以程序化地提取圖像或生成 PDF 頁面的圖像表示。 通過利用 IronPDF 的功能,開發者可以無縫地將 PDF 到圖像的轉換功能集成到其 C# 應用中。 IronPDF 可免費用於開發,也可用於商業用途。 此外,您還可以使用免費試用模式進行商業使用。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新日期 6月 22, 2025 發現2025年最佳PDF刪除軟件 探索2025年最佳PDF刪除解決方案,包括Adobe Acrobat Pro DC、Nitro PDF Pro、Foxit PDF Editor和PDF-XChange Editor。了解IronPDF如何自動化.NET中的刪除以增強安全性和合規性。 閱讀更多 更新日期 6月 22, 2025 iPhone上最佳PDF閱讀器(免費和付費工具比較) 在本文中,我們將探索一些適合iPhone的最佳PDF閱讀器,並總結為何IronPDF脫穎而出成為最佳選擇。 閱讀更多 更新日期 6月 26, 2025 Windows上最佳免費PDF編輯器(免費和付費工具比較) 本文探討2025年可用的頂級免費PDF編輯器,並總結出最強大和靈活的選擇:IronPDF。 閱讀更多 如何在Chrome中打開PDF文件如何在C++中查看PDF文件
更新日期 6月 22, 2025 發現2025年最佳PDF刪除軟件 探索2025年最佳PDF刪除解決方案,包括Adobe Acrobat Pro DC、Nitro PDF Pro、Foxit PDF Editor和PDF-XChange Editor。了解IronPDF如何自動化.NET中的刪除以增強安全性和合規性。 閱讀更多
更新日期 6月 22, 2025 iPhone上最佳PDF閱讀器(免費和付費工具比較) 在本文中,我們將探索一些適合iPhone的最佳PDF閱讀器,並總結為何IronPDF脫穎而出成為最佳選擇。 閱讀更多