【问题标题】:How to print HTML to PDF如何将 HTML 打印为 PDF
【发布时间】:2014-04-04 18:02:29
【问题描述】:

我想将 html 打印为 pdf。这是我的代码。它没有抛出任何错误,但是当我打开 pdf 时它显示错误

这是我的java代码

URL u = new URL("http://localhost/printPdf.Html");
URLConnection uc = u.openConnection();
BufferedInputStream is = new BufferedInputStream(uc.getInputStream());
File outs = new File("D:/HtmlToPdf.pdf")
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(outs));
byte[] b = new byte[8 * 1024];
int read = 0;
while ((read = is.read(b)) > -1) {
bout.write(b, 0, read);
}
bout.flush();
bout.close();
is.close();

这是我的 html 文件

<html>
<head></head>
<body><div>Hi !!! Example PDF </div></body>
</html>

当我打开 pdf 时出现此错误

Adobe Reader 无法打开,因为它不是受支持的文件类型或文件已损坏(例如,它是作为电子邮件附件发送的并且未正确解码)。

【问题讨论】:

标签: java html pdf-generation


【解决方案1】:

您不能简单地将一种文件格式写入另一种格式。您需要按照 PDF 规范的方式编写 HTML。

这是一个您可以使用的 PDF 库:http://pdfbox.apache.org/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 2018-05-20
    • 2011-08-31
    • 2015-08-21
    • 2014-02-09
    • 2011-07-10
    • 1970-01-01
    相关资源
    最近更新 更多