フッターコンテンツにスキップ
JAVA用IRONPDFの使用

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

この記事では、IronPDFライブラリについて探ります。JavaでPDFを作成する素晴らしいツールです。

IronPDF: Java PDF Library

IronPDFは、開発者が簡単にPDFドキュメント、PDFフォームを作成し、PDFファイルにデジタル署名をすることができる人気のあるJava PDFライブラリです。 With IronPDF, you can use existing PDF documents as templates to generate new PDF files, store PDF data in databases for future use, convert PDFs into other formats like HTML, and even merge multiple PDFs into one.

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

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

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

Install IronPDF Java PDF library

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

<dependencies>
    <!-- Add IronPDF dependency -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>
    <!-- Add SLF4J logging dependency -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>
</dependencies>
<dependencies>
    <!-- Add IronPDF dependency -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>
    <!-- Add SLF4J logging dependency -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>YOUR-VERSION-HERE</version>
    </dependency>
</dependencies>
XML

これにより、IronPDF for Javaライブラリとその使用するSLF4Jロガーが追加されます。 Java用のIronPDFの最新バージョンを使用することが推奨されます。 依存関係を追加したら、mvn installを実行して依存関係をローカルリポジトリにインストールし、プロジェクトでIronPDF for Javaを使用できるようになります。

Java Code for Creating PDF documents

このコードは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 to store log files generated by IronPDF
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Define the HTML content to convert into a PDF
        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>";

        // Convert HTML to PDF document
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(html);

        // Save the PDF document to a specified path
        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 to store log files generated by IronPDF
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Define the HTML content to convert into a PDF
        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>";

        // Convert HTML to PDF document
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(html);

        // Save the PDF document to a specified path
        myPdf.saveAs(Paths.get("C://HTMLtoPDF.pdf"));
    }
}
JAVA
  • 最初のステップは、setLicenseKeyメソッドを使用してライセンスキーを適用することです。 このキーは文字列引数として渡されます。 この場合、"YOUR-LICENSE-KEY"を実際のライセンスキーに置き換える必要があります。
  • 次のステップは、setLogPathメソッドを使用してログパスを設定することです。 これは、IronPDFエンジンのログファイルが保存される場所です。 この場合、"C:/tmp/IronPdfEngine.log"に設定されています。
  • The main method is defined, and a PdfDocument object is created by calling the renderHtmlAsPdf method, passing in a string of HTML as the argument. これにより、HTMLがPDFに変換され、myPdfオブジェクトに保存されます。
  • 最終ステップは、myPdfオブジェクトをsaveAsメソッドを使用してファイルに保存することです。 ファイルの場所は、Pathsオブジェクトの形式で引数として渡されます。この場合は"HTMLtoPDF.pdf"です。

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

Java Library PDF Generation (Full Code Example), Figure 1: The output PDF file from an HTML string HTML文字列から生成された出力PDFファイル

URLからPDFファイルを作成

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

import com.ironsoftware.ironpdf.*;

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

public class UrlToPdfExample {
    public static void main(String[] args) throws IOException {
        // Apply your license key
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set a log path to store log files generated by IronPDF
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Convert a webpage to a PDF by specifying the URL
        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;

public class UrlToPdfExample {
    public static void main(String[] args) throws IOException {
        // Apply your license key
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set a log path to store log files generated by IronPDF
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Convert a webpage to a PDF by specifying the URL
        PdfDocument myPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");

        // Save the PdfDocument to a file
        myPdf.saveAs(Paths.get("url.pdf"));
    }
}
JAVA
  • PdfDocument.renderUrlAsPdfメソッドは、この目的のために特に設計されており、変換するウェブページのURLを含む文字列を受け取ります。 このメソッドは、ウェブページのHTMLコンテンツを取得し、それをPDFドキュメントに変換します。 IronPDFは、すべてのウェブコンポーネントの外観を保持しながら、インタラクティブ機能(リンク、フォームフィールドなど)が機能するようにします。

結果は以下の通りです。

Java Library PDF Generation (Full Code Example), Figure 2: The output PDF file from a URL URLから生成された出力PDFファイル

まとめ

結論として、IronPDFはPDFファイルを作成・操作するための多くの機能を備えた価値あるJavaライブラリです。 Whether you need to digitally sign a PDF document, fill out PDF forms, or perform other tasks, IronPDF makes it easy to do so with minimal coding.

無料トライアルが利用可能で、柔軟な価格設定オプションが用意されているため、IronPDFはプロジェクトにPDF機能を追加したいと考える開発者にとって費用対効果の高いソリューションです。

よくある質問

JavaでPDF文書を作成するにはどうすればいいですか?

IronPDFを使用することで、完全なAPIを活用し、新しいPDFをゼロから作成したり、既存の文書をPDF形式に変換することで、JavaでPDF文書を作成できます。

JavaでHTMLをPDFに変換するプロセスは何ですか?

JavaでHTMLをPDFに変換するには、IronPDFはrenderHtmlAsPdfメソッドを提供しており、これを使用することでHTML文字列を入力し、PDF文書を出力として受け取れます。

JavaアプリケーションでウェブページのURLをPDFに変換するにはどうすればいいですか?

IronPDFはrenderUrlAsPdfメソッドを使用してウェブページのURLをPDFに変換できます。このメソッドはURLからHTMLコンテンツを取得し、それをPDF文書に変換します。

Javaライブラリを使用してPDF文書にデジタル署名を行うことはできますか?

はい、IronPDFはPDF文書にデジタル署名を行う機能を提供しており、文書の真正性と完全性を確保します。

Javaを使用してPDFにセキュリティ機能を追加するにはどうすればいいですか?

IronPDFはパスワード保護や透かしといったセキュリティ機能を提供し、これをPDFに適用することでセキュリティを強化できます。

MavenプロジェクトでPDFライブラリをインストールするにはどのような手順が含まれますか?

MavenプロジェクトにIronPDFをインストールするには、IronPDFの依存関係とSLF4Jロギング依存関係をpom.xmlファイルに追加し、mvn installコマンドを実行する必要があります。

Javaを使用して既存のPDFファイルを操作するにはどうすればいいですか?

IronPDFを使用すると、既存のPDFファイルを操作でき、テキストを編集する方法、文書をマージする方法、注釈を追加する方法、およびデジタル署名を適用する方法が提供されています。

購入前にIronPDFの機能をテストする方法はありますか?

はい、IronPDFは無料トライアルを提供しており、開発者が購入前にその機能をテストできます。

JavaでPDFライブラリを使用する利点は何ですか?

JavaでIronPDFのようなPDFライブラリを使用すると、PDFの作成、編集、変換のプロセスが合理化され、大量のコーディングが不要になり、効率が向上します。

Javaを使用して複数のPDFファイルを1つにまとめるにはどうすればいいですか?

IronPDFには複数のPDFファイルを1つの文書にマージする機能が含まれており、複数のPDFを1つのファイルに簡単に統合できます。

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

Darrius Serrantは、マイアミ大学でコンピュータサイエンスの学士号を取得し、Iron SoftwareでフルスタックWebOpsマーケティングエンジニアとして働いています。若い頃からコーディングに惹かれ、コンピューティングを神秘的かつアクセス可能なものとし、創造性と問題解決のための完璧な媒体と考えていました。

Iron Softwareでは、新しいものを創造することと、複雑なコンセプトをより理解しやすくすることを楽しんでいます。Resident Developerの一人として、次世代に専門知識を共有するために、学生を教えることにも志願しました。

Darriusにとって、その仕事は価値があり、実際の影響があるため、満足感があります。