PDF 工具

如何在NodeJS中将PDF转换为图像

将PDF文档转换为PNG、JPG、GIF等图像格式在各种应用中可以是一个有价值的功能,从文档管理系统到图像处理软件。 在本文中,我们将学习如何使用 Node.js 将 PDF 转换为图像文件。 我们将利用一个名为pdf-poppler的流行npm(Node Package Manager)软件包的强大功能来完成此任务。

先决条件

首先,确保您的计算机上已安装 Node.js 和npm(Node 包管理器)。您可以通过在命令提示符(cmd)中运行以下命令来检查 Node 的安装情况:

node --version
npm --version
node --version
npm --version
NODE.JS

如果尚未安装,您需要从 Node.js 网站下载

如何在 NodeJS 中将 PDF 转换为图像:图 1 - Node.js 模块

设置项目

要开始,请为您的项目创建一个新的目录。 在本教程中,让我们将此目录命名为NodeJS_PDFtoImage。 接下来,在命令提示符中导航到该目录,并通过运行以下命令初始化一个新的 Node.js 项目:

npm init -y
npm init -y
NODE.JS

运行上述命令会生成一个 package.json 文件,允许我们安装项目所需的依赖项。

安装依赖项

我们将使用的依赖项是pdf-poppler,这是一个提供易于使用的API的包,用于将PDF转换为图像。

通过在 Windows PowerShell 或命令提示符中运行以下命令来安装它:

npm install pdf-poppler
npm install pdf-poppler
NODE.JS

完成! 让我们编写将PDF转换为图像的逻辑。

将PDF转换为图像文件

安装完成后,在项目的根目录下创建一个新文件,并将其命名为pdfToImage.js。 在您首选的文本编辑器中打开文件,并添加所需的模块:

const pdfPoppler = require('pdf-poppler');
const pdfPoppler = require('pdf-poppler');
NODE.JS

下面显示了一个28页的PDF文件示例。

如何在 NodeJS 中将 PDF 转换为图像:图2 - 输入文件

接下来,定义一个名为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);
    //log message
    console.log('PDF converted to image successfully!');
  } catch (error) {
    console.error('Error converting PDF to image:', error);
  }
}
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);
    //log message
    console.log('PDF converted to image successfully!');
  } catch (error) {
    console.error('Error converting PDF to image:', error);
  }
}
NODE.JS

该函数使用pdfPoppler软件包将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);
const pdfPath = '/path/to/input.pdf';
const outputPath = '/path/to/output/folder';

convertPdfToImage(pdfPath, outputPath);
NODE.JS

注意: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);
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.js 脚本:

node pdfToImage.js
node pdfToImage.js
NODE.JS

这将运行Node.js脚本,并使用pdf-poppler将PDF转换为图像文件。

如何在 NodeJS 中将 PDF 转换为图像:图 3 - Node.js 脚本

输出文件夹

如何在 NodeJS 中将 PDF 转换为图像:图 4 - 输出

在 C&num 中将 PDF 文件栅格化为图像;

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

如何在NodeJS中将PDF转换为图像:图5 - Node JS PDF到图像的输出

使用IronPDF将PDF转换为图像文件就是如此简单。 有关将PDF转换为图像的更多详细信息,请访问此代码示例页面

结论

在本文中,我们探讨了如何使用pdf-poppler包在Node.js中将PDF文件转换为图片。 通过遵循概述的步骤,您可以在您的Node.js应用中集成PDF到图像的转换功能,使您能够以编程方式处理和操控PDF文档,实现多种可能性。

另一方面,IronPDF 是一个功能强大的 C# 库,能够简化 PDF 操作和转换任务。 其将PDF转换为图像的能力为以编程方式提取图像或生成PDF页面的图像表示提供了一种便利的方法。 通过利用IronPDF的功能,开发人员可以将PDF转图像转换功能无缝集成到他们的C#应用中。

IronPDF 在开发期间是免费的,可用于商业用途需要获得许可。 此外,您还可以使用免费试用进行商业模式的使用。

Chipego
软件工程师
Chipego 拥有出色的倾听技巧,这帮助他理解客户问题并提供智能解决方案。他在 2023 年加入 Iron Software 团队,此前他获得了信息技术学士学位。IronPDF 和 IronOCR 是 Chipego 主要专注的两个产品,但他对所有产品的了解每天都在增长,因为他不断找到支持客户的新方法。他喜欢 Iron Software 的合作氛围,公司各地的团队成员贡献他们丰富的经验,以提供有效的创新解决方案。当 Chipego 离开办公桌时,你经常可以发现他在看书或踢足球。
< 前一页
如何在Chrome中打开PDF文件
下一步 >
如何在 C++ 中查看 PDF 文件