在 Node.js 中如何生成 PDF 文件 Darrius Serrant 更新日期:8月 19, 2025 Download IronPDF npm 下載 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 透過觀看我們的教學,您將學會如何使用 IronPDF 將 HTML、網頁和圖片轉換成動態 PDF,並發現這個強大的工具如何簡化您的開發流程-立即訂閱並開始免費試用,釋放您的 Node.js 應用程式中生成 PDF 的全部潛能! 更多... 在本綜合教學中,我們將深入探討使用 IronPDF 函式庫在 Node.js 中產生 PDF 檔案。 我們首先安裝必要的套件並設定環境。 本教學涵蓋了重要模組的匯入,包括 PDF Generator、PDF Document 和 IronPDF Global Config,以及 Node.js 的 FS 模組。 我們透過將一個簡單的「Hello World」HTML 標籤轉換成 PDF 來示範從 HTML 字串建立 PDF。 接下來,我們探索將維基百科中關於 PDF 的網頁 URL 轉換成 PDF 文件,展現 IronPdf 擷取網頁細節的能力。 本教學還涵蓋了透過篩選目錄中的 JPEG 並使用 PDF Generator 的 image-to-PDF 功能將影像轉換為單一 PDF。 通過在終端運行代碼,我們成功地生成並審查了 PDF,展示了 IronPDF 將各種來源轉換為 PDF 文件的能力。 這款功能強大的工具可大幅提升 Node.js 應用程式中產生 PDF 的可能性。 如需瞭解更多教學並探索 IronPDF 的功能,請訂閱 Iron Software 的頻道,並註冊試用,親身體驗該軟體。# Bash code to install the IronPDF library in your Node.js project.npm install ironpdf # Bash code to install the IronPDF library in your Node.js project.npm install ironpdf SHELL // Import necessary modules from IronPDF and Node.jsconst { PDFGenerator, PDFDocument, IronPDFGlobalConfig } = require('ironpdf');const fs = require('fs'); // File system module for handling file operations// Initialize PDF Generatorconst pdfGenerator = new PDFGenerator();async function generatePdfFromHtml() { // HTML content to be converted into a PDF const htmlContent = '<h1>Hello World</h1>'; try { // Convert HTML string to a PDF document const pdfDoc = await pdfGenerator.fromHtml(htmlContent); // Save the generated PDF document to a file await pdfDoc.saveAs('hello_world.pdf'); console.log('PDF generated successfully from HTML string.'); } catch (error) { console.error('Error generating PDF from HTML:', error); }}async function generatePdfFromWebPage() { // URL of the web page to be converted into a PDF const url = 'https://en.wikipedia.org/wiki/PDF'; try { // Convert web page URL to a PDF document const pdfDoc = await pdfGenerator.fromUrl(url); // Save the generated PDF document to a file await pdfDoc.saveAs('wikipedia_pdf.pdf'); console.log('PDF generated successfully from webpage URL.'); } catch (error) { console.error('Error generating PDF from webpage:', error); }}async function generatePdfFromImages() { // Directory containing images const imageDir = './images'; const imageFiles = fs.readdirSync(imageDir); try { // Filter only JPEG files from the directory const jpegImages = imageFiles.filter(file => file.endsWith('.jpeg')); // Convert filtered images to a single PDF document const pdfDoc = await pdfGenerator.fromImages(jpegImages.map(img => `${imageDir}/${img}`)); // Save the generated PDF document to a file await pdfDoc.saveAs('images.pdf'); console.log('PDF generated successfully from images.'); } catch (error) { console.error('Error generating PDF from images:', error); }}// Execute the functions to generate PDFsgeneratePdfFromHtml();generatePdfFromWebPage();generatePdfFromImages(); // Import necessary modules from IronPDF and Node.jsconst { PDFGenerator, PDFDocument, IronPDFGlobalConfig } = require('ironpdf');const fs = require('fs'); // File system module for handling file operations// Initialize PDF Generatorconst pdfGenerator = new PDFGenerator();async function generatePdfFromHtml() { // HTML content to be converted into a PDF const htmlContent = '<h1>Hello World</h1>'; try { // Convert HTML string to a PDF document const pdfDoc = await pdfGenerator.fromHtml(htmlContent); // Save the generated PDF document to a file await pdfDoc.saveAs('hello_world.pdf'); console.log('PDF generated successfully from HTML string.'); } catch (error) { console.error('Error generating PDF from HTML:', error); }}async function generatePdfFromWebPage() { // URL of the web page to be converted into a PDF const url = 'https://en.wikipedia.org/wiki/PDF'; try { // Convert web page URL to a PDF document const pdfDoc = await pdfGenerator.fromUrl(url); // Save the generated PDF document to a file await pdfDoc.saveAs('wikipedia_pdf.pdf'); console.log('PDF generated successfully from webpage URL.'); } catch (error) { console.error('Error generating PDF from webpage:', error); }}async function generatePdfFromImages() { // Directory containing images const imageDir = './images'; const imageFiles = fs.readdirSync(imageDir); try { // Filter only JPEG files from the directory const jpegImages = imageFiles.filter(file => file.endsWith('.jpeg')); // Convert filtered images to a single PDF document const pdfDoc = await pdfGenerator.fromImages(jpegImages.map(img => `${imageDir}/${img}`)); // Save the generated PDF document to a file await pdfDoc.saveAs('images.pdf'); console.log('PDF generated successfully from images.'); } catch (error) { console.error('Error generating PDF from images:', error); }}// Execute the functions to generate PDFsgeneratePdfFromHtml();generatePdfFromWebPage();generatePdfFromImages(); JAVASCRIPT
更新日期 8月 19, 2025 如何在 Node.js 中將 PDF 轉換為圖像文件 這個視頻教程指導您使用 Iron PDF 在 JavaScript 中將 PDF 文件轉換為圖像文件。學習如何導入 PDF 文件,提取頁面,並將其作為 BMP 圖像保存。學會如何轉換特定的 PDF 頁,並以高質量圖像增強您的應用程式的能力。 閱讀更多
更新日期 9月 22, 2025 在 Node.js 中不使用 Puppeteer 將 HTML 轉換為 PDF | IronPDF 探索一個全面的指南,使用 IronPDF 在 Node.js 中將 HTML 轉換為 PDF。此教程提供一個逐步的方法,提供了一個看似能替代 Puppeteer 的高效 PDF 生成途徑。 閱讀更多
更新日期 8月 19, 2025 如何在 Nodejs 中簽署 PDF 文件 本教程提供了在 Node.js 環境中使用 Iron PDF 庫簽署 PDFs的綜合指南。它涵蓋了安裝、配置和執行,讓用戶能輕易在 PDF 文件中添加數字簽名。 閱讀更多
更新日期 8月 19, 2025 如何在 Node.js 中讀取 PDF 文件 該教程指導開發者如何使用 Node.js 讀取 PDFs 並利用 Iron PDF 庫。內容包括安裝過程、導入必要的模塊,並展示用於從 PDF 文件抽取文本和元數據的异步函數。探索在 Node.js 應用程式中處理 PDFs 的簡單方法。 閱讀更多
更新日期 8月 19, 2025 在 Node.js 中不使用 Puppeteer 將 HTML 轉換為 PDF 本教程探討採用 Iron PDF 庫在 Node.js 中進行 HTML 到 PDF 的轉換,避免使用 Puppeteer。內容涵蓋安裝、HTML 字串的基本轉換、將網頁轉換等更複雜的操作,以及處理離線 HTML 文件。指南突出 Iron PDF 的簡便性和多樣性。 閱讀更多
更新日期 8月 19, 2025 在 Node.js 中如何將 HTML 轉換為 PDF 此教程指導您如何在 Node.js 中使用 Iron PDF 將 HTML 轉換為 PDF。學習安裝必要的套件、設置您的環境,并從 HTML 字串、文件和網頁創建 PDF。發現 Iron PDF 的多樣性以生成高質量的 PDF。 閱讀更多