代码片段位于问题的底部
我能够使用 servlet 和飞碟/iText 在客户端的浏览器中成功呈现 PDF。但是,我不知道如何获取 PDF 渲染中包含的样式表。
我已经尝试了以下方法,但到目前为止没有一个有效:
- getServletContext().getRealPath("/PDFservlet.css") 并将 "PDFservlet.css" 放在 web 根目录
buf.append("<head><link rel='stylesheet' type='text/css' href='PDFservlet.css' /></head>")
并将“PDFservlet.css”放在web根目录下,即servlet类文件所在的目录和“classes”目录的正下方- 与上述相同,除了
href='\PDFservlet.css'
将“PDFservlet.css”放在我的 Web 根目录下的各个位置
不确定在此处尝试什么以及如何在客户端浏览器中呈现 PDF 时识别此 CSS 表。
你们谁能告诉我我在这里做错了什么?
另外,我java.io.IOException: Stream closed
在 PDF 渲染后得到了一个,但不确定它来自哪里。
我正在本地对此进行测试并运行 WebLogic Server 10.3.3。
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws javax.servlet.ServletException, java.io.IOException
{
resp.setContentType("application/pdf");
StringBuffer buf = new StringBuffer();
buf.append("<html>");
// put in some style
buf.append("<head><link rel='stylesheet' type='text/css' href='PDFservlet.css' /></head>");
// generate the rest of the HTML...
buf.append("<body>");
buf.append(" <div id='container'>");
buf.append(" <div id='check_num'>1000</div>");
buf.append(" <div id='address'><b>Estate Of JAMES SMITH</b><br />35 Addison Avenue<br />New York, NY 00000<br />(123)456-7890</div>");
buf.append(" <div id='date'><i>Date</i> <u>02/08/2012</u></div>");
buf.append(" <div id='void_message'><b>VOID 180 DAYS FROM CHECK DATE</b></div>");
buf.append(" <div id='pay_line_container'>");
buf.append(" <div id='pay_line_message'><i>Pay To The Order Of:</i></div>");
buf.append(" <div id='pay_line'></div>");
buf.append(" <div id='pay_line_pay_to'>Richard Richards XXIII</div>");
buf.append(" <div id='pay_line_amount'>$ 5.00</div>");
buf.append(" </div>");
buf.append(" <div id='pay_line2_container'>");
buf.append(" <div id='pay_line2'></div>");
buf.append(" <div id='pay_line2_amount_description'>Five and 00/100</div>");
buf.append(" <div id='pay_line2_dollars'>DOLLARS</div>");
buf.append(" </div>");
buf.append(" <div id='void_stamp'><b>VOID</b></div>");
buf.append(" <div id='for_line'><i>For:</i> <u>test</u></div>");
buf.append(" <div id='bank_info'><b>TD BANKNORTH</b><br />MAINE</div>");
buf.append(" <div id='signature_line'></div>");
buf.append(" <div id='bank_numbers'><b>c1000c a123456789a 987654321c</b></div>");
buf.append(" </div>");
buf.append("</body>");
buf.append("</html>");
System.out.println(buf.toString());
// parse our markup into an xml Document
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new StringBufferInputStream(buf.toString()));
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.layout();
OutputStream os = resp.getOutputStream();
renderer.createPDF(os);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
编辑我
将服务器端代码上的 CSS 文件的内容读入字符串对我来说是一种替代方法(该readFile
方法基于 Jon Skeet 在How do I create a Java string from the contents of a file?上的帖子):
buf.append("<head><style>");
buf.append(readFile(getServletContext().getRealPath("/PDFservlet.css"), "UTF-8"));
buf.append("</style></head>");
作为替代方案,这似乎是一种不错的方法吗?
编辑二
我为 iText 创建了一个聊天室,希望你们中的一些人可以每隔一段时间看看。我有几个关于 iText/flying-saucer 的问题,我认为在你们中的一些专家的帮助下可能很容易解决。如果您有机会,请查看并发布您可能针对 iText 问题提供的任何有用材料:https ://chat.stackoverflow.com/rooms/8945/itext