在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:wooio/htmltopdf-java开源软件地址:https://github.com/wooio/htmltopdf-java开源编程语言:Java 100.0%开源软件介绍:OverviewThis project is based on wkhtmltopdf, which converts HTML documents to PDF. Access to wkhtmltopdf is performed via JNA, exposed through a Java-friendly layer. Get itGradle: compile 'io.woo:htmltopdf:1.0.8' Maven: <dependency>
<groupId>io.woo</groupId>
<artifactId>htmltopdf</artifactId>
<version>1.0.8</version>
<scope>compile</scope>
</dependency> Getting startedThe following examples should be sufficient to get you started, however there
are many more options discoverable by looking into the methods of Saving HTML as a PDF fileboolean success = HtmlToPdf.create()
.object(HtmlToPdfObject.forHtml("<p><em>Apples</em>, not oranges</p>"))
.convert("/path/to/file.pdf"); Saving a webpage from URL as a PDF fileboolean success = HtmlToPdf.create()
.object(HtmlToPdfObject.forUrl("https://github.com/wooio/htmltopdf-java"))
.convert("/path/to/file.pdf"); Saving multiple objects as a PDF fileboolean success = HtmlToPdf.create()
.object(HtmlToPdfObject.forUrl("https://github.com/wooio/htmltopdf-java"))
.object(HtmlToPdfObject.forHtml("<p>This is the second object...</p>"))
// ...
.convert("/path/to/file.pdf"); Converting to InputStream (instead of saving as file)Converting to an InputStream would be useful if you intend on returning the resulting PDF document as an HTTP response or adding it as an email attachment HtmlToPdf htmlToPdf = HtmlToPdf.create()
// ...
.object(HtmlToPdfObject.forUrl("https://github.com/wooio/htmltopdf-java"));
try (InputStream in = htmlToPdf.convert()) {
// "in" has PDF bytes loaded
} catch (HtmlToPdfException e) {
// HtmlToPdfException is a RuntimeException, thus you are not required to
// catch it in this scope. It is thrown when the conversion fails
// for any reason.
} Concurrency limitationsWhile the library is thread-safe, it unfortunately cannot perform conversions concurrently. Because wkhtmltopdf uses Qt behind the scenes to render webpages, there is a single thread which performs such rendering across a single process. Therefore, at this point, it is only possible to perform one conversion at the same time per process. TroubleshootingMissing native dependenciesIf you get the following exception:
Then that likely means that one of the native dependencies of wkhtmltopdf is not met. It might be worth checking that the following packages are installed:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论