浏览器打印PDF去除浏览器自动生成部分
业务要求:
浏览器先预览PDF,点击打印调出打印机进行打印,需要去除浏览器自动生成的多余的部分广告。可以下载PDF。
说明:这里的代码是jsp,根据需要可以手动修改代码,去除jsp的标记就为原生的h5,复制代码可以直接使用。js使用的CDN
下图是生成的pdf示例:
代码部分(jsp)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>预览</title>
<meta http-equiv="X-UA-Compatible" content="IE=11" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<style type="text/css">
@media print {
.noprint {
display: none;
}
}
</style>
<body>
<p class="noprint">
<!-- 不需要打印的地方 -->
<input id="printBtn" class="print-link no-print" type="button" value="打印" onclick="doPrint()" />
</p>
<div id="myPrintArea">
<!-- 这里是获取PDF的位置,也是展示PDF使用的,其中src指的是PDF生成的链接,可以直接是服务的pdf文件,也可以是服务器直接返回pdf流,都是可以读取到的。 -->
<iframe style="" width="100%" height="100%" src="${pageContext.request.contextPath}/pdfurl" id="displayPdfIframe"></iframe>
</div>
</body>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var pdfresult=isAcrobatPluginInstall();
if(!pdfresult){
$("#displayPdfIframe").attr("src","");
// 禁用打印按钮
$("#printBtn").attr("disabled","disabled");
// 未安装跳转到下载安装地址
$.messager.confirm('提示','对不起,您还未安装PDF阅读器软件,为预览PDF文档,需安装该软件,请确认是否安装!',function(result){
if(result) {
// PDF软件离线安装包
window.location.href = '${staticPath}/static/ex/ +ternalApplication/AcroRdrDC.exe';
} else {
window.opener=null;
window.open('','_self');
window.close();
}
});
return;
}
var src = $("#displayPdfIframe").attr("src");
if (!src) {
$.messager.alert('提示', "生成PDF文件失败!", 'warning');
}
});
function doPrint() {
var src = $("#displayPdfIframe").attr("src");
if (!src) {//当src为空,即第一次加载时才赋值,如果是需要动态生成的话,那么条件要稍稍变化一下
// $.messager.alert('提示', "获取PDF文件失败!", 'warning');
} else {
var browserType = BrowserType();
if (("Chrome" == browserType)) {
$("#displayPdfIframe")[0].contentWindow.print();
}
if (("FF" == browserType)) {
$("#displayPdfIframe")[0].contentWindow.print();
}
if ("IE8910" == browserType) {
printPage();
}
if ("IE11" == browserType) {
printPage();
}
}
}
/*使用IE浏览器打印*/
function printPage() {
beforePrint();
callPrint();
afterPrint();
}
function beforePrint() {
document.getElementById("printBtn").style.display = "none";
}
function afterPrint() {
document.getElementById("printBtn").style.display = "inline-block";
}
function callPrint() {
var srcBody = '', startPrintMark = '', endPrintMark = '', printContent = '';
srcBody = document.frames["displayPdfIframe"].document.execCommand('print');
window.location.reload();
}
function BrowserType() {
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器
if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
return "IE8910";
}
if ((!!window.ActiveXObject) || ("ActiveXObject" in window)) {
return "IE11";
}
if (isFF) {
return "FF";
}
if (isChrome) {
return "Chrome";
}
}
function isAcrobatPluginInstall() {
//下面代码都是处理IE浏览器的情况
if((window.ActiveXObject)||(navigator.userAgent.indexOf("Trident") > -1)) {
for(x = 2; x < 10; x++) {
try {
oAcro = eval("new ActiveXObject('PDF.PdfCtrl." + x + "');");
if(oAcro) {
return true;
}
} catch(e) {}
}
try {
oAcro4 = new ActiveXObject('PDF.PdfCtrl.1');
if(oAcro4)
return true;
} catch(e) {}
try {
oAcro7 = new ActiveXObject('AcroPDF.PDF.1');
if(oAcro7)
return true;
} catch(e) {}
}else{
//chrome和FF、safrai等其他浏览器
return true;
}
}
</script>
</html>
如果是采用的是流返回,示例代码:
@RequestMapping("/pdfurl")
public void getPdf(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
response.setContentType("application/pdf");
FileInputStream in;
OutputStream out;
try {
//或者这里可以将base64转化为流,最终得到文件流就行了。
in = new FileInputStream(new File(这里写本地pdf的路径));
out = response.getOutputStream();
byte[] b = new byte[1024];
while ((in.read(b)) != -1) {
out.write(b);
}
out.flush();
in.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
正文到此结束(点击广告是对作者最大的支持)