JAVA向けIRONPDFの使用

JavaライブラリPDF生成(完全なコード例)

この記事では、JavaでPDFを作成するための優れたツールであるIronPDFライブラリについて説明します。

IronPDF:Java PDFライブラリ

IronPDFは、開発者が簡単にPDFドキュメントやPDFフォームを作成したり、PDFファイルにデジタル署名をしたりできる、人気のJava PDFライブラリです。 IronPDFを使用すると、既存のPDFドキュメントをテンプレートとして使用して新しいPDFファイルを生成したり、PDFデータをデータベースに保存して将来使用したり、PDFをHTMLなどの他の形式に変換したり、複数のPDFを1つにマージしたりできます。

IronPDFは、ユーザーが作成したファイルをパーソナライズするためにPDFにテキスト注釈を追加することを可能にします。 さらに、IronPDFを使用すると、PDF内にパスワードや透かしといったセキュリティ設定を含めることができます。 JavaプログラムにPDF機能を統合するのに役立ちます。 IronPDFは、PDFを迅速かつ安全に生成するための非常に多様で強力なツールです。 IronPDFを使用してPDFファイルを作成する方法を見てみましょう。

IronPDFを使用してPDFファイルを生成

IronPDFはPDFファイルを作成するための非常に貴重なツールです。 必要なすべての機能が備わっており、ドキュメント、ウェブページ、画像を迅速に安定した安全なPDFに変換し、簡単に共有できます。 このデモプログラムにIronPDFをインストールしましょう。

IronPDF Java PDFライブラリをインストール

MavenプロジェクトにIronPDF Javaをインストールするには、次の依存関係をプロジェクトのpom.xmlファイルに追加することができます:

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>com.ironsoftware</artifactId>
   <version>2025.5.6</version>
</dependency>

これは、IronPDF for Javaライブラリと、それが使用するSLF4Jロガーを追加します。 最新バージョンのIronPDF for Javaを使用することをお勧めします。 依存関係を追加したら、mvn install を実行してローカルリポジトリに依存関係をインストールすると、プロジェクトでIronPDF for Javaを使用する準備が整います。

PDFドキュメントを作成するためのJavaコード

このコードはJavaで書かれており、IronPDFライブラリを使用してHTMLをPDFドキュメントに変換します。

// Import statement for IronPDF Java  
import com.ironsoftware.ironpdf.*;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Apply your license key
        License.setLicenseKey("YOUR-LICENSE-KEY");
        // Set a log path  
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
        // Render the HTML as a PDF. Stored in myPdf as type PdfDocument  
        String html = "<!DOCTYPE html>\r\n"
                + "<html>\r\n"
                + "  <head>\r\n"
                + "    <link href='https://fonts.googleapis.com/css2?family=Popin&display=swap' rel='stylesheet'>\r\n"
                + "    <style>\r\n"
                + "      /* Add CSS styles for the invoice here */\r\n"
                + "      body{\r\n"
                + "        font-family: 'Popin', cursive;\r\n"
                + "      }\r\n"
                + "      .invoice {\r\n"
                + "        width: 80%;\r\n"
                + "        margin: 0 auto;\r\n"
                + "        border: 1px solid #ccc;\r\n"
                + "        padding: 20px;\r\n"
                + "        background-color: #f5f5f5;\r\n"
                + "        color: #333;\r\n"
                + "      }\r\n"
                + "      .invoice h1 {\r\n"
                + "        text-align: center;\r\n"
                + "      }\r\n"
                + "      .invoice .invoice-info {\r\n"
                + "        display: flex;\r\n"
                + "        justify-content: space-between;\r\n"
                + "        margin-bottom: 20px;\r\n"
                + "      }\r\n"
                + "      .invoice .invoice-info div {\r\n"
                + "        width: 45%;\r\n"
                + "      }\r\n"
                + "      .invoice table {\r\n"
                + "        width: 100%;\r\n"
                + "        border-collapse: collapse;\r\n"
                + "      }\r\n"
                + "      .invoice table th, .invoice table td {\r\n"
                + "        border: 1px solid #ccc;\r\n"
                + "        padding: 10px;\r\n"
                + "      }\r\n"
                + "      .invoice table th {\r\n"
                + "        text-align: left;\r\n"
                + "        background-color: #f5f5f5;\r\n"
                + "      }\r\n"
                + "      .invoice table td {\r\n"
                + "        text-align: right;\r\n"
                + "      }\r\n"
                + "      .invoice table td.total {\r\n"
                + "        font-weight: bold;\r\n"
                + "      }\r\n"
                + "    </style>\r\n"
                + "  </head>\r\n"
                + "  <body>\r\n"
                + "    <div class=\"invoice\">\r\n"
                + "      <h1>Invoice</h1>\r\n"
                + "      <div class=\"invoice-info\">\r\n"
                + "        <div>\r\n"
                + "          <p><strong>From:</strong></p>\r\n"
                + "          <p>Your Company Name</p>\r\n"
                + "          <p>123 Main St</p>\r\n"
                + "          <p>City, State ZIP</p>\r\n"
                + "        </div>\r\n"
                + "        <div>\r\n"
                + "          <p><strong>To:</strong></p>\r\n"
                + "          <p>Customer Name</p>\r\n"
                + "          <p>456 Park Ave</p>\r\n"
                + "          <p>City, State ZIP</p>\r\n"
                + "        </div>\r\n"
                + "      </div>\r\n"
                + "      <table>\r\n"
                + "        <thead>\r\n"
                + "          <tr>\r\n"
                + "            <th>Product</th>\r\n"
                + "            <th>Quantity</th>\r\n"
                + "            <th>Price</th>\r\n"
                + "            <th>Total</th>\r\n"
                + "          </tr>\r\n"
                + "        </thead>\r\n"
                + "        <tbody>\r\n"
                + "          <tr>\r\n"
                + "            <td>Product 1</td>\r\n"
                + "            <td>1</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "          </tr>\r\n"
                + "          <tr>\r\n"
                + "            <td>Product 2</td>\r\n"
                + "            <td>2</td>\r\n"
                + "            <td>$5.00</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "          </tr>\r\n"
                + "          <tr>\r\n"
                + "            <td colspan=\"3\" class=\"total\">Total:</td>\r\n"
                + "            <td class=\"total\">$20.00</td>\r\n"
                + "          </tr>\r\n"
                + "        </tbody>\r\n"
                + "      </table>\r\n"
                + "    </div>\r\n"
                + "  </body>\r\n"
                + "</html>";

        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(html);
        //Save PDF document
        myPdf.saveAs(Paths.get("C://HTMLtoPDF.pdf"));
    }
}
// Import statement for IronPDF Java  
import com.ironsoftware.ironpdf.*;

import java.io.IOException;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) throws IOException {
        // Apply your license key
        License.setLicenseKey("YOUR-LICENSE-KEY");
        // Set a log path  
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));
        // Render the HTML as a PDF. Stored in myPdf as type PdfDocument  
        String html = "<!DOCTYPE html>\r\n"
                + "<html>\r\n"
                + "  <head>\r\n"
                + "    <link href='https://fonts.googleapis.com/css2?family=Popin&display=swap' rel='stylesheet'>\r\n"
                + "    <style>\r\n"
                + "      /* Add CSS styles for the invoice here */\r\n"
                + "      body{\r\n"
                + "        font-family: 'Popin', cursive;\r\n"
                + "      }\r\n"
                + "      .invoice {\r\n"
                + "        width: 80%;\r\n"
                + "        margin: 0 auto;\r\n"
                + "        border: 1px solid #ccc;\r\n"
                + "        padding: 20px;\r\n"
                + "        background-color: #f5f5f5;\r\n"
                + "        color: #333;\r\n"
                + "      }\r\n"
                + "      .invoice h1 {\r\n"
                + "        text-align: center;\r\n"
                + "      }\r\n"
                + "      .invoice .invoice-info {\r\n"
                + "        display: flex;\r\n"
                + "        justify-content: space-between;\r\n"
                + "        margin-bottom: 20px;\r\n"
                + "      }\r\n"
                + "      .invoice .invoice-info div {\r\n"
                + "        width: 45%;\r\n"
                + "      }\r\n"
                + "      .invoice table {\r\n"
                + "        width: 100%;\r\n"
                + "        border-collapse: collapse;\r\n"
                + "      }\r\n"
                + "      .invoice table th, .invoice table td {\r\n"
                + "        border: 1px solid #ccc;\r\n"
                + "        padding: 10px;\r\n"
                + "      }\r\n"
                + "      .invoice table th {\r\n"
                + "        text-align: left;\r\n"
                + "        background-color: #f5f5f5;\r\n"
                + "      }\r\n"
                + "      .invoice table td {\r\n"
                + "        text-align: right;\r\n"
                + "      }\r\n"
                + "      .invoice table td.total {\r\n"
                + "        font-weight: bold;\r\n"
                + "      }\r\n"
                + "    </style>\r\n"
                + "  </head>\r\n"
                + "  <body>\r\n"
                + "    <div class=\"invoice\">\r\n"
                + "      <h1>Invoice</h1>\r\n"
                + "      <div class=\"invoice-info\">\r\n"
                + "        <div>\r\n"
                + "          <p><strong>From:</strong></p>\r\n"
                + "          <p>Your Company Name</p>\r\n"
                + "          <p>123 Main St</p>\r\n"
                + "          <p>City, State ZIP</p>\r\n"
                + "        </div>\r\n"
                + "        <div>\r\n"
                + "          <p><strong>To:</strong></p>\r\n"
                + "          <p>Customer Name</p>\r\n"
                + "          <p>456 Park Ave</p>\r\n"
                + "          <p>City, State ZIP</p>\r\n"
                + "        </div>\r\n"
                + "      </div>\r\n"
                + "      <table>\r\n"
                + "        <thead>\r\n"
                + "          <tr>\r\n"
                + "            <th>Product</th>\r\n"
                + "            <th>Quantity</th>\r\n"
                + "            <th>Price</th>\r\n"
                + "            <th>Total</th>\r\n"
                + "          </tr>\r\n"
                + "        </thead>\r\n"
                + "        <tbody>\r\n"
                + "          <tr>\r\n"
                + "            <td>Product 1</td>\r\n"
                + "            <td>1</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "          </tr>\r\n"
                + "          <tr>\r\n"
                + "            <td>Product 2</td>\r\n"
                + "            <td>2</td>\r\n"
                + "            <td>$5.00</td>\r\n"
                + "            <td>$10.00</td>\r\n"
                + "          </tr>\r\n"
                + "          <tr>\r\n"
                + "            <td colspan=\"3\" class=\"total\">Total:</td>\r\n"
                + "            <td class=\"total\">$20.00</td>\r\n"
                + "          </tr>\r\n"
                + "        </tbody>\r\n"
                + "      </table>\r\n"
                + "    </div>\r\n"
                + "  </body>\r\n"
                + "</html>";

        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(html);
        //Save PDF document
        myPdf.saveAs(Paths.get("C://HTMLtoPDF.pdf"));
    }
}
JAVA

最初のステップは、setLicenseKey メソッドを使用してライセンスキーを適用することです。 鍵は文字列引数として渡されます。 この場合、"YOUR-LICENSE-KEY" を実際のライセンスキーで置き換える必要があります。

次のステップは、setLogPath メソッドを使用してログパスを設定することです。 これは、IronPDFエンジンのログファイルが保存される場所です。 この場合、それは "C:/tmp/IronPdfEngine.log" に設定されています。

メインメソッドが定義され、PdfDocumentオブジェクトが、HTML文字列を引数として渡してrenderHtmlAsPdfメソッドを呼び出すことで作成されます。 これはHTMLをPDFに変換し、それをmyPdfオブジェクトに格納します。

最後のステップは、saveAs) メソッドを使用して、myPdf オブジェクトをファイルに保存することです。 ファイルの場所は、Pathsオブジェクトの形式で引数として渡されます。この場合、「HTMLtoPDF.pdf」です。

ここでは、IronPDF Java PDF ライブラリを使用してPDFファイルが作成されるプログラムの出力を見ることができます。

JavaライブラリPDF生成 (完全なコード例), 図1: HTML文字列からの出力PDFファイル

HTML文字列からの出力PDFファイル

URLからPDFファイルを作成する

IronPDFは、ローカルネットワークや外部サーバーを含むさまざまなソースからウェブページをPDFにレンダリングできます。

import com.ironsoftware.ironpdf.*;

import java.io.IOException;
import java.nio.file.Paths;

// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");

// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");

// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("url.pdf"));
import com.ironsoftware.ironpdf.*;

import java.io.IOException;
import java.nio.file.Paths;

// Apply your license key
License.setLicenseKey("YOUR-LICENSE-KEY");

// Set a log path
Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

// Render the HTML as a PDF. Stored in myPdf as type PdfDocument;
PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");

// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("url.pdf"));
JAVA

PdfDocument.renderUrlAsPdf メソッドは、この目的のために特別に設計されており、変換するWebページのURLを含む文字列を受け入れます。 そのメソッドはウェブページのHTMLコンテンツを取得し、PDFドキュメントに変換します。 IronPDFは、すべてのWebコンポーネントの外観を保持しながら、インタラクティブ機能(リンク、フォームフィールドなど)を機能させます。

結果は以下の通りです:

Java ライブラリ PDF 生成 (完全なコード例), 図 2: URL からの出力 PDF ファイル

URLから取得した出力PDFファイル

サマリー

結論として、IronPDFは、PDFファイルの作成および操作に多くの機能を備えた価値のあるJavaライブラリです。 PDFドキュメントをデジタル署名する必要がある場合や、PDFフォームに記入する、その他のタスクを実行する必要がある場合でも、IronPDFは最小限のコーディングで簡単にこれを実現できます。

無料トライアル利用可能、$749から始まる柔軟な価格設定オプションにより、IronPDFはプロジェクトにPDF機能を追加したい開発者にとってコスト効率の良いソリューションです。

Darrius Serrant
フルスタックソフトウェアエンジニア(WebOps)

Darrius Serrantは、マイアミ大学でコンピュータサイエンスの学士号を取得しており、Iron SoftwareでフルスタックWebOpsマーケティングエンジニアとして働いています。若い頃からコーディングに魅了され、コンピューティングを神秘的でありながらアクセスしやすいものと見なし、それが創造性と問題解決のための完璧な媒体であると感じました。

Iron Softwareでは、新しいものを作り出し、複雑な概念を簡単にすることでより理解しやすくすることを楽しんでいます。彼は常駐の開発者の一人として、学生に教えることを志願し、自分の専門知識を次世代と共有しています。

Darriusにとって、彼の仕事は評価され、実際に影響があることで充実しています。

< 以前
HTML2PDF Java(コード例チュートリアル)
次へ >
JavaでPDFを生成する方法