PDFBox:在 tomcat 上运行时无法保存 pdf [英] PDFBox: Unable to save pdf while running on tomcat

查看:77
本文介绍了PDFBox:在 tomcat 上运行时无法保存 pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个 PDFBOX 示例 当我从主要方法运行时 &运行java应用程序然后它成功保存pdf文档.但是,如果在 Tomcat 服务器上从 servlet 的 doGet 方法运行时使用相同的代码,则它不会保存 pdf 文件.很困惑你能帮我为什么吗?我是否需要添加一些外部库来支持 Tomcat 服务器,请帮忙..

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..

注意:

  • 我已经调试了 doGet 方法,它的获取调用没有抛出任何
  • 异常 Tomcat 6、PDFBox 1.87、我拥有的所有 PDFbox 依赖项包含在类路径中

  • 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);
    }   

}

推荐答案

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

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.

希望你已经下载了pdfbox jar,这样你就可以默认导入类了.

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

这篇关于PDFBox:在 tomcat 上运行时无法保存 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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