js将网页导出成pdf(js 打印指定div内容)2020-09-02

JavaScript011

js将网页导出成pdf(js 打印指定div内容)2020-09-02,第1张

<input type="button"  id="button" value="点击打印"/>

<div id="div_print">

<p>打印此处内容</p>

</div>

<script type="text/javascript">

function printdiv(printpage){

  var newstr=document.getElementById(printpage).innerHTML

  var oldstr=document.body.innerHTML

  document.body.innerHTML=newstr

  window.print()

  document.body.innerHTML=oldstr

  return false

}

window.onload=function(){

  var bt=document.getElementById("button")

  bt.onclick=function(){printdiv('div_print')}

}

</script>

html, body{

 overflow:visible  

}

打印时另存为pdf模式打印后的内容可复制 默认为Microsoft Print to PDF 模式

单击“文件”菜单中的“打印”命令,在弹出的“打印”对话框;选中“根据纸张调整大小”复选框,用户不需再设置幻灯片的大小,系统会根据所设置的纸张大小自动调整其大小;去掉“幻灯片加框”复选框。谢谢!

jsp中要利用java来实现打开,可以通过浏览器打开:

以下程序实现了读取某个路径下的pdf文件,并用浏览器打开:

package test

import java.io.File

import java.io.FileInputStream

import java.io.IOException

import java.io.OutputStream

import javax.servlet.ServletException

import javax.servlet.http.HttpServlet

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponse

public class PDFServlet extends HttpServlet {

private static final long serialVersionUID = -3065671125866266804L

public PDFServlet() {

super()

}

public void destroy() {

super.destroy()

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("application/pdf")

FileInputStream in = new FileInputStream(new File("d:/1.pdf"))

OutputStream out = response.getOutputStream()

byte[] b = new byte[512]

while ((in.read(b)) != -1) {

out.write(b)

}

out.flush()

in.close()

out.close()

}

public void init() throws ServletException {

}

}