如何在 Node.js 中生成 PDF 文件 Darrius Serrant 已更新:八月 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 通过观看我们的教程,充分发挥在Node.js应用程序中生成PDF的潜力。在教程中,您将学习如何使用IronPDF将HTML、网页和图像转换为动态PDF,并发现这一强大工具如何简化开发过程——立即订阅并开始您的免费试用吧! 更多... 在这个全面的教程中,我们深入探讨使用IronPDF库在Node.js中生成PDF文件。 我们首先安装必要的包并设置环境。 教程涵盖了导入必要的模块,包括PDF生成器、PDF文档和IronPDF全局配置,以及Node.js的FS模块。 我们演示如何将HTML字符串转换为PDF,通过将一个简单的‘Hello World’HTML标签转换为PDF。 接下来,我们探讨将关于PDF的Wikipedia页面网址转换为PDF文档,展示IronPDF捕捉网页细节的能力。 教程还涵盖了通过从目录中过滤出JPEG并使用PDF生成器的图像到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
已更新八月 19, 2025 如何在 Node.js 中将 PDF 转换为图像文件 这段视频教程指导你通过使用 Iron PDF 在 JavaScript 中将 PDF 文件转换为图像文件。学习如何导入PDF文档、提取页面,并将其保存为BMP图像。了解如何转换特定PDF页面,并通过高质量图像增强您的应用。 阅读更多
已更新九月 22, 2025 在不使用 Pupateer 的情况下在 Node.js 中将 HTML 转换为 PDF | IronPDF 探索一个关于使用 IronPDF 将 HTML 转换为 PDF 的 Node.js 的全面指南。本教程提供了逐步方法,提供可靠的 Puppeteer 替代方案,以实现高效的 PDF 生成。 阅读更多
已更新八月 19, 2025 如何在 Node.js 中签名 PDF 文件 本教程提供了在 Node.js 环境中使用 Iron PDF 库签署 PDF 的综合指南。它涵盖安装、配置和执行,允许用户轻松向其 PDF 文档添加数字签名。 阅读更多
已更新八月 19, 2025 如何在 Node.js 中读取 PDF 文件 此教程指导开发者如何在 Node.js 中使用 Iron PDF Library 阅读 PDF。它涵盖安装过程、导入必要模块,并演示了从 PDF 文件中提取文本和元数据的异步函数。探索在 Node.js 应用程序中处理 PDF 的简单方法。 阅读更多
已更新八月 19, 2025 如何在不使用 Puppeteer 的情况下在 Node.js 中将 HTML 转换为 PDF 本教程探讨了如何通过 Iron PDF 库在 Node.js 中转换 HTML 为 PDF,避免使用 Puppeteer。它涵盖安装、从 HTML 字符串进行基本转换及更复杂的任务如转换网页,以及处理离线 HTML 文件。指南强调了 Iron PDF 的简单性和多功能性。 阅读更多
已更新八月 19, 2025 如何在 Node.js 中将 HTML 转换为 PDF 本教程指导您通过在 Node.js 中使用 Iron PDF 将 HTML 转换为 PDF。学习安装必要的包,设置环境,以及从 HTML 字符串、文件和网页创建 PDF。探索 Iron PDF 的多功能性以生成专业品质的 PDF。 阅读更多