将PDF打印到div中

时间:2011-06-08 19:26:17

标签: html jsp

当我在浏览器上打开PDF时,我想在div中打印它而不是整个页面。 我怎样才能做到这一点?这是我的JSP源代码:

<%@ page language="java" import="com.search.ts.*
                                ,java.io.*
                                ,java.net.*
                                ,javax.xml.namespace.QName
                                ,javax.jws.*
                                ,javax.xml.ws.* "
                                                   contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ebook reader</title>

<%@ page language="java" import="com.search.ts.CallSEI_CallSPort_Client,java.util.*,com.search.ts.Links,com.search.ts.LinksResponse"  %>

<link rel="stylesheet" type="text/css" href="style.css" />

</head>
<body>

            <div id="right_section">
              <div class="right_box">

<% 

        String filename= request.getParameter("err");
        //String filename =(String) request.getAttribute("linkbook");
        File file = new File("F:/fichiers/", filename+".pdf");

        response.setContentType(getServletContext().getMimeType(file.getName()));
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            input = new BufferedInputStream(new FileInputStream(file));
            output = new BufferedOutputStream(response.getOutputStream());

            byte[] buffer = new byte[8192];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
        } finally {
            if (output != null) try { output.close(); } catch (IOException ignore) {}
            if (input != null) try { input.close(); } catch (IOException ignore) {}
        }


        %>


          </div>
 </div>


</body>
</html>

3 个答案:

答案 0 :(得分:0)

有更简单的选项,涉及的代码更少....看看 http://www.webdeveloper.com/forum/showthread.php?t=152923

答案 1 :(得分:0)

你想要的是一个object / embed标签。

检查出来

http://blogs.adobe.com/pdfdevjunkie/2007/08/using_the_html_embed_tag_to_di.html

概要是,您希望像提供图像一样来源PDF:

<object type="application/pdf" data="myfile.pdf" width="500" height="650" ><embed src="myfile.pdf"/>
</object>

答案 2 :(得分:0)

我似乎在prbm你必须从第一个pdf页面发送一个带有pdf(myfile.pdf)名称的链接,就像那样:

<a href="pdfread.jsp?err=<%=filename %>"><%=bookName %> </a> 

到页面pdfread.jsp并在此页面中放置

<%
    String filename= request.getParameter("err");
%>
<embed src="${pageContext.request.contextPath}/pdfreader/<%=filename %>#toolbar=0&navpanes=0&scrollbar=0" width="500" height="375">
  </embed>

这个代码你必须把它放在一个带有do get

的servlet中
    String filename= request.getParameter("err");
    //String filename =(String) request.getAttribute("linkbook");
    File file = new File("F:/fichiers/", filename+".pdf");

    response.setContentType(getServletContext().getMimeType(file.getName()));
    response.setContentLength((int) file.length());
    response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        input = new BufferedInputStream(new FileInputStream(file));
        output = new BufferedOutputStream(response.getOutputStream());

        byte[] buffer = new byte[8192];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
    } finally {
        if (output != null) try { output.close(); } catch (IOException ignore) {}
        if (input != null) try { input.close(); } catch (IOException ignore) {}
    }

请参阅此链接以执行此操作

How to use doGet in jsp with Servlet