HTML转PDF

qq_25288595 2015-09-07 09:32:01
已经利用 var html=document.documentElement.outerHTML;取到jsp页面的HTML部分,将此部分传到后台,如何把他生成PDF格式为文件!!!


部分文件代码:
function htmCon() {
var html=document.documentElement.outerHTML;
document.getElementById("html").value=html;
alert(html);
}

<\script>
<\body><\html>">
<button type="submit" id="login-button2" onclick="htmCon()">下载<\button>
<\form>


<script>

\ alert(reportData);
init_converters();
get_data();
function htmCon() {
var html=document.documentElement.outerHTML;
document.getElementById("html").value=html;
alert(html);
}

<\script>
<\body>
<\html>
...全文
185 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuangqingch 2015-09-09
  • 打赏
  • 举报
回复
引用 6 楼 qq_25288595 的回复:
这个问题解决了,,貌似在解析HTML页面的时候,会报出各种页面不符合规范的要求!!各位大哥,有没有好的获取jsp页面内容转换为HTML的方法。
页面不符合规则只能说明是你的JSP编写不规范或者解析HTML时没处理好。jsp转html无非就是提取响应结果中的Html内容。结果楼主想要实现的功能,可以参考这篇博文:iText和flying saucer结合生成pdf的技术
qq_25288595 2015-09-09
  • 打赏
  • 举报
回复
这个问题解决了,,貌似在解析HTML页面的时候,会报出各种页面不符合规范的要求!!各位大哥,有没有好的获取jsp页面内容转换为HTML的方法。
qq_25288595 2015-09-07
  • 打赏
  • 举报
回复
我的后台是这样处理的: protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ String html="<!DOCTYPE HTML>"+new String(req.getParameter("html").getBytes("ISO-8859-1"),"utf-8"); System.out.print(new File(html)); String outputFile = "E://DataConnection.pdf"; OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(new File(html)); // 解决中文支持问题 ITextFontResolver fontResolver = renderer.getFontResolver(); try { fontResolver.addFont("C:\\Windows\\Fonts\\simsunb.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // 解决图片的相对路径问题 renderer.getSharedContext().setBaseURL("file:/D:/Work/Demo2do/Yoda/branch/Yoda%20-%20All/conf/template/"); renderer.layout(); renderer.createPDF(os); os.close(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } 但是走到 renderer.setDocument(new File(html)); 这一步会报一个流程关闭的错误
zhuangqingch 2015-09-07
  • 打赏
  • 举报
回复
引用 4 楼 rui888 的回复:
[quote=引用 2 楼 zhuangqingch 的回复:] 谢邀,renderer.setDocument(new File(html));这段代码中的new File(html)写错了。 html变量存储的是你要处理的html内容,被你当成文件路径来使用了。你可以修改为以下代码:

File file = new File("temp.html");
FileOutputStream out =  new FileOutputStream(file);
out.write(html.getBytes());
out.close();
renderer.setDocument(file);
那个是系统邀请的吧。[/quote] 应该是了,对CSDN的BBS系统不熟悉
tony4geek 2015-09-07
  • 打赏
  • 举报
回复
引用 2 楼 zhuangqingch 的回复:
谢邀,renderer.setDocument(new File(html));这段代码中的new File(html)写错了。 html变量存储的是你要处理的html内容,被你当成文件路径来使用了。你可以修改为以下代码:

File file = new File("temp.html");
FileOutputStream out =  new FileOutputStream(file);
out.write(html.getBytes());
out.close();
renderer.setDocument(file);
那个是系统邀请的吧。
zhuangqingch 2015-09-07
  • 打赏
  • 举报
回复
引用 2 楼 zhuangqingch 的回复:
谢邀,renderer.setDocument(new File(html));这段代码中的new File(html)写错了。 html变量存储的是你要处理的html内容,被你当成文件路径来使用了。你可以修改为以下代码:

File file = new File("temp.html");
FileOutputStream out =  new FileOutputStream(file);
out.write(html.getBytes());
out.close();
renderer.setDocument(file);
补充下,其实没必要采用setDocument(File file)这个方法,如果是直接处理html内容,可以直接用 renderer.setDocumentFromString(html);,即你只需要把renderer.setDocument(new File(html));修改为renderer.setDocumentFromString(html);就OK了
zhuangqingch 2015-09-07
  • 打赏
  • 举报
回复
谢邀,renderer.setDocument(new File(html));这段代码中的new File(html)写错了。 html变量存储的是你要处理的html内容,被你当成文件路径来使用了。你可以修改为以下代码:

File file = new File("temp.html");
FileOutputStream out =  new FileOutputStream(file);
out.write(html.getBytes());
out.close();
renderer.setDocument(file);

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧