JavaでPDFファイルを作成する方法
IronPDFライブラリを使用すると、Java で PDF ファイルを簡単に作成できます。このライブラリは、HTML 文字列の場合は renderHtmlAsPdf()、HTML ファイルの場合は renderHtmlFileAsPdf()、Web ページの場合は renderUrlAsPdf() などのメソッドを使用して、HTML を PDF に変換します。
クイックスタート: Java で最初の PDF を作成する
1.IronPDF依存関係をpom.xmlに追加してください:
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>
2.IronPDFクラスをインポートしてください:
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.*;
3.HTMLからPDFを作成します: ```java :title=クイックスタート PdfDocument pdf = PdfDocument.renderHtmlAsPdf(""); pdf.saveAs(Paths.get("output.pdf"));
<div class="hsg-featured-snippet">
<h3>JavaでPDFファイルを作成する方法</h3>
<ol>
<li><a class="js-modal-open" data-modal-id="download-modal" href="#download-modal">IronPDF for Javaライブラリをインストールする</a>.</li>
<li>`renderHtmlAsPdf`メソッドでHTML文字列からPDFファイルを作成する</li>
<li>HTMLファイルからPDFファイルを作成するには、`renderHtmlFileAsPdf`メソッドを使用してください。</li>
<li>`renderUrlAsPdf`メソッドを使ってURLからPDFを作成する</li>
<li>パスワード保護されたPDFファイルを希望のディレクトリにエクスポート</li>
</ol>
</div>
<em>Javaでプログラム的にPDFを作成することで、請求書や報告書、その他のビジネス文書をオンデマンドで自動生成することができます。
<em>このガイドはJavaアプリケーションでプログラムでPDFファイルを作成するためにIronPDFを使用することをカバーしています。
## IronPDF for Java PDFライブラリとは何ですか?
IronPDFは、HTMLからPDFドキュメントを作成するため for Javaライブラリです。 PDFを作成およびカスタマイズするための機能を提供します。含まれる機能は次の通りです:
1.テキスト、画像、その他のコンテンツタイプの追加
2.フォント、色の選択、レイアウトとフォーマットの制御
IronPdfは.NET Framework上に構築されており、.NETとJavaアプリケーションの両方で使用することができます。 このライブラリは、[カスタム透かし](https://ironpdf.com/java/how-to/custom-watermark/)、[PDF圧縮](https://ironpdf.com/java/how-to/compress-pdf-java-tutorial/)、[フォーム作成](https://ironpdf.com/java/how-to/create-forms/)などの高度な機能をサポートしています。
IronPDFはまた、ファイルフォーマットの変換、テキストとデータの抽出、パスワードの暗号化を含むPDF関連のタスクを処理します。 必要に応じて、[複数のPDFをマージ](https://ironpdf.com/java/how-to/java-merge-pdf-tutorial/)したり、[分割したり](https://ironpdf.com/java/examples/split-pdfs/)することができます。
## JavaアプリケーションでPDFドキュメントを作成するには?
### どのような前提条件が必要ですか?
MavenプロジェクトでIronPDFを使用するには、これらの前提条件がインストールされていることを確認してください:
1.**Java Development Kit (JDK):**Javaアプリケーションのコンパイルと実行に必要。 Download from <a href="https://www.oracle.com/java/technologies/javase-downloads.html" target="_blank" rel="nofollow noopener noreferrer">Oracle website</a>.
2.**Maven:**プロジェクトのライブラリをダウンロードするために必要です。 [Apache Maven ウェブサイト](https://maven.apache.org/download.cgi)からダウンロードしてください。
3.**IronPDFライブラリ:** pom.xmlの依存関係としてMavenプロジェクトに追加します:
```xml
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>1.0.0</version>
</dependency>
Gradleプロジェクトでは、IronPdfを追加してください:
implementation 'com.ironsoftware:ironpdf:1.0.0'
コードを書く前に、どのようなステップを踏むべきですか?
まず、Javaソースファイルにこのimport文を追加します:
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.*;
特定の機能については、追加のクラスをインポートしてください:
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;
import com.ironsoftware.ironpdf.security.SecurityOptions;
import com.ironsoftware.ironpdf.security.SecurityManager;
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;
import com.ironsoftware.ironpdf.security.SecurityOptions;
import com.ironsoftware.ironpdf.security.SecurityManager;
次に、main メソッドで有効なライセンス キーを使用してIronPDFを構成します。
License.setLicenseKey("Your license key");
License.setLicenseKey("Your license key");
注意: *ライセンスキーは透かしを取り除きます。 Purchase a License Key または Get a Free Trial. ライセンスがない場合、PDFには透かしが入ります。 詳細については、ライセンスキーの使用を参照してください。
JavaでHTML文字列からPDFファイルを作成するには?
HTML 文字列を PDF に変換するには、renderHtmlAsPdf() を使用します。 このメソッドは、HTML5、CSS3、JavaScriptのレンダリングをサポートしています。
HTML 文字列を renderHtmlAsPdf に渡します。 IronPDF はこれを PdfDocument インスタンスに変換します。
// HTML content to be converted to PDF
String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";
// Convert HTML string to PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);
// Save the PDF document to a file
pdf.saveAs(Paths.get("html.pdf"));
// HTML content to be converted to PDF
String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";
// Convert HTML string to PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);
// Save the PDF document to a file
pdf.saveAs(Paths.get("html.pdf"));
これにより、HTMLコンテンツを含む"html.pdf"が作成されます。
CSSスタイリングによる複雑なHTMLを含むこと:
// HTML with CSS styling
String styledHtml = """
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
h1 { color: #2563eb; }
.content { margin: 20px; padding: 15px; background-color: #f3f4f6; }
</style>
</head>
<body>
<div class="content">
<h1>Styled PDF Document</h1>
<p>This PDF was created from HTML with custom CSS styling.</p>
</div>
</body>
</html>
""";
PdfDocument styledPdf = PdfDocument.renderHtmlAsPdf(styledHtml);
styledPdf.saveAs(Paths.get("styled.pdf"));
// HTML with CSS styling
String styledHtml = """
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
h1 { color: #2563eb; }
.content { margin: 20px; padding: 15px; background-color: #f3f4f6; }
</style>
</head>
<body>
<div class="content">
<h1>Styled PDF Document</h1>
<p>This PDF was created from HTML with custom CSS styling.</p>
</div>
</body>
</html>
""";
PdfDocument styledPdf = PdfDocument.renderHtmlAsPdf(styledHtml);
styledPdf.saveAs(Paths.get("styled.pdf"));
高度な変換については、HTML to PDF チュートリアルをご覧ください。
JavaでHTMLページからPDFファイルを作成するには?
ローカルのHTMLファイルからPDFを作成します:
// Convert HTML file to PDF
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
// Convert HTML file to PDF
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
renderHtmlFileAsPdf メソッドは、ファイル パスを String または Path オブジェクトとして受け入れます。
IronPDFはCSSとJavaScriptでHTML要素をブラウザと同じようにレンダリングします。 これには、外部CSSファイル、画像、JavaScriptライブラリも含まれます。 詳しくはIronPDFファイルをご覧ください。
PDF を特定の場所に保存するには、saveAs を使用します。
JavaでURLからPDFファイルを作成するには?
Web ページから PDF を作成するには、renderUrlAsPdf() を使用します。
// Convert a URL to PDF
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Convert a URL to PDF
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
認証されたウェブサイトについては、認証情報を提供してください:
// Create render options with login credentials
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setAuthUsername("username");
renderOptions.setAuthPassword("password");
// Convert secured URL to PDF
PdfDocument securedPdf = PdfDocument.renderUrlAsPdf("https://secure-site.com", renderOptions);
securedPdf.saveAs(Paths.get("secured.pdf"));
// Create render options with login credentials
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setAuthUsername("username");
renderOptions.setAuthPassword("password");
// Convert secured URL to PDF
PdfDocument securedPdf = PdfDocument.renderUrlAsPdf("https://secure-site.com", renderOptions);
securedPdf.saveAs(Paths.get("secured.pdf"));
詳細はURL to PDF Code Exampleを参照してください。 複雑な認証については、Website & System Loginsを参照してください。
PDFファイルをフォーマットするにはどうすればよいですか?
PDF 形式を指定するには、ChromePdfRenderOptions を使用します。 ページの向き、サイズ、余白を設定します。 オプションは、レンダリング・メソッドの第2引数として渡します:
// Create render options with custom settings
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
// Set page orientation to landscape
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);
// Set custom paper size (Letter size)
renderOptions.setPaperSize(PaperSize.LETTER);
// Set custom margins (in millimeters)
renderOptions.setMarginTop(10);
renderOptions.setMarginRight(10);
renderOptions.setMarginBottom(10);
renderOptions.setMarginLeft(10);
// Enable background images and colors
renderOptions.setPrintHtmlBackgrounds(true);
// Apply options to PDF generation
PdfDocument formattedPdf = PdfDocument.renderHtmlAsPdf("<h1>Formatted PDF</h1>", renderOptions);
formattedPdf.saveAs(Paths.get("formatted.pdf"));
// Create render options with custom settings
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
// Set page orientation to landscape
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);
// Set custom paper size (Letter size)
renderOptions.setPaperSize(PaperSize.LETTER);
// Set custom margins (in millimeters)
renderOptions.setMarginTop(10);
renderOptions.setMarginRight(10);
renderOptions.setMarginBottom(10);
renderOptions.setMarginLeft(10);
// Enable background images and colors
renderOptions.setPrintHtmlBackgrounds(true);
// Apply options to PDF generation
PdfDocument formattedPdf = PdfDocument.renderHtmlAsPdf("<h1>Formatted PDF</h1>", renderOptions);
formattedPdf.saveAs(Paths.get("formatted.pdf"));
PDFファイルをパスワードで保護するにはどうすればよいですか?
PDF をパスワードで保護するには、SecurityOptions を使用します。
// Create security options and set user password
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");
// Create security options and set user password
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");
高度なセキュリティオプションを設定します:
// Advanced security settings
SecurityOptions advancedSecurity = new SecurityOptions();
advancedSecurity.setUserPassword("user123");
advancedSecurity.setOwnerPassword("owner456");
// Restrict permissions
advancedSecurity.setAllowPrint(false);
advancedSecurity.setAllowCopy(false);
advancedSecurity.setAllowEditContent(false);
advancedSecurity.setAllowEditAnnotations(false);
// Advanced security settings
SecurityOptions advancedSecurity = new SecurityOptions();
advancedSecurity.setUserPassword("user123");
advancedSecurity.setOwnerPassword("owner456");
// Restrict permissions
advancedSecurity.setAllowPrint(false);
advancedSecurity.setAllowCopy(false);
advancedSecurity.setAllowEditContent(false);
advancedSecurity.setAllowEditAnnotations(false);
PDF の SecurityManager 経由でセキュリティを適用します:
// Apply security options to the PDF
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
// Save the password-protected PDF document
urlToPdf.saveAs("protected.pdf");
// Apply security options to the PDF
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
// Save the password-protected PDF document
urlToPdf.saveAs("protected.pdf");
PDFを開くとパスワードのプロンプトが表示されます:
正しいパスワードを入力すると、PDFは正常に開きます。
追加の設定については、セキュリティとメタデータの例を参照してください。
完全なソースコードとは何ですか?
このチュートリアルの完全なソースコード
// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) throws IOException {
// Apply your license key
License.setLicenseKey("Your License Key");
// Convert HTML string to a PDF and save it
String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);
pdf.saveAs(Paths.get("html.pdf"));
// Convert HTML file to a PDF and save it
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
// Convert URL to a PDF and save it
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
urlToPdf.saveAs(Paths.get("urlToPdf.pdf"));
// Password-protect the PDF file
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
urlToPdf.saveAs(Paths.get("protected.pdf"));
}
}
// Import statement for IronPDF Java
import com.ironsoftware.ironpdf.*;
import java.io.IOException;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) throws IOException {
// Apply your license key
License.setLicenseKey("Your License Key");
// Convert HTML string to a PDF and save it
String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);
pdf.saveAs(Paths.get("html.pdf"));
// Convert HTML file to a PDF and save it
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
// Convert URL to a PDF and save it
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
urlToPdf.saveAs(Paths.get("urlToPdf.pdf"));
// Password-protect the PDF file
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);
urlToPdf.saveAs(Paths.get("protected.pdf"));
}
}
IronPdfはフォーマットを損なうことなく画像やテキストをレンダリングします。 ボタンはクリック可能なまま、テキストボックスは編集可能なまま。 このライブラリは、署名の追加、グラフの描画、PDFの印刷をサポートしています。
重要なポイントは何ですか?
このガイドではIronPDFを使ってJavaでPDFを作成するデモンストレーションを行いました。 IronPDFはHTMLファイルやXMLドキュメント、その他のソースからPDFを生成するためのシンプルなAPIを提供します。
レポートや請求書など、あらゆるタイプのドキュメントを迅速に作成します。 AWS、Azure、またはGoogleクラウドにデプロイしてください。
IronPDF には、$999 から始まる商用ライセンスが必要です。 本番テストのための無料トライアルを入手。
よくある質問
JavaでPDFファイルを作成する最も簡単な方法は何ですか?
JavaでPDFを作成する最も簡単な方法はIronPDFのrenderHtmlAsPdf()メソッドを使うことです。このメソッドにHTML文字列を渡して結果を保存するだけです:PdfDocument pdf = PdfDocument.renderHtmlAsPdf("Hello World!"); pdf.saveAs(Paths.get("output.pdf"));
MavenプロジェクトにIronPDFを追加するには?
この依存関係をpom.xmlファイルに含めることで、IronPDFをMavenプロジェクトに追加します:
既存のHTMLファイルをPDF形式に変換できますか?
はい、IronPDFはHTMLファイルをPDFに変換するためのrenderHtmlFileAsPdf()メソッドを提供しています。このメソッドはファイルシステムからHTMLファイルを読み込み、PDFドキュメントに変換します。
JavaでWebページからPDFを生成するには?
IronPDFはウェブページを直接PDFに変換するrenderUrlAsPdf()メソッドを提供します。変換したいウェブページのURLを指定するだけで、IronPDFはそれをPDFドキュメントとしてレンダリングします。
プログラムで作成できるビジネス文書の種類は?
IronPDFは請求書、レポート、フォーム、その他のオンデマンドドキュメントを含む様々なビジネスドキュメントの自動生成を可能にします。このライブラリはカスタム透かし、PDF圧縮、フォーム作成などの高度な機能をサポートしています。
パスワードで保護されたPDFを作成することは可能ですか?
はい、IronPDFはPDFファイルのパスワード暗号化をサポートしています。パスワードで保護されたPDFを任意のディレクトリにエクスポートすることができ、機密性の高いビジネス情報のドキュメントセキュリティを確保できます。
Java で IronPDF を使用するためのシステム要件は何ですか?
JavaでIronPDFを使用するには、アプリケーションのコンパイルと実行にJava Development Kit (JDK)、依存関係の管理にMavenまたはGradle、そしてIronPDFライブラリを依存関係としてプロジェクトに追加する必要があります。
新しいPDFを作成するだけでなく、既存のPDFを操作することはできますか?
はい、IronPDFは作成だけでなく様々なPDF操作のタスクを処理します。複数のPDFをマージしたり、分割したり、テキストやデータを抽出したり、ライブラリの包括的な機能を使ってファイルフォーマットの変換を実行したりすることができます。



