在servlet中找不到css文件的fly-saucer / iText PDF [英] flying-saucer/iText PDF in servlet not finding css file

查看:144
本文介绍了在servlet中找不到css文件的fly-saucer / iText PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码片段位于问题的底部



我能够成功地使用servlet在客户端的浏览器中呈现PDF和飞碟/ iText。但是,我无法弄清楚如何获得PDF渲染中包含的样式表。



我尝试了以下内容,但到目前为止还没有任何工作:




  • getServletContext()。getRealPath(/ PDFservlet.css)并将PDFservlet.css放在网络根目录

  • 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:在关闭PDF后流关闭并且不知道它来自何处。



<我正在本地测试并运行WebLogic Server 10.3.3。

  public void doGet(HttpServletRequest req,HttpServletResponse resp) 
抛出javax.servlet.ServletException,java.io.IOException
{
resp.setContentType(application / pdf);

StringBuffer buf = new StringBuffer();
buf.append(< html>);

//放入某种风格
buf.append(< head>< link rel ='stylesheet'type ='text / css'href ='PDFservlet.css'/ >< /头>中);

//生成剩下的HTML ...
buf.append(< body>);
buf.append(< div id ='container'>);
buf.append(< div id ='check_num'> 1000< / div>);
buf.append(< div id ='address'>< b>庄园的JAMES SMITH< / b>< br /> 35 Addison Avenue< br />纽约,纽约00000< ; br />(123)456-7890< / div>);
buf.append(< div id ='date'>< i> Date< / i>&#160;< u> 02/08 / 2012< / u>< / div> ;);
buf.append(< div id ='void_message'>< b> VOID 180天退房日期< / b>< / div>);
buf.append(< div id ='pay_line_container'>);
buf.append(< div id ='pay_line_message'>< i>支付给订单:< / 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'> $&#160; 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>&#160;< 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());

//将我们的标记解析为xml文档
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的帖子如何根据文件内容创建Java字符串?):

  buf .append( < HEAD><风格> 中); 
buf.append(readFile(getServletContext()。getRealPath(/ PDFservlet.css),UTF-8));
buf.append(< / style>< / head>);

这看起来像是一种好方法吗?



编辑II



我为iText创建了一个聊天室,我希望你们中的一些人可以每隔一段时间看一次。我有一些关于iText /飞碟的问题,我认为在一些专家的帮助下可能很容易解决。请查看您是否有机会发布有关iText问题的任何有用材料: http:// chat .stackoverflow.com / rooms / 8945 / itext

解决方案

我决定只是在服务器上阅读我的CSS文件支持字符串。



readFile 方法基于Jon Skeet在如何根据文件内容创建Java字符串? ):

  buf.append(< head>< style>); 
buf.append(readFile(getServletContext()。getRealPath(/ PDFservlet.css),UTF-8));
buf.append(< / style>< / head>);


A code snippet is at the bottom of the question

I am successfully able to render a PDF in the client's browser using a servlet and flying-saucer/iText. However, I can't figure out how to get the style sheet included in the PDF rendering.

I've tried the following and none have worked thus far:

  • getServletContext().getRealPath("/PDFservlet.css") and putting "PDFservlet.css" at the web root directory
  • buf.append("<head><link rel='stylesheet' type='text/css' href='PDFservlet.css' /></head>") and putting "PDFservlet.css" at the web root directory, the directory where the servlet class file is and right under the "classes" directory
  • Same as above except for href='\PDFservlet.css' and putting "PDFservlet.css" in various places under my web root directory

Not sure what else to try here and how to get this CSS sheet recognized when the PDF is rendered in the client's browser.

Can any of you tell me what I'm doing wrong here?

Also, I'm getting a java.io.IOException: Stream closed after the PDF is rendered and not sure where that's coming from.

I'm testing this locally and running 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>&#160;<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'>$&#160;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>&#160;<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();
    }
}

Edit I

Reading the contents of the CSS file on the server side code into a String is working for me as an alternative (the readFile method is based off of Jon Skeet's post at 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>");

Does this seem like an OK approach as an alternative?

Edit II

I've created a chat room for iText that I'm hoping some of you may take a look in every once in awhile. I've had several issues with iText/flying-saucer that I think may be easy to resolve with help from some of you experts. Please take a look if you get a chance and post whatever helpful material you might be able to for iText issues: http://chat.stackoverflow.com/rooms/8945/itext

解决方案

I decided with just reading my CSS file on the server side into a String.

The readFile method is based off of Jon Skeet's post at 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>");

这篇关于在servlet中找不到css文件的fly-saucer / iText PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆