今日は Spire.PDF for Javaという無料のライブラリを使って、HTMLをPDFに変換する方法を紹介します。今回はSpire.PDF for Java 3.6.6以上のバージョンが必要なので、要注意です。また、従来の方法とは違って、今度はGUI機能も必須だから、Xvfbといったようなプログラムをダウンロードしておいてください。
下準備
1.E-iceblueの公式サイトからFree Spire.PDF for Java無料版をダウンロードしてください。
2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire.PDF.jarを参照に追加してください。
```JAVAimport com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import com.spire.pdf.htmlconverter.qt.Size;
public class HtmltoPDF {
public static void main(String[] args) {
//HTML を名付ます。
String url = "https://www.google.com/";
String fileName = "Result.pdf";
String pluginPath = "D:/Qt/plugins_32";
HtmlConverter.setPluginPath(pluginPath);
//PDFで保存ます。
HtmlConverter.convert(url, fileName, true, 1000000, new Size(600f, 900f), new PdfMargins(0));
}
}
<h4><strong>実行結果</strong></h4>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210507/20210507154732.png" alt="f:id:lendoris:20210507154732p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p><strong> </strong></p>