【发布时间】: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 无法打开,因为它不是受支持的文件类型或文件已损坏(例如,它是作为电子邮件附件发送的并且未正确解码)。
【问题讨论】:
-
将数据发送到名为 .PDF 的文件不会生成 PDF 文件。关于 SO 上的 PDF 生成有几个问题。比如这样:stackoverflow.com/questions/7355025/create-pdf-with-java
标签: java html pdf-generation