PDFBox:在tomcat上运行时无法保存pdf

时间:2014-10-24 13:35:05

标签: tomcat servlets pdfbox

This PDFBOX Example当我从主方法&运行java应用程序然后它成功保存pdf文档。但是如果在Tomcat服务器上运行servlet的doGet方法时使用相同的代码,那么它将保存pdf文件。你能帮我解决原因吗?我是否需要添加一些外部库以支持Tomcat服务器,请帮助..

注意:

  • 我调试了doGet方法,它的调用没有任何问题
  • 异常Tomcat 6,PDFBox 1.87,我拥有的所有PDFbox依赖 包含在类路径中

    /* this is the servlet method which is saving pdf file, but the same if run from Main then it saves the "hello world.pdf"*/ 
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
          try { 
              PDDocument  document = new PDDocument();
                 PDPage page = new PDPage();
                 document.addPage( page ); 
                 // Create a new font object selecting one of the PDF base fonts
                 PDFont font = PDType1Font.HELVETICA_BOLD; 
                 // Start a new content stream which will "hold" the to be created content
                 PDPageContentStream contentStream = new PDPageContentStream(document, page); 
                 // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
                 contentStream.beginText();
                 contentStream.setFont( font, 12 );
                 contentStream.moveTextPositionByAmount( 100, 700 );
                 contentStream.drawString( "Hello World1" );
                 contentStream.endText(); 
                 // Make sure that the content stream is closed:
                 contentStream.close(); 
                 // Save the results and ensure that the document is properly closed:
                 document.save( "Hello World.pdf");
    
                 document.close();
    
                 response.setContentType("text/html"); 
              PrintWriter out = response.getWriter();   
                  out.println("<iframe height=\"100%\"   width=\"100%\" src=\"http://eurecaproject.eu/files/4613/9886/3802/report3.pdf\" ></iframe>");
    
    
        } catch (Exception e) {
            // TODO Auto-generated catch block
    
            System.out.println(e);
        }   
    
    }
    

2 个答案:

答案 0 :(得分:0)

如果你更新到最新版本的PDFBox(2.0.0-SNAPSHOT),一切都应该没问题。

答案 1 :(得分:0)

以下是示例代码:抱歉,我有点忙,所以我现在无法测试您的代码,但我相信这会对您有所帮助。只需在致电doGet()doPost()时添加几行,只需检查一下我使用的内容即可。我花了很多时间才找到这个解决方案。它肯定会奏效。如果它不起作用,请告诉我。

我希望您已经下载了pdfbox jar,因此您可以默认导入类。

//you can use servlet to create pdf 

@SuppressWarnings("javadoc")

public class Billing extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        performTask(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        performTask(request, response);
    }

    private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {



            //Create pdf
            PDDocument document = new PDDocument();

            //Create Page
            PDPage page = new PDPage();

            //Adding the page
            document.addPage(page);


           //Loading the page
           File file = new File("D:/akash/my_doc.pdf");
           //writing text
           contentStream.beginText();
           contentStream.newLineAtOffset(295, 757);
           contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
           contentStream.showText("CHIMERA TRANSPLANT RESEARCH FOUNDATION");
           contentStream.endText();

           //Saving the document
           document.save("D:/akash/my_doc.pdf");


           //Closing the document
            document.close();
   }  
}