本帖最后由 wanliang2001 于 2010-07-19 16:47:44 编辑

解决方案 »

  1.   

    2008-08-28 zhaipuhong (资深程序员)
    这个还不容易。把PDF文件写入response流里面就可以了! 
    方法有很多,这里给个独立又简单的例子: Java代码 
    1.package com.zhaipuhong.j2se.pdf;   
    2.  
    3.import java.io.IOException;   
    4.import java.util.Date;   
    5.  
    6.import javax.servlet.ServletException;   
    7.import javax.servlet.http.HttpServlet;   
    8.import javax.servlet.http.HttpServletRequest;   
    9.import javax.servlet.http.HttpServletResponse;   
    10.  
    11.import com.lowagie.text.Document;   
    12.import com.lowagie.text.DocumentException;   
    13.import com.lowagie.text.Paragraph;   
    14.import com.lowagie.text.pdf.PdfWriter;   
    15.import com.lowagie.text.pdf.BaseFont;   
    16.import com.lowagie.text.pdf.PdfPTable;   
    17.import com.lowagie.text.pdf.PdfPCell;   
    18.import java.awt.Color;   
    19.  
    20.public class PdfServlet extends HttpServlet {   
    21.  
    22.    private static final long serialVersionUID = -6033026500372479591L;   
    23.  
    24.    public void doGet (HttpServletRequest request, HttpServletResponse response)   
    25.    throws IOException, ServletException {   
    26.  
    27.        // step 1  建立文档对象   
    28.        Document document = new Document();   
    29.        try {   
    30.                //设置文档相应类型   
    31.                response.setContentType("application/pdf");   
    32.                PdfWriter.getInstance(document, response.getOutputStream());   
    33.  
    34.  
    35.                // step 3  打开文档   
    36.                document.open();   
    37.                //支持中文   
    38.                BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);   
    39.                com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);   
    40.                Paragraph pragraph=new Paragraph("你好", FontChinese);   
    41.  
    42.  
    43.                // step 4  向文档中添加内容   
    44.                document.add(pragraph);   
    45.                document.add(new Paragraph(" Hello World !"));   
    46.                document.add(new Paragraph("Date 时间"+new Date().toString()));   
    47.                document.add(new Paragraph(new Date().toString()));   
    48.                document.add(new Paragraph(new Date().toString()));   
    49.  
    50.  
    51.  
    52.                PdfPTable table = new PdfPTable(3);   
    53.                        PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));   
    54.                        cell.setColspan(3);   
    55.                        table.addCell(cell);   
    56.                        table.addCell("1.1");   
    57.                        table.addCell("2.1");   
    58.                        table.addCell("3.1");   
    59.                        table.addCell("1.2");   
    60.                        table.addCell("2.2");   
    61.                        table.addCell("3.2");   
    62.                        cell = new PdfPCell(new Paragraph("cell test1"));   
    63.                        cell.setBorderColor(new Color(255, 0, 0));   
    64.                        table.addCell(cell);   
    65.                        cell = new PdfPCell(new Paragraph("cell test2"));   
    66.                        cell.setColspan(2);   
    67.                        cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));   
    68.                        table.addCell(cell);   
    69.                        document.add(table);   
    70.  
    71.        }catch(DocumentException de) {   
    72.            de.printStackTrace();   
    73.            System.err.println("document: " + de.getMessage());   
    74.        }   
    75.  
    76.        // step 5: 关闭文档对象   
    77.        document.close();   
    78.    }   
    79.  
    80.    //支持中文   
    81.    public Paragraph getChineseString(String chineseString){   
    82.      Paragraph pragraph=null;   
    83.      BaseFont bfChinese = null;   
    84.      try {   
    85.        bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",   
    86.                                        BaseFont.NOT_EMBEDDED);   
    87.        com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese,   
    88.            12, com.lowagie.text.Font.NORMAL);   
    89.         pragraph = new Paragraph(chineseString, FontChinese);   
    90.      }   
    91.      catch (Exception ex) {   
    92.        ex.printStackTrace();   
    93.      }   
    94.      return pragraph;   
    95.    }   
    96.}  
    package com.zhaipuhong.j2se.pdf;import java.io.IOException;
    import java.util.Date;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.pdf.PdfWriter;
    import com.lowagie.text.pdf.BaseFont;
    import com.lowagie.text.pdf.PdfPTable;
    import com.lowagie.text.pdf.PdfPCell;
    import java.awt.Color;public class PdfServlet extends HttpServlet {    private static final long serialVersionUID = -6033026500372479591L;    public void doGet (HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {        // step 1  建立文档对象
            Document document = new Document();
            try {
                    //设置文档相应类型
                    response.setContentType("application/pdf");
                    PdfWriter.getInstance(document, response.getOutputStream());
                    // step 3  打开文档
                    document.open();
                    //支持中文
                    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                    com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
                    Paragraph pragraph=new Paragraph("你好", FontChinese);
                    // step 4  向文档中添加内容
                    document.add(pragraph);
                    document.add(new Paragraph(" Hello World !"));
                    document.add(new Paragraph("Date 时间"+new Date().toString()));
                    document.add(new Paragraph(new Date().toString()));
                    document.add(new Paragraph(new Date().toString()));                PdfPTable table = new PdfPTable(3);
                            PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
                            cell.setColspan(3);
                            table.addCell(cell);
                            table.addCell("1.1");
                            table.addCell("2.1");
                            table.addCell("3.1");
                            table.addCell("1.2");
                            table.addCell("2.2");
                            table.addCell("3.2");
                            cell = new PdfPCell(new Paragraph("cell test1"));
                            cell.setBorderColor(new Color(255, 0, 0));
                            table.addCell(cell);
                            cell = new PdfPCell(new Paragraph("cell test2"));
                            cell.setColspan(2);
                            cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
                            table.addCell(cell);
                            document.add(table);        }catch(DocumentException de) {
                de.printStackTrace();
                System.err.println("document: " + de.getMessage());
            }        // step 5: 关闭文档对象
            document.close();
        }    //支持中文
        public Paragraph getChineseString(String chineseString){
          Paragraph pragraph=null;
          BaseFont bfChinese = null;
          try {
            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                                            BaseFont.NOT_EMBEDDED);
            com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese,
                12, com.lowagie.text.Font.NORMAL);
             pragraph = new Paragraph(chineseString, FontChinese);
          }
          catch (Exception ex) {
            ex.printStackTrace();
          }
          return pragraph;
        }
    }--------------------------------------------------------------------------------
    提问者对于答案的评价:
    试试