zoe984623 2008-08-28 12:18
浏览 289
已采纳

关于如何在网页里打开pdf文件

加入我点击按钮,后台生成一个pdf文件。

我不要它出现另存为的询问弹出框,而是在网页里直接显示,该如何实现?

要求如果本地没有安装adobe reader等软件,提示客户下载安装浏览器插件后显示。
[b]问题补充:[/b]
我曾经见过在网页里是可以打开单个pdf文件的

  • 写回答

3条回答 默认 最新

  • piccbj 2008-08-28 12:52
    关注

    这个还不容易。把PDF文件写入response流里面就可以了!
    方法有很多,这里给个独立又简单的例子:
    [code="java"]
    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;
    }
    

    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错