跳过浏览器的PDF预览 直接打印
总结一下如何跳过浏览器的PDF预览 直接打印
由于安全性的问题 旧版本的Adobe PDF Reader 已经不在工作了 但是有些时候我们还是需要跳过繁琐的打印预览,来直接直接打印我们所需要的PDF
首先
需要准备一个应用 : WebApp Hardware Bridge ; 下载地址是: https://github.com/imTigger/webapp-hardware-bridge/releases 具体下载那一个呢 就看自己的需求了 我下载的是 whb-0.13.0.exe 版本, 下载好之后一波无脑安装就好.安装好之后,进入下一个环节.
其次
来到这个地址https://github.com/imTigger/webapp-hardware-bridge你可以通过git下载 也可以下载压缩包解压, 之后你会看到这样的目录
, 然后 点击 demo文件夹,选择websocket-pruinter.js文件,将这个文件放在你项目的js
文件夹中,并在你需要跳过打印预览的页面引入这个js
文件,然后使用你的编辑器打开printer-advanced.htm
这个文件,你会在下面看到一系列的script就是这些:
<script>
var printService = new WebSocketPrinter({
onConnect: function () {
$('#status').val('Connected');
},
onDisconnect: function () {
$('#status').val('Disconnected');
},
onUpdate: function (message) {
$('#status').val(message);
},
});
function printPDF() {
// console.log(111);
printService.submit({
'type': 'INVOICE',
'url': $('#url').val()
});
}
function printPDFWithId() {
printService.submit({
'type': 'INVOICE',
'id': new Date().getTime(),
'url': $('#url').val()
});
}
function printPDFWithQty() {
printService.submit({
'type': 'INVOICE',
'qty': $('#qty').val(),
'url': $('#url').val()
});
}
function printPDFWithFallback() {
if (printService.isConnected()) {
printPDF();
} else {
alert('WebApp Hardware Bridge not running');
window.open($('#url').val());
}
}
</script>
我需要的只有var printService = new WebSocketPrinter()
和printPDF()
以及printPDFWithFallback()
为了方便 我直接 整合了后面的两个方法
关于具体的代码我是这样子写的
我的这些代码是在.php
文件内写的,如果需要写在.js
文件内,请自行修改
//CONFIG_PRINTSERVICE_ADRESSE是当前打印机的地址 default = 127.0.0.1:12212
CONFIG_PRINTSERVICE_ADRESSE = <?php echo urldecode($_GET['configprintservice']);?>
printService = null;
if (typeof jQuery !== 'undefind'){
if (parseInt(str_replace('.','',jQuery.fn.jquery)) >= 331) {
printService = new WebStocketPrinter({
url = "ws://" + CONFIG_PRINTSERVICE_ADRESSE +"/printer",
onConnect: function () {
// 这里的信息 你可以根据自己或者客户的需要来打印自定义的消息
console.log('WebApp Hardware Birdge connected!!');
},
onDisconnect: function () {
// 这里的信息 你可以根据自己或者客户的需要来打印自定义的消息
console.log('WebApp Hardware Birdge disconnected! ')
},
onUpdate: funciton (message) {
console.log(message)
}
})
}
}
function WebAppAutoPrint(url,redirsectto){
if (printService && printService.isConnected()) {
printService.submit({
// 这里的type内容需要和你下载的应用里的print type(文章末尾有图片) 相同就可以 注意: 区分大小写
'type': 'INVOICE',
'url': window.location.protocol+'//'+window.location.hostname+':'+window.location.port+WebAppRewriteUrl(url)
});
alert('print ok! ('+WebAppRewriteUrl(url).replace('/pdf/','')+')');
if (redirsectto) {
window.location.href=redirsectto;
}
} else {
TINY86.box.show('<iframe src='+ url +' width='+ CONFIG_TINY_WIDTH +' height='+ CONFIG_TINY_HEIGHT +' style=border:0></iframe>',0,0,0,1);
if (redirsectto) {
if (document.getElementById('close_tinybox2')) document.getElementById('close_tinybox2').setAttribute('onclick',"window.location.href='"+redirsectto+"'")
}
}
}
funciton WebAppRewriteUrl(url){
var new_url = '';
new_url = url.replace('/print/','/pdf/') ;
new_url = new_url.replace('.php?','-');
new_url = new_url.replace('id=(\d+)&qte=(\d+)','-$1-$2');
new_url = new_url.replace('id=','');
new_url = new_url.replace('&qte=','-');
new_url += '.pdf';
return new_url;
}
上面的JSRewriteUrl
使我自己写的js重写url的方法,网上找了找看了看 , 借鉴了一下
注意
由于我的url是生成PDF的php文件所以 我需要在伪静态和js中重写 这个文件的路由^ 重写的路由
然后
你可以在你的电脑的工具栏找到下载的软件的图标 ,然后右键单击 ,选择configurator
会出来一个界面,你只需要在右下角的框中选中正确的打印机就好
其中的
General
如果你设置了类似于我的这个样子那就说明 : 是的,这意味着安装在 Web 服务器上的应用程序将监听来自本地网络的任何地址
# id, qte是我的项目中用到的参数,你可以根据自己的项目修改参数 <IfModule mod_rewrite.c> RewriteRule ^pdf/PDF_NAME-(\d+)-(\d+).pdf$ print/PDF_SCRIPT.php?id=$1&qte=$2 </IfModule>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗