微信小程序使用webview頁面轉(zhuǎn)pdf文件代碼示例
前言:正式上線,在小程序后臺(tái)配置業(yè)務(wù)域名



一、uniapp 使用 webview
<template>
<web-view :src="url" @message="message"></web-view>
</template>
<script>
export default {
data() {
return {
url: ''
}
},
onLoad(option) {
this.url = `http://www.xxx.com?orderId=${option.orderId}`;
},
methods: {
message(e) {
this.imageData = e.detail.data[0].imageData
let path = this.imageData.split('base64,')[1]
this.download(path)
},
async download(url) {
let result = url.replace(/[\r\n]/g, '');
var fs = wx.getFileSystemManager();
let fileName = '';
var times = new Date().getTime();
url = wx.base64ToArrayBuffer(result);
// console.log(url,'圖片臨時(shí)路徑')
const filePath = wx.env.USER_DATA_PATH + '/' + Date.now() + '.pdf'
fs.writeFile({
filePath,
data: url, // 將 base64 轉(zhuǎn)為 arrayuffer
success(res) {
uni.openDocument({
showMenu: true,
fileType: 'pdf',
filePath,
success: function(res) {
console.log('打開文檔成功')
}
})
},
fail(err) {
console.log('錯(cuò)誤', err)
}
})
}
}
}
</script>
二、H5頁面
(1) 安裝兩個(gè)插件
npm i jspdf html2canvas
<template>
<view id="content">這是轉(zhuǎn)pdf的內(nèi)容</view>
</template>
<script>
import html2Canvas from 'html2canvas';
import { jsPDF } from 'jspdf';
export default {
data() {
return {
orderId: ''
}
},
onLoad(option) {
this.orderId = this.getUrlParameter('orderId');
uni.showLoading({
title: '正在加載'
})
setTimeout(() => {
uni.hideLoading();
this.getPdf()
}, 2000)
},
methods:{
//接收uniapp傳來的鏈接參數(shù)
getUrlParameter(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
},
getPdf() {
let that = this
var shareContent = document.getElementById('content');
var width = shareContent.offsetWidth / 1;
var height = shareContent.offsetHeight / 1;
html2Canvas(shareContent, {
dpi: 900,
scrolly: 0,
// width:eleW,//生成后的寬度
// height:eleH,//生成后的高度
scrollx: -10,
useCORS: true, //允許canvas畫布內(nèi)可以跨域請(qǐng)求外部鏈接圖片, 允許跨域請(qǐng)求。
// backgroundColor: null //避免圖片有白色邊框
}).then((canvas) => {
var context = canvas.getContext('2d');
context.mozImageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
var pageData = canvas.toDataURL('image/jpeg', 1.0);
var img = new Image();
img.src = pageData;
img.onload = () => {
// 獲取dom高度、寬度
img.width = img.width / 2;
img.height = img.height / 2;
img.style.transform = 'scale(0.5)';
if (width > height) {
// 此可以根據(jù)打印的大小進(jìn)行自動(dòng)調(diào)節(jié)
// eslint-disable-next-line
var pdf = new jsPDF('l', 'mm', [width * 0.545, height * 0.545]);
} else {
// eslint-disable-next-line
var pdf = new jsPDF('p', 'mm', [width * 0.545, height * 0.545]);
}
pdf.addImage(pageData, 'jpeg', 0, 0, width * 0.545, height * 0.545);
pdf.save('這是文件命名' + '.pdf'); //h5在這就可以保存pdf
//內(nèi)嵌到微信小程序
var blob = pdf.output("datauristring");
console.log(wx, 'wx')
wx.miniProgram.getEnv(function(res) {
console.log("當(dāng)前環(huán)境:" + JSON.stringify(res));
});
wx.miniProgram.postMessage({
data: {
imageData: blob
},
});
wx.miniProgram.navigateBack()
};
}).catch((r) => {
console.log(r);
})
}
}
}
</script>
(2) 在App.vue 添加以下代碼
onLaunch() {
console.log('App Launch')
// #ifdef H5
var script = document.createElement('script');
script.src = "https://res.wx.qq.com/open/js/jweixin-1.4.0.js";
script.type = 'text/javascript';
document.body.appendChild(script);
// #endif
},總結(jié)
到此這篇關(guān)于微信小程序使用webview頁面轉(zhuǎn)pdf文件的文章就介紹到這了,更多相關(guān)微信小程序webview頁面轉(zhuǎn)pdf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php gethostbyname獲取域名ip地址函數(shù)詳解
php gethostbyname獲取域名ip地址函數(shù),需要根據(jù)域名得到ip地址的朋友有福了。2010-01-01
xmlplus組件設(shè)計(jì)系列之選項(xiàng)卡(Tabbar)(5)
xmlplus 是一個(gè)JavaScript框架,用于快速開發(fā)前后端項(xiàng)目。這篇文章主要介紹了xmlplus組件設(shè)計(jì)系列之選項(xiàng)卡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
jquery 實(shí)現(xiàn)輸入郵箱時(shí)自動(dòng)補(bǔ)全下拉提示功能
大家在做Web項(xiàng)目,都會(huì)有注冊(cè)登錄模塊,如果是郵箱注冊(cè),想要在輸入@后觸發(fā)下拉框顯示各個(gè)郵箱的功能。下面介紹一款jQuery實(shí)現(xiàn)輸入郵箱的時(shí)候,可自動(dòng)補(bǔ)全郵箱地址,也可稱為是“輸入提示”的功能,比如輸入aaa時(shí),自動(dòng)變成aaa@163.com,有效提升網(wǎng)頁的人性化體驗(yàn)2015-10-10
在js中做數(shù)字字符串補(bǔ)0(js補(bǔ)零)
這篇文章主要介紹了在js中做數(shù)字字符串補(bǔ)0(js補(bǔ)零),需要的朋友可以參考下2017-03-03
Layui實(shí)現(xiàn)數(shù)據(jù)表格中鼠標(biāo)懸浮圖片放大效果,離開時(shí)恢復(fù)原圖的方法
今天小編就為大家分享一篇Layui實(shí)現(xiàn)數(shù)據(jù)表格中鼠標(biāo)懸浮圖片放大效果,離開時(shí)恢復(fù)原圖的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09

