PDFBox: Unable to save pdf while running on tomcat

烂漫一生 提交于 2019-12-11 11:06:23

问题


This PDFBOX Example when i run from the main method & run java application then it successfully saving pdf document. But if the same code while running from doGet method of servlet on Tomcat server, then its NOT saving the pdf file. Very confuse can you help me why? do i have to add some external libarary to support for Tomcat server, please help..

Note :

  • i have debugged doGet method its getting call not throuwing any
  • exception Tomcat 6, PDFBox 1.87, all PDFbox dependecies i have included in classpath

    /* 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);
        }   
    
    }
    

回答1:


If you update to the latest version of PDFBox (2.0.0-SNAPSHOT), everything should be ok.




回答2:


Here is the example code: Sorry I am little bit busy so I can't test your code right now, but I am sure this will help you. Just add few lines while call doGet() or doPost() just check once what I am using. I spent many hours to find this solution. It will definitely work. If it won't work, please let me know.

I hope that you have already downloaded the pdfbox jar, so then you can import classes by default.

//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();
   }  
}


来源:https://stackoverflow.com/questions/26548973/pdfbox-unable-to-save-pdf-while-running-on-tomcat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!