この文章では、Javaを使用してHTMLからPDFを生成する方法を紹介します。CSSスタイルを含むHTMLドキュメントを簡単に操作・レンダリングできます。
Aspose.HTML for Java
は、アプリケーションが幅広いHTML操作タスクを実行できるようにし、HTML5、CSS3、SVG、HTML Canvas の機能をサポートします。API を使用してウェブページを読み込み、解析・編集し、ページを保存・変換できます。
操作可能な主なフォーマット: XHTML、MHTML、SVG、MD、PDF、XPS、EPUB、PNG、TIFF、JPEG、BMP など。
JavaでHTMLからPDF
豊富な機能セット
Aspose.HTML for Java は、さまざまな領域で多数の機能を提供します。
• Document Object Model – ノードの編集、作成、削除が可能で、公式 HTML ドキュメントに基づいて構築されています。
• 高性能コンバータ。サポートされているファイル形式の詳細は 機能一覧
または 変換
記事をご覧ください。
• CSS、JavaScript、その他組み込みフォーマットの処理。
Javaコード1行でHTMLからPDFを生成
静的メソッドを使用すれば、Converter
クラスの 1 行だけで Java アプリケーション内で HTML を PDF に変換できます。
// Invoke the ConvertHTML method to convert the HTML to PDF.
com.aspose.html.converters.Converter.convertHTML(
"<span>Hello World!!</span>",
".",
new com.aspose.html.saving.PdfSaveOptions(),
"output.pdf"
);
Javaで行単位にHTMLからPDFを生成
以下の手順で、HTML を行単位で PDF に変換します。
- HtmlDocument
クラスを使用して最初に HTML ファイルを読み込みます。
- 次に PdfSaveOptions
のインスタンスを作成します。
- Converter
クラスの ConvertHTML()
メソッドで HTML を PDF ファイルとして保存します。ConvertHTML() メソッドには HTMLDocument
、PdfSaveOptions
、および出力ファイルパスを渡す必要があります。
// Prepare an HTML code and save it to the file.
String code = "<span>Hello World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html");
try {
// Initialize PdfSaveOptions
com.aspose.html.saving.PdfSaveOptions options = new com.aspose.html.saving.PdfSaveOptions();
// Convert HTML to PDF
com.aspose.html.converters.Converter.convertHTML(
document,
options,
"output.pdf"
);
} finally {
if (document != null) {
document.dispose();
}
}
Save オプションでレンダリングプロセスをカスタマイズ
PdfSaveOptions
を使用して、ページサイズ、ファイル権限、余白、CSS メディアタイプなどを指定し、レンダリングプロセスをカスタマイズできます。以下の例では、PdfSaveOptions を使用してカスタムページサイズと背景色を設定した PDF ファイルを作成する方法を示します。
// Prepare an HTML code and save it to the file
String code = "<span>Hello</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
fileWriter.write(code);
}
// Set A5 as a page-size and change the background color to green
com.aspose.html.saving.PdfSaveOptions options = new com.aspose.html.saving.PdfSaveOptions();
com.aspose.html.rendering.PageSetup pageSetup = new com.aspose.html.rendering.PageSetup();
com.aspose.html.drawing.Page anyPage = new com.aspose.html.drawing.Page();
anyPage.setSize(
new com.aspose.html.drawing.Size(
com.aspose.html.drawing.Length.fromInches(8.3f),
com.aspose.html.drawing.Length.fromInches(5.8f)
)
);
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);
options.setBackgroundColor(com.aspose.html.drawing.Color.getGreen());
// Convert HTML document to PDF
com.aspose.html.converters.Converter.convertHTML(
"document.html",
options,
"output.pdf"
);
Fine-Tuning Converters
記事で、PdfSaveOptions
の詳細を確認できます。
出力ストリームプロバイダー
リモートストレージ(クラウドやデータベース)にファイルを保存したい場合は、MemoryStreamProvider
インターフェイスを実装できます。MemoryStreamProvider は、ページやドキュメントのレンダリング後にストリームを作成・解放するコールバックオブジェクトとして設計されています。
以下に MemoryStreamProvider の使用例を示します。
package com.aspose.html.examples.java;
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-Java
public class MemoryStreamProvider implements java.io.Closeable {
// List of InputStream objects created during the document rendering
public java.util.List<java.io.InputStream> lStream = new java.util.ArrayList<>();
@Override
public void close() throws java.io.IOException {
for (java.io.InputStream stream : lStream) {
stream.close();
}
}
}
// Create an instance of MemoryStreamProvider
try (MemoryStreamProvider streamProvider = new MemoryStreamProvider()) {
// Initialize an HTML document
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("<span>Hello World!!</span>", ".");
try {
// Convert HTML to PDF by using the MemoryStreamProvider
com.aspose.html.converters.Converter.convertHTML(
document,
new com.aspose.html.saving.PdfSaveOptions(),
streamProvider.lStream
);
// Get access to the memory stream that contains the result data
java.io.InputStream inputStream = streamProvider.lStream.stream().findFirst().get();
// Flush the result data to the output file
try (java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream("output.pdf")) {
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
fileOutputStream.write(buffer);
}
} finally {
if (document != null) {
document.dispose();
}
}
}
Aspose は、ナレッジベース記事
、ドキュメント
、ブログ
、コードサンプル
、デモ
、そして 無料サポート
や高度に利用可能な 有料サポート
を通じて、すべての製品に対する技術サポートを提供しています。
お客様の要件に合わせて Aspose.HTML for Java の実装を支援するエキスパートが必要な場合は、最適なソリューションは当社の 有料コンサルティング
です。