IronPDF for Javaを使用してHTMLをPDFに変換する方法

HTML to PDF in Java

This article was translated from English: Does it need improvement?
Translated
View the article in English

このチュートリアルは、Java開発者にIronPDFライブラリを使用してHTMLコンテンツをピクセル完璧なPDF(ポータブルドキュメントフォーマット)文書に変換する方法を指導します。

IronPDFは、完全機能を備えたPDFコンバーターおよびPDF処理ライブラリです。 IronPDF is available for both .NET and Java programming languages. このチュートリアルでは、JavaアプリケーションでHTMLコンテンツ(ファイル、マークアップなど)を変換するためのライブラリの使用について説明します。 HTMLをPDFに変換する.NETアプリケーション向けのチュートリアルは、HTML to PDF .NETチュートリアルで利用できます。


JavaでHTMLをPDFに変換する方法

JavaでHTMLをPDFに変換する方法

  1. HTMLをPDFに変換するためのJavaライブラリをインストール
  2. renderHtmlAsPdfメソッドを使用してHTML文字列をPDF文書に変換
  3. JavaでウェブサイトのURLからPDFファイルを生成
  4. renderHtmlFileAsPdfメソッドを使用してHTMLファイルをPDFファイルに変換
  5. 生成されたPDFを新しいファイルとして保存

開始方法

今日あなたのプロジェクトでIronPDFを無料トライアルで使用開始。

最初のステップ:
green arrow pointer


1. IronPDF PDFライブラリのJavaへのインストール

JavaプロジェクトにIronPDFライブラリを組み込む方法は2つあります:

  1. Mavenで構成されたJavaプロジェクトにIronPDFを依存関係として追加
  2. IronPDFのJARファイルをダウンロードしてプロジェクトのクラスパスに手動で追加。

次のセクションでは、両方のインストール方法を簡単に説明します。

オプション1: Maven依存関係としてIronPDFをインストール

Mavenを使用してJavaプロジェクトにIronPDFをインストールするには、Javaプロジェクトのpom.xmlファイルの依存関係セクションに次のアーティファクトを追加します。

<dependencies>
    <!-- IronPDF Library -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>[LATEST_VERSION]</version>
    </dependency>

    <!-- SLF4J for logging (optional but recommended) -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>[LATEST_VERSION]</version>
    </dependency>
</dependencies>
<dependencies>
    <!-- IronPDF Library -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>[LATEST_VERSION]</version>
    </dependency>

    <!-- SLF4J for logging (optional but recommended) -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>[LATEST_VERSION]</version>
    </dependency>
</dependencies>
XML

最初のアーティファクトはIronPDFライブラリの最新バージョンを参照します。 2番目のアーティファクトはSLF4Jの実装を参照します。 この依存関係は、IronPDFのレンダリングエンジンが実行中にログメッセージを生成するために必要です。 Software engineers can substitute this dependency for other logging providers (such as Logback and Log4J); ログを必要としない場合や望まない場合は完全に省略することができます。

前述のライブラリをダウンロードするには、Javaプロジェクトのルートディレクトリでmvn installコマンドを実行します。

オプション2: IronPDF JARを手動でインストール

Developers who prefer not to use Maven or any other dependency management system will need to download the IronPDF library JAR file (and the optional SLF4J implementation) and manually add it to their project's class path.

IronPDFのJARファイルをIronPDF JARダウンロードから直接(またはMaven Repositoryから)ダウンロードします。


2. HTMLをPDFに変換する

このセクションでは、IronPDFの主要なHTMLからPDFへのレンダリング能力を紹介します。

PdfDocumentクラスは、IronPDFのPDF文書のレンダリングおよび操作機能すべてのエントリーポイントです。 このクラスには、HTMLからPDFドキュメントに変換するための強力なメソッドセットが含まれており、次の3つのユースケースをサポートします:HTML文字列/マークアップからの変換、HTMLファイルからの変換、URLからの変換。 このセクションでは、これらのユースケースのそれぞれについて簡単に説明し、追加の情報を取得するためのリンクを提供します。

2.1 IronPDFパッケージをインポート

IronPDFのすべての変換および処理コンポーネントは、com.ironsoftware.ironpdfパッケージに含まれています。

これらのコンポーネントをアプリケーションのソースコードで利用できるようにするために、IronPDFを使用するJavaのソースファイルの上部に、次のインポート文を含めてください。

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

2.2. ライセンスキーの設定(オプション)

IronPDF for Javaは無料で使用できます。 ただし、無料ユーザーの場合、PDF文書にはタイル状の背景透かしが付加されます(以下の画像に示されています)。

透かしを示す画像

IronPDFを使用してウォーターマークなしでPDFを生成するには、有効なライセンスキーを使用する必要があります。 以下のコード行は、ライブラリにライセンスキーを設定します。

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

PDFファイルを生成したりファイル内容をカスタマイズする前にライセンスキーを設定する必要があります。 他のすべてのコード行より前にsetLicenseKeyメソッドを呼び出すことをお勧めします。

Purchase a license key from the IronPDF licensing page, or contact us to 無料トライアルライセンスキーを取得するためにご連絡ください。

2.3 ログファイルの場所の設定(オプション)

デフォルトでは(そしてSLF4Jプロバイダーがインストールされていると仮定して)、IronPDFはJavaアプリケーションのルートディレクトリにある_IronPdfEngine.log_というテキストファイルにログメッセージを生成します。

ログファイルの名前と場所を指定するには、Settings.setLogPathメソッドを使用します。

// Set a log path
Settings.setLogPath(Paths.get("IronPdfEngine.log"));
// Set a log path
Settings.setLogPath(Paths.get("IronPdfEngine.log"));
JAVA

[{i:(Settings.setLogPathは、PDF変換および操作メソッドを使用する前に呼び出す必要があります。)}]

2.4. HTML文字列からPDFを作成

PdfDocument.renderHtmlAsPdfは、HTMLコンテンツの文字列をPDF文書に変換します。

以下のコードサンプルは、単一のヘッドライン要素を使用して新しいファイルを生成します。

// Convert HTML string to PDF document
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Hello from IronPDF!</h1>");
// Save the PDF document
pdf.saveAs("htmlstring_to_pdf.pdf");
// Convert HTML string to PDF document
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Hello from IronPDF!</h1>");
// Save the PDF document
pdf.saveAs("htmlstring_to_pdf.pdf");
JAVA

HTML文字列からPDFへの変換

renderHtmlAsPdfは、モダンで基準に準拠したブラウザが可能な方法で、すべてのHTML、CSS、JavaScriptコンテンツを処理します。 これは、ソフトウェアエンジニアがウェブブラウザで表示されるように正確に見えるPDFドキュメントを作成するのに役立ちます。

renderHtmlAsPdfメソッドは、コンピュータまたはネットワークドライブのフォルダーにある画像、スタイルシート、スクリプトを参照できます。次の例では、CSSファイルとassetsフォルダーにある画像を参照するHTMLからPDF文書を生成します。

// HTML string with external assets
String html = "<html><head><title>Hello world!</title><link rel='stylesheet' href='assets/style.css'></head><body><h1>Hello from IronPDF!</h1><a href='https://ironpdf.com/java/'><img src='assets/logo.png' /></a></body></html>";
// Convert HTML to PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);
// Save the PDF document
pdf.saveAs("output.pdf");
// HTML string with external assets
String html = "<html><head><title>Hello world!</title><link rel='stylesheet' href='assets/style.css'></head><body><h1>Hello from IronPDF!</h1><a href='https://ironpdf.com/java/'><img src='assets/logo.png' /></a></body></html>";
// Convert HTML to PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(html);
// Save the PDF document
pdf.saveAs("output.pdf");
JAVA

外部資産を使用した変換結果

renderHtmlAsPdfの2番目(オプション)の引数を使って、開発者はウェブアセットを参照するための基本パスを指定できます。 このパスはローカルファイルシステム上のディレクトリへのパスやURLパスにすることができます。

Learn more about the renderHtmlAsPdf method from this code example on using HTML to create a PDF, or read about it in the API Reference page for PdfDocument class.

2.5. URLからPDFを作成

開発者は、IronPDFのPdfDocument.renderUrlAsPdfメソッドを使用して、オンラインのウェブページをPDF文書に変換できます。

次の例は、Wikipediaの記事をPDFコンテンツにレンダリングします。

// Convert a URL to PDF
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://en.wikipedia.org/wiki/PDF");
// Save the PDF document
pdf.saveAs("url_to_pdf.pdf");
// Convert a URL to PDF
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://en.wikipedia.org/wiki/PDF");
// Save the PDF document
pdf.saveAs("url_to_pdf.pdf");
JAVA

URLへのPDF変換結果

URLをPDFに変換するこのコード例から、ウェブページをPDFコンテンツに変換する詳細を学びます。

2.6. HTMLファイルからPDFを作成

IronPDFは、ローカルファイルシステム上に保存されたHTMLドキュメントを、同等のPDF形式に直接レンダリングすることもできます。

次のコード例では、IronPDFがHTMLファイルをどれだけうまく変換できるかを示す実際のデモンストレーションとしてこの請求書を使用しています。

請求書のHTMLマークアップをここに便宜的に再現しました。

<html>
<head>
    <meta charset="utf-8">
    <title>Invoice</title>
    <link rel="stylesheet" href="style.css">
    <link rel="license" href="https://www.opensource.org/licenses/mit-license/">
    <script src="script.js"></script>
</head>
<body>
<header>
    <h1>Invoice</h1>
    <address contenteditable>
        <p>Jonathan Neal</p>
        <p>101 E. Chapman Ave<br>Orange, CA 92866</p>
        <p>(800) 555-1234</p>
    </address>
    <span><img alt="" src="http://www.jonathantneal.com/examples/invoice/logo.png"><input type="file" accept="image/*"></span>
</header>
<article>
    <h1>Recipient</h1>
    <address contenteditable>
        <p>Some Company<br>c/o Some Guy</p>
    </address>
    <table class="meta">
        <tr>
            <th><span contenteditable>Invoice #</span></th>
            <td><span contenteditable>101138</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Date</span></th>
            <td><span contenteditable>January 1, 2012</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Amount Due</span></th>
            <td><span id="prefix" contenteditable>$</span><span>600.00</span></td>
        </tr>
    </table>
    <table class="inventory">
        <thead>
        <tr>
            <th><span contenteditable>Item</span></th>
            <th><span contenteditable>Description</span></th>
            <th><span contenteditable>Rate</span></th>
            <th><span contenteditable>Quantity</span></th>
            <th><span contenteditable>Price</span></th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td><a class="cut">-</a><span contenteditable>Front End Consultation</span></td>
            <td><span contenteditable>Experience Review</span></td>
            <td><span data-prefix>$</span><span contenteditable>150.00</span></td>
            <td><span contenteditable>4</span></td>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
        </tbody>
    </table>
    <a class="add">+</a>
    <table class="balance">
        <tr>
            <th><span contenteditable>Total</span></th>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Amount Paid</span></th>
            <td><span data-prefix>$</span><span contenteditable>0.00</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Balance Due</span></th>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
    </table>
</article>
<aside>
    <h1><span contenteditable>Additional Notes</span></h1>
    <div contenteditable>
        <p>A finance charge of 1.5% will be made on unpaid balances after 30 days.</p>
    </div>
</aside>
</body>
</html>
<html>
<head>
    <meta charset="utf-8">
    <title>Invoice</title>
    <link rel="stylesheet" href="style.css">
    <link rel="license" href="https://www.opensource.org/licenses/mit-license/">
    <script src="script.js"></script>
</head>
<body>
<header>
    <h1>Invoice</h1>
    <address contenteditable>
        <p>Jonathan Neal</p>
        <p>101 E. Chapman Ave<br>Orange, CA 92866</p>
        <p>(800) 555-1234</p>
    </address>
    <span><img alt="" src="http://www.jonathantneal.com/examples/invoice/logo.png"><input type="file" accept="image/*"></span>
</header>
<article>
    <h1>Recipient</h1>
    <address contenteditable>
        <p>Some Company<br>c/o Some Guy</p>
    </address>
    <table class="meta">
        <tr>
            <th><span contenteditable>Invoice #</span></th>
            <td><span contenteditable>101138</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Date</span></th>
            <td><span contenteditable>January 1, 2012</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Amount Due</span></th>
            <td><span id="prefix" contenteditable>$</span><span>600.00</span></td>
        </tr>
    </table>
    <table class="inventory">
        <thead>
        <tr>
            <th><span contenteditable>Item</span></th>
            <th><span contenteditable>Description</span></th>
            <th><span contenteditable>Rate</span></th>
            <th><span contenteditable>Quantity</span></th>
            <th><span contenteditable>Price</span></th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td><a class="cut">-</a><span contenteditable>Front End Consultation</span></td>
            <td><span contenteditable>Experience Review</span></td>
            <td><span data-prefix>$</span><span contenteditable>150.00</span></td>
            <td><span contenteditable>4</span></td>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
        </tbody>
    </table>
    <a class="add">+</a>
    <table class="balance">
        <tr>
            <th><span contenteditable>Total</span></th>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Amount Paid</span></th>
            <td><span data-prefix>$</span><span contenteditable>0.00</span></td>
        </tr>
        <tr>
            <th><span contenteditable>Balance Due</span></th>
            <td><span data-prefix>$</span><span>600.00</span></td>
        </tr>
    </table>
</article>
<aside>
    <h1><span contenteditable>Additional Notes</span></h1>
    <div contenteditable>
        <p>A finance charge of 1.5% will be made on unpaid balances after 30 days.</p>
    </div>
</aside>
</body>
</html>
HTML

HTMLファイルとそのCSSファイル、JavaScriptファイルが"invoices"と呼ばれるフォルダーに保存されていると仮定しましょう。以下のようにIronPDFを使用してHTMLファイルを変換できます。

// Convert an HTML file to PDF
PdfDocument pdf = PdfDocument.renderHtmlFileAsPdf("C:/invoices/TestInvoice1.html");
// Save the PDF document
pdf.saveAs("htmlfile_to_pdf.pdf");
// Convert an HTML file to PDF
PdfDocument pdf = PdfDocument.renderHtmlFileAsPdf("C:/invoices/TestInvoice1.html");
// Save the PDF document
pdf.saveAs("htmlfile_to_pdf.pdf");
JAVA

HTML文字列からPDFへの変換例と同様に、IronPDFはHTMLドキュメント内のすべての相対URLをファイルシステム上の正しいパスに正しく解決します。 その結果、この例が生成するPDFファイルは、通常、ウェブページに与える視覚的な影響を完全にキャプチャすることができます。

3. さらに読む

IronPDFのHTML to PDFレンダリング能力は、まだほんの一部しか紹介していません。

Java開発のためのHTMLからPDFへのコンバーターの使用方法を、コード例セクションに掲載されたキュレーションされたコードサンプルを使用してさらに理解を深めましょう。

  1. このPDF生成設定のコード例を読み、変換プロセス中にPDF文書の外観をカスタマイズする方法を学びます。
  2. Generate PDF files with custom headers and footers, margin sizes, page dimensions, watermarks, and much more.
  3. Extract PDF content (text extraction from PDFs and image extraction from PDFs) from documents, optimize file sizes with PDF compression, and print PDFs programmatically with IronPrint capabilities.

IronPDF Java APIリファレンスページを<!code>PdfDocumentクラスで、レンダリングされたHTMLからPDFまでの制御をさらに深めます。

よくある質問

JavaでHTMLコンテンツをPDFに変換するにはどうすればよいですか?

IronPDFのPdfDocument.renderHtmlAsPdfメソッドを使用して、HTMLコンテンツをPDFドキュメントに変換できます。このメソッドはHTMLのフォーマットを保持します。

JavaでウェブページのURLを直接PDFに変換することは可能ですか?

はい、IronPDFのPdfDocument.renderUrlAsPdfメソッドを使用して、任意のオンラインウェブページを直接PDFドキュメントに変換できます。

Javaを使用してローカルHTMLファイルをPDFに変換するにはどうすればよいですか?

IronPDFを使用すると、PdfDocument.renderHtmlFileAsPdfメソッドを使用して、ローカルシステムに保存されているHTMLファイルをPDFドキュメントにレンダリングできます。

Java PDFライブラリで生成されたPDFから透かしを削除できますか?

IronPDFで生成されたPDFから透かしを削除するには、PDFを作成する前にLicense.setLicenseKeyメソッドを使用して有効なライセンスキーを設定する必要があります。

HTMLからPDFに変換する際に、CSSのような外部アセットを含めるにはどうすればよいですか?

IronPDFはHTMLからPDFの変換において外部CSSおよびその他のアセットの取り込みをサポートしています。これらのアセットへのパスがHTMLコンテンツ内で正しく参照されていることを確認してください。

Mavenを使用してIronPDFをJavaにインストールする手順は何ですか?

Mavenを使用してJavaプロジェクトにIronPDFをインストールするには、pom.xmlファイルにIronPDFの依存関係を追加し、プロジェクトのルートディレクトリでmvn installを実行します。

JavaでPDF生成プロセスをログに記録するオプションはありますか?

はい、IronPDFはSLF4Jを通じてログ記録をサポートしています。SLF4Jプロバイダーがインストールされていれば、Settings.setLogPathメソッドを使用してログファイルの場所を設定できます。

JavaでHTMLから生成されたPDFの外観をカスタマイズできますか?

はい、IronPDFはヘッダー、フッター、および余白を含むPDF生成設定をカスタマイズすることを許可しています。ライブラリで利用可能なさまざまなカスタマイズオプションを使用することでこれを実現できます。

JavaでIronPDFを使用するための他の例をどこで見つけることができますか?

IronPDF for Javaの使用に関する他の例やドキュメントは、IronPDFウェブサイトのコーディング例セクションおよびJava APIリファレンスページで見つけることができます。

IronPDF は HTML から PDF への変換において .NET 10 と完全に互換性がありますか?

はい。IronPDFは.NET 10と完全に互換性があり、特別な設定なしですぐに使用できます。ChromePdfRenderer.RenderHtmlAsPdfなどのHTMLからPDFへのChromePdfRenderer.RenderHtmlAsPdfメソッドを.NET 10で使用し、パフォーマンスの向上や新しいAPIなどのフレームワークの改善を活用できます。

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

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

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

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

準備はいいですか?
バージョン: 2025.11 ただ今リリースされました