- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想使用 wkhtmltopdf
将 HTML 文件转换为 PDF 文件。 wkhtmltopdf
是我的最佳选择,因为它使用 WebKit 呈现 HTML 文件。问题是我想用 Java 做同样的事情,但是 wkhtmltopdf
不提供任何 Java API。
我可以使用 Runtime.exec()
或 ProcessBuilder
从 Java 派生一个新进程并使用 wkhtmtopdf
创建 PDF 输出过程。但是,由于我正在开发基于 Web 的应用程序,因此不允许在服务器中创建这么多新进程。
有没有其他方法可以使用wkhtmltopdf
?我真的很想使用它,因为它会给我准确的输出。
或者,是否有任何其他开源浏览器引擎提供 Java API,可以像 wkhtmltopdf
一样呈现我的 HTML 页面?
最佳答案
请记住,运行您的 Java 代码的系统必须安装 wkhtmltopdf 才能使我在这里所说的一切正常工作...转到 www.wkhtmltopdf.org 并下载您需要的版本。
我知道这是旧的,到现在为止您肯定已经弄清楚了,但是如果您不想使用 JNI 或 JNA 来执行此操作,则可以通过系统上的 .exec 调用非常简单地完成。
这是一个类,它可以完全满足您的需求,而不必为 JNI 或 JNA 操心:
public class MegaSimplePdfGenerator {
public void makeAPdf() throws InterruptedException, IOException {
Process wkhtml; // Create uninitialized process
String command = "wkhtmltopdf http://www.google.com /Users/Shared/output.pdf"; // Desired command
wkhtml = Runtime.getRuntime().exec(command); // Start process
IOUtils.copy(wkhtml.getErrorStream(), System.err); // Print output to console
wkhtml.waitFor(); // Allow process to run
}
}
您必须以某种方式绑定(bind)到输入流之一,以便进程运行。这可以是 inputStream 或 errorStream。在这种情况下,因为我只是写入一个文件,所以我继续将 System.err 连接到 wkhtml 进程的 errorStream。
如何只使用流!
如果您希望源 HTML 来自流和/或目标 PDF 写入流,那么您可以使用 '-' 作为“URI”而不是常规字符串。
示例:wkhtmltopdf - -
或 wkhtmltopdf/Users/Shared/somefile.html -
然后您可以捕获输入和输出流并根据需要进行写入和读取。
如果您只连接到单个流,则不需要使用线程,并且不会出现流无休止地相互等待的情况。
但是,如果您对 HTML 源和 PDF 目标都使用流,则必须使用线程才能完成该过程。
注意:请记住,必须刷新并关闭 OutputStream,wkhtmltopdf 才能开始构建 PDF 并流式传输结果!
示例:
public class StreamBasedPdfGenerator {
public void makeAPdfWithStreams() throws InterruptedException, IOException {
Process wkhtml; // Create uninitialized process
// Start by setting up file streams
File destinationFile = new File("/Users/Shared/output.pdf");
File sourceFile = new File("/Users/Shared/pdfPrintExample.html");
FileInputStream fis = new FileInputStream(sourceFile);
FileOutputStream fos = new FileOutputStream(destinationFile);
String command = "wkhtmltopdf - -"; // Desired command
wkhtml = Runtime.getRuntime().exec(command); // Start process
Thread errThread = new Thread(() -> {
try {
IOUtils.copy(wkhtml.getErrorStream(), System.err);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
Thread htmlReadThread = new Thread(() -> {
try {
IOUtils.copy(fis, wkhtml.getOutputStream());
wkhtml.getOutputStream().flush();
wkhtml.getOutputStream().close();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
Thread pdfWriteThread = new Thread(() -> {
try {
IOUtils.copy(wkhtml.getInputStream(), fos);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
// Do NOT use Run... it should be clear why, you want them to all be going at the same time.
errThread.start();
pdfWriteThread.start();
htmlReadThread.start();
// Connect HTML Source Stream to wkhtmltopdf
// Connect PDF Source Stream from wkhtmltopdf to the Destination file steam
wkhtml.waitFor(); // Allow process to run
}
}
当您在 Web 服务器上运行它并希望避免创建临时 HTML 或 PDF 文件时,流非常有用,您可以通过捕获和写入 HTTP 响应流来简单地流式传输响应。
希望对大家有所帮助!
关于java - 如何在 Java 中使用 wkhtmlpdf 将 HTML 文件转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28442711/
我想使用 wkhtmltopdf 将 HTML 文件转换为 PDF 文件。 wkhtmltopdf 是我的最佳选择,因为它使用 WebKit 呈现 HTML 文件。问题是我想用 Java 做同样的事情
我是一名优秀的程序员,十分优秀!