http://xml.apache.org/fop/index.html,这或许是你需要的。
什么叫“只能用servlet,不能用JSP”?这话透着有点莫名其妙。

解决方案 »

  1.   

    我的页面是用servlet生成的 ,公司不用JSP
      

  2.   

    Our example application uses these iText classes: com.lowagie.text.pdf.PdfWriter 
    com.lowagie.text.Document 
    com.lowagie.text.HeaderFooter 
    com.lowagie.text.Paragraph 
    com.lowagie.text.Phrase 
    com.lowagie.text.Table 
    com.lowagie.text.Cell 
    The key classes are Document and PdfWriter. You will always use both of these classes when creating PDF documents. Document is an object-oriented representation of a PDF document. You can add content to the document by invoking methods provided by the Document class. A PdfWriter object associates a Document with a java.io.OutputStream object. Coordinate System for iText Documents 
    When I wrote my first iText program, I stumbled over the coordinate system. I naively assumed that iText's coordinate system was identical to Swing's coordinate system. This is not the case. In Swing, the origin (0, 0) is located in the upper left-hand corner of a component. In iText, the origin is located in the bottom left-hand corner of a page. 
      
    Using iText in a Web Application 
    During your design phase, you must decide how you plan to use iText. I've built web applications using both of the following techniques. Technique A 
    Create the PDF file on the server's filesystem. The application uses java.io.FileOutputStream to write the file to the server's filesystem. The user will download the file via HTTP GET. Technique B 
    Create the PDF file in memory using java.io.ByteArrayOutputStream. The application sends the PDF bytes to the client via the servlet's output stream. Source Code Download the source code for this example: 
    pdfservlet-files.zip 
      
    I prefer technique B to technique A because the application does not write to the server's filesystem, and the application is guaranteed to work in a clustered server environment. Technique A can fail if your application runs in a clustered environment, and the server cluster does not provide session affinity. Example: PDFServlet 
    Our example application consists of a single class: PDFServlet. This servlet uses technique B from the previous section. The OutputStream is a java.io.ByteArrayOutputStream. With ByteArrayOutputStream, the PDF document bytes will be in memory. When PDFServlet receives an HTTP request, it will dynamically generate a PDF document and send the document to the client. The PDFServlet class extends javax.servlet.http.HttpServlet and imports two of the iText packages, com.lowagie.text and com.lowagie.text.pdf.  The doGet Method 
    Most servlets override either the doPost method or the doGet method. Our servlet is no different. The PDFServlet class overrides the doGet method. The servlet will generate a PDF file any time it receives an incoming HTTP GET request. In a nutshell, the servlet's doGet method does the following: Creates a ByteArrayOutputStream object that contains the PDF document bytes.  
    Sets the HTTP response headers on the response object.  
    Gets the servlet output stream.  
    Writes the document bytes to the servlet output stream.  
    Flushes the servlet output stream.  Figure 1. Editing doGet in Eclipse The generatePDFDocumentBytes Method 
    The generatePDFDocumentBytes method is responsible for creating the PDF document. The three most important objects in this method are the Document object, the ByteArrayOutputStream object, and the PdfWriter object. The PdfWriter associates the Document with the ByteArrayOutputStream. Document doc = new Document(); 
    ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(); 
    PdfWriter docWriter = null; 
    docWriter = PdfWriter.getInstance(doc, baosPDF); 
    // ... 
      

  3.   


    Adding content to a Document is done with the add method. doc.add(new Paragraph( 
        "This document was created by a class named: " 
        + this.getClass().getName())); doc.add(new Paragraph( 
        "This document was created on " 
        + new java.util.Date())); 
    When you are done adding content, close the Document and PdfWriter objects. doc.close(); 
    docWriter.close(); 
    After closing the document, the ByteArrayOutputStream object is returned to the caller. return baosPDF; 
    The ByteArrayOutputStream contains all bytes for the PDF document. HTTP Response Headers 
    In this application, we care only about four HTTP response headers: Content-type, Content-disposition, Content-length, and Cache-control. If you've never worked with HTTP headers before, consult the HTTP 1.1 specification. Examine the doGet method in the PDFServlet. You'll notice that the HTTP response headers are set before any data is written to the servlet output stream. This is an important, yet subtle, point. Let's look at each response header in more detail. Content-type 
    In servlets, HttpServletResponse has a content type that indicates the type of content that the response contains. For PDF files, the content type is application/pdf. If the servlet does not set a content type, the web browser may have a difficult time determining how to handle the file. PDFServlet sets the content type with the following line:  resp.setContentType("application/pdf"); 
    Content-disposition 
    The Content-disposition header provides information that helps a web browser identify the content of the HTTP response. When a web browser reads this header, it can determine: That the HTTP response contains a file.  
    The name of the file contained in the response.  
    Whether the file should be displayed inside of the browser's main window or viewed by an external application.  
    RFC 2183 provides a full explanation of the Content-disposition header. By setting the Content-disposition header appropriately, the servlet can instruct the browser to display the file "inline," or to treat it like an attachment. Example 1. Displaying a file inline Content-disposition: inline; filename=foobar.pdf 
    Example 2. Attaching a file to the response Content-disposition: attachment; filename=foobar.pdf 
    The following pseudo-code demonstrates how to set the header: public void doGet(HttpServletRequest req, HttpServletResponse resp) 

    // ...  
    resp.setHeader( 
    "Content-disposition", 
    "inline; filename=foobar.pdf" ); 
    // ...  

    Cache-Control Headers 
    Depending upon the nature of your application, you may or may not want web browsers to cache the PDF files that you are generating. There are a variety of HTTP headers that a server-side web application can use to control caching of content. Some examples are: Cache-Control: no-cache  
    Cache-Control: no-store  
    Cache-Control: must-revalidate  
    Cache-Control: max-age=30  
    Pragma: no-cache  
    Expires: 0  
    A full explanation of Cache-Control headers is found in the HTTP 1.1 specification. The PDFServlet sets Cache-Control to max-age=30. This header tells the web browser to cache the file for a maximum of 30 seconds. Content-length 
    The Content-length header must be set to the number of bytes in the PDF file. If the Content-length header is not set correctly, the web browser may not be able to display the file. Example code might be: ByteArrayOutputStream baos = getByteArrayOutputStream(); 
    resp.setContentLength(baos.size()); 
    Sending the PDF Document to a Web Browser 
    PDFServlet sends the PDF document to the client by writing bytes to the servlet's output stream. It obtains the output stream by calling getOutputStream() on the HttpServletResponse object. getOutputStream returns an object of type javax.servlet.ServletOutputStream. ServletOutputStream sos; 
    sos = resp.getOutputStream(); 
    baos.writeTo(sos); 
    sos.flush(); 
    After writing all data to the stream, call the flush() method to send all bytes to the client. Packaging and Deployment 
    To run the PDFServlet in Tomcat, you'll need to package the application in a WAR file. The iText JAR file (itext-0.99.jar) must be placed in the WAR file's lib directory. If you forget to include the iText JAR file, the servlet will fail with a java.lang.NoClassDefFoundError. Running the Application 
    After the WAR file has been deployed, you are ready to test the servlet. Jakarta Tomcat listens for requests on port 8080. Point your web browser to http://hostname:8080/pdfservlet/createpdf. When you visit the URL, the servlet executes and sends a PDF document back to your browser. Beyond iText 
    iText provides a great low-level API for producing PDF documents. However, it may not be the best tool for every application. At my day job, we used iText in combination with Microsoft Word and Adobe Acrobat. First, our team designed a shipment form using Microsoft Word. Next, we converted the Word document to PDF using Adobe Acrobat. Then, using iText's template capability, we loaded the PDF file into our application. From there, it was quite easy to fill in data values on the form and output the final PDF document. For report-oriented web applications, tools like JasperReports provide a higher level of abstraction than iText. Conclusion 
    When your Java application needs to dynamically create PDF documents, the iText class library is a great solution. You can experiment with iText's capabilities by enhancing and extending the code in this article. In a short time, you'll be able to impress your co-workers and customers with sophisticated PDF documents
      

  4.   

    JAVA报表处理的好东西,值得收藏的
      

  5.   

    在下有刚做完的源程序  不过不在手头 iTEXt+servlet的
      

  6.   

    支持一下! jscsqb() 可不可以给小弟发一下啊?多谢了!
      

  7.   

    to  Keepers(中文昵称) :
      老兄 你是中国人吗?? 你的英语很榜呀,我对你的敬仰如滔滔江水
    连绵不绝,一发而不可收拾......
      感谢你的代码,上述功能我以基本可以实现。
      
    按照上述方法,我有一种报表就要画一个pdf,有10个画10个,
    有没有方法可以直接接受html 页面生成pdf,就像xml转pdf那样,PdfWriter.getInstance(document, new FileOutputStream("cv.pdf"));
    XmlParser.parse(document, "cv.xml");直接可以把cv.xml文件生成pdf。
    我现在想找一种方法可以把html直接转成pdf,不知有无此方法???不胜感激。
      

  8.   

    恐怕还得自己一句一句编程,这也是最直接的方法。
    你也可以写个程序调用xml中的数据,然后根据这些数据在java中生成pdf文件,
    因为数据可以时刻改变,所以这已经是动态的生成pdf文件了。
    不过归根结底这些都是你自己变成实现的。
       如果报表格式改变(而不仅仅是数据改变),那应该是重画pdf的。如果想要一劳永逸,
    似乎应该编一个转化程序,记录报表格式的文件一改变,就产生相应的java代码,这不能说
    实现不了,但是估计不能马上完成吧。
       以上Keepers(中文昵称) 所发的英文虽然长了一些,但这是iText教程上所没有的,
    是告诉我们如何在jsp中生成PDF文件,而不是通过application生成(就像教程上的那样)。
    我这里的的源码是Servlet的,但是JSP本身就是Servlet(在tomcat中,jsp被服务器解释成servlet)