是这样的,项目要求将当前已打开的JSP页面通过该页面上的一个按钮保存成PDF格式的文件,并且保存到服务器上,请问各位应该怎么实现啊?

解决方案 »

  1.   

    直接通过JSP页面生成
    <%@ 
    page import="java.io.*,java.awt.Color,com.lowagie.text.*,com.lowagie.text.pdf.*"%>
    <%
     response.setContentType( "application/pdf" );
     Document document = new Document();
     ByteArrayOutputStream buffer = new ByteArrayOutputStream();
     PdfWriter writer=PdfWriter.getInstance( document, buffer );
     document.open();
     document.add(new Paragraph("Hello World"));
     document.close();
     DataOutput output = new DataOutputStream( response.getOutputStream() );
     byte[] bytes = buffer.toByteArray();
     response.setContentLength(bytes.length);
     for( int i = 0; i < bytes.length; i++ ) 
     {
      output.writeByte( bytes[i] ); 
     }
    %>  
    需要引入的包是ITEXT一个开源项目,用来生成PDF,原理就是服务器的response作为文件流用ITEXT写入PDF
      

  2.   

    保存成pdf格式不是难事,关键是怎样保存到服务器上。