VUE获取PDF文件流在页面中直接掉起打印弹框

6 篇文章 0 订阅
1 篇文章 0 订阅

1.首先获取到当前PDF文件的链接

2.在放JS的文件包里面创建一个common.js文件,写入要实现功能的代码。

// 调用打印弹框
function dialogPoint(urlAdd){
    fetch(urlAdd).then(res => res.blob()).then(blob => {
        var date = (new Date()).getTime()
        var ifr = document.createElement('iframe')
        ifr.style.frameborder = 'no'
        ifr.style.display = 'none'
        ifr.style.pageBreakBefore = 'always'
        ifr.setAttribute('id', 'printPdf' + date)
        ifr.setAttribute('name', 'printPdf' + date)
        ifr.src = window.URL.createObjectURL(blob);
        document.body.appendChild(ifr)
        doPrint('printPdf' + date)
    }); 
}

function doPrint(val) {
    var ordonnance = document.getElementById(val).contentWindow
    setTimeout(() => {
        ordonnance.print()
    }, 100)
}

export {
    dialogPoint,
}

3.在需要实现打印弹框功能的页面导入使用就可以实现以下效果了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Vue的axios库来跨域获取图片并打印。下面是一个简单的示例代码: ```javascript <template> <div> <button @click="getImage">获取图片并打印</button> </div> </template> <script> import axios from 'axios'; export default { methods: { getImage() { const imageUrl = 'http://example.com/image.jpg'; // 替换为你想要获取的图片URL axios.get(imageUrl, { responseType: 'blob', // 指定响应类型为二进制文件 headers: { 'Content-Type': 'image/jpeg' // 根据实际情况设置请求头 } }) .then(response => { const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', 'image.jpg'); document.body.appendChild(link); link.click(); }) .catch(error => { console.error('获取图片失败:', error); }); } } } </script> ``` 在上面的示例代码,我们使用了axios库来发送GET请求获取图片数据。设置`responseType`为`blob`,表示响应类型为二进制文件。然后,使用`window.URL.createObjectURL`来创建一个临时的URL,然后创建一个`<a>`标签,设置其`href`属性为临时URL,同时设置`download`属性为想要保存的文件名,最后将该`<a>`标签添加到文档,并模拟点击该标签来触发下载操作。 请注意,由于浏览器的安全策略,跨域请求可能会受到限制。你需要确保在服务器端设置了正确的CORS(跨域资源共享)配置,允许来自你的Vue应用的域名的请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值