uni-app打印
<p>1.手机端调用window.print()方法无效,无论是在浏览器或者app中或者webview中,(可能有的手机浏览器可以)。</p><p>2.uniapp app端无法直接获取dom元素,,没有document对象,可使用renderjs处理,在app中只能使用原生的html元素如button(在元素上添加点击事件)调用renderjs中的方法,在例如u-button中就调用无效</p>
<pre>在renderjs里需要调用 主js的saveImg方法,传值,因为在renderjs模块里无法获取uni对象,因此需要调用主js</pre>
<p>3.jspdf.js插件在手机端无法正常使用,在浏览器中时页面打不开,在app中页面能正常打开但下载pdf时一直下载失败。</p>
<p>最终解决方案:</p>
<p><span style="font-size: 14pt; color: rgba(255, 0, 0, 1)">uniapp的renderjs+html2canvas+后台转pdf+android原生打印插件(这个插件是在HBuilder的插件市场安装的,地址:https://ext.dcloud.net.cn/plugin?id=1633)</span></p>
<p>使用uniapp的renderjs,在renderjs里面调用html2canvas的页面转图片功能生成base64的图片,然后调用后台接口将base64图片转成pdf(本来这里是使用jspdf插件进行图片转pdf,但是在手机端这里一直无法正常转换,详情见3.),</p>
<p>再调用uni的文件下载接口下载pdf文件,然后查看文件下载后在手机本地的存储路径,调用安卓原生打印插件(该插件只能打印pdf文件,且文件路径必须是本地绝对路径),连接打印机进行打印(打印机通过wlan链接,因此打印时设备应在同一网络下)</p>
<p> </p>
<div class="cnblogs_code">
<pre><button class="printBtn" type='primary' @click="renderTest.changeToImg1">打印</button>
<script><span style="color: rgba(0, 0, 0, 1)">
import {
loadPdf
} from </span>'../../api/common.js'<span style="color: rgba(0, 0, 0, 1)">
import {
baseUrl
} from </span>'../../api/api.js'<span style="color: rgba(0, 0, 0, 1)">
const printPdf </span>= uni.requireNativePlugin('YanYu-PrintPDF'<span style="color: rgba(0, 0, 0, 1)">);
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
data() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {}
},<br> methods:{
saveImg(src) {
loadPdf({
image: src
}).then(res </span>=><span style="color: rgba(0, 0, 0, 1)"> {
console.log(res.result) </span><span style="color: rgba(0, 0, 0, 1)">
uni.downloadFile({
url: res.result,
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">url:"http://61.162.225.227:19000/wechat/static/direct.png",</span>
success: <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(res) {
console.log(</span>'save success-------'<span style="color: rgba(0, 0, 0, 1)">);
console.log(res)
let base </span>=
"/storage/emulated/0/Android/data/store.yard.law/apps/__UNI__A6E30DD/"
<span style="color: rgba(0, 0, 255, 1)">var</span> savedFilePath = res.tempFilePath.substr(1<span style="color: rgba(0, 0, 0, 1)">);
printPdf.managerPrint(base </span>+<span style="color: rgba(0, 0, 0, 1)"> savedFilePath);
},
fail(err) {
console.log(</span>'save err-------------'<span style="color: rgba(0, 0, 0, 1)">);
console.log(err);
}
});
})
}
}
}<br></script><br></span></pre>
</div>
<p> </p>
<div class="cnblogs_code">
<pre><script module="renderTest" lang="renderjs"><span style="color: rgba(0, 0, 0, 1)">
import html2canvas from </span>'html2canvas'<span style="color: rgba(0, 0, 0, 1)">;</span><span style="color: rgba(0, 0, 0, 1)">
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
data() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {}
},
methods: {
changeToImg1(event, ownerInstance) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> dom = document.querySelector("#dy"); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取dom元素 </span>
<span style="color: rgba(0, 0, 0, 1)"> html2canvas(dom, {
useCORS: </span><span style="color: rgba(0, 0, 255, 1)">true</span>, <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">支持图片跨域</span>
width: "950"<span style="color: rgba(0, 0, 0, 1)">,
height: </span>"1344"<span style="color: rgba(0, 0, 0, 1)">
}).then(</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(canvas) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> image = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Image();
let src </span>= image.src = canvas.toDataURL("image/png"<span style="color: rgba(0, 0, 0, 1)">);<br></span></pre>
<pre> // 调用 service层的saveImg方法,传值,因为在renderjs模块里无法获取uni对象,因此需要调用主js<br><br><span> ownerInstance.callMethod(</span>'saveImg'<span>, src) </span></pre>
<pre><em id="__mceDel"><span style="color: rgba(0, 0, 0, 1)"> }); <br> } <br> } <br> }<br> </span></script></em></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/BlingSun/p/15100148.html
頁:
[1]