跳至页脚内容

如何在 Node.js 中生成 PDF 文件

通过观看我们的教程,充分发挥在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

相关视频

准备开始了吗?
版本: 2025.11 刚刚发布