flying-saucer/iText PDF in servlet not finding css file HTML生成PDF未加载css

来源:互联网 发布:淘宝登陆界面异常 编辑:程序博客网 时间:2024/05/06 15:13


当flying-saucer使用模板文件加载html代码 生成PDF时,如果css样式使用了相对路径这会出现加载不上的问题,

类似问题http://stackoverflow.com/questions/9722038/flying-saucer-itext-pdf-in-servlet-not-finding-css-file


例如:

<link href="/css/home.css" type="text/css" rel="stylesheet"/> 

解决方法:

1.将stylesheet 的连接地址写成完整的URL地址,如http://..../css/home.css即可

<link href="http://..../css/home.css" type="text/css" rel="stylesheet"/> 
本应用地址生成代码:
HttpServletRequest request = getRequest();String path = request.getContextPath();String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path+ "/";

2.也可以写成是绝对地址

<link href="file:/E:\\home.css" type="text/css" rel="stylesheet"/> 

3.可以再读取模板是就将css读出来,拼接模板文件中

类似:

buf.append("<head><style>");buf.append(readFile(getServletContext().getRealPath("/PDFservlet.css"), "UTF-8"));buf.append("</style></head>");
4.使用document生成PDF时,可以设置相对url地址

例如:

  • Your document has <link href="my.css" ..
  • The css is located at http://example.com/something/my.css
  • You should call renderer.setDocument(doc, "http://example.com/something/page.html");

0 0
原创粉丝点击