PDFツール

NodeJSでPDFを画像に変換する方法

PDFドキュメントをPNG、JPG、GIFなどの画像形式に変換する機能は、ドキュメント管理システムから画像処理ソフトウェアに至るまで、様々なアプリケーションで価値のある機能となります。 この記事では、Node.jsを使用してPDFを画像ファイルに変換する方法を学びます。 このタスクを達成するために、人気のnpm (Node Package Manager) パッケージであるpdf-popplerの力を活用します。

前提条件

まず、Node.js とnpm(Node Package Manager)がマシンにインストールされていることを確認してください。Node のインストールを確認するには、コマンドプロンプト(cmd)で以下のコマンドを実行してください:

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を画像に変換するための使いやすいAPIを提供するパッケージであるpdf-popplerです。

次のコマンドを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 - 入力ファイル

次に、PDFファイルのパスpdfPathと出力ディレクトリのパスoutputPathを受け取るconvertPdfToImageという名前の関数を定義します。 この関数は、サンプルの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

この関数は、PDF を JPEG 画像形式に変換するために pdfPoppler パッケージを使用します。 この場合、format オプションを 'JPEG' に設定していますが、'PNG' や 'TIFF' など他のフォーマットを選択することもできます。 out_dir オプションは出力画像が保存されるディレクトリを指定し、out_prefix は出力画像ファイルのプレフィックスを設定します。 page オプションを使用すると、変換する特定のページを指定することができます。また、すべてのページを変換するには null のままにしておくことができます。

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#でPDFファイルをラスタイメージに変換

C# .NET 用 IronPDF

IronPDF は、C# 開発者が即座に PDF ドキュメントを処理できる多用途な .NET ライブラリです。 それは、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はPDF操作および変換タスクを支援する強力なC#ライブラリです。 PDFを画像に変換する機能は、画像を抽出したり、PDFページの画像表現をプログラムで生成する便利な方法を提供します。 IronPDFの機能を活用することにより、開発者はC#にPDFから画像への変換機能をシームレスに統合できます。

IronPDFは開発には無料で使用でき、商用利用にはライセンスが必要です。 さらに、無料トライアルを使用して商用モードでも利用できます。

チペゴ
ソフトウェアエンジニア
チペゴは優れた傾聴能力を持ち、それが顧客の問題を理解し、賢明な解決策を提供する助けとなっています。彼は情報技術の学士号を取得後、2023年にIron Softwareチームに加わりました。現在、彼はIronPDFとIronOCRの2つの製品に注力していますが、顧客をサポートする新しい方法を見つけるにつれて、他の製品に関する知識も日々成長しています。Iron Softwareでの協力的な生活を楽しんでおり、さまざまな経験を持つチームメンバーが集まり、効果的で革新的な解決策を提供することに貢献しています。チペゴがデスクを離れているときは、良い本を楽しんだり、サッカーをしていることが多いです。
< 以前
ChromeでPDFファイルを開く方法
次へ >
C++でPDFファイルを表示する方法