2012-03-15 58 views
2

代碼片斷是在問題飛碟/ iText的在servlet的PDF沒有找到CSS文件

的底部,我能成功使用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」放在不同的地方在我的網站根目錄下

不知道還有什麼可以嘗試的,以及如何在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>&#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(); 
    } 
} 

編輯我

閱讀在服務器端代碼中CSS文件的內容爲一個字符串爲我工作作爲替代(該readFile方法是基於關閉喬恩斯基特的帖子在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>"); 

這是否看起來是一個好辦法來替代?

編輯II

我已經創造了iText的一個聊天室,我希望你們中的一些可能看一看在每過一段時間。我在iText /飛碟上遇到過幾個問題,我想在你們的專家的幫助下可能很容易解決。請看看你是否有機會發布一些你可能會遇到的iText問題的有用資料:http://chat.stackoverflow.com/rooms/8945/itext

回答

1

我剛剛在服務器端讀取我的CSS文件到一個String中。

readFile方法在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>"); 
4

有多種方式來解決這個問題,一個是你,別人是:

1:設置網址你的文件。你打電話renderer.setDocument(doc, null);第二個參數是基礎url,資源將與那個相關聯。

一個例子:

  • 您的文檔<link href="my.css" ..
  • 的CSS位於http://example.com/something/my.css
  • 你應該叫renderer.setDocument(doc, "http://example.com/something/page.html");

2:實現UserAgentCallback接口,並renderer.getSharedContext().setUserAgentCallback(myUserAgentCallback);設置

+0

「第二個參數是你的文檔的URL」 - 我的文檔在OutputStream中呈現,所以我不確定這會是什麼。現在,我將從Servlet返回的OutputStream轉換爲iframe。 – 2012-03-16 18:08:42

+0

它不會(也不能)驗證,您正在告訴飛碟作爲查找相關網址的基礎網址。 (我已經用這個信息更新了我的答案) – Adam 2012-03-16 18:29:11