vue實現(xiàn)html轉(zhuǎn)化pdf并復(fù)制文字
一、html2canvas 和 jsPDF(圖片插入pdf不可復(fù)制)
創(chuàng)建pdf.js文件
代碼如下
import html2canvas from 'html2canvas'
import jsPDF from 'jspdf'
import Vue from 'vue'
export const downloadPDF = (page, ne, isDownload) => {
html2canvas(page, {
allowTaint: true,
taintTest: false,
useCORS: true,
// width:960,
// height:5072,
dpi: window.devicePixelRatio * 8, // 將分辨率提高到特定的DPI 提高四倍
scale: 10 // 按比例增加分辨率
}).then(function(canvas) {
canvas2PDF(canvas, ne, isDownload)
})
}
const canvas2PDF = (canvas, ne, isDownload) => {
var contentWidth = canvas.width
var contentHeight = canvas.height
// 一頁pdf顯示html頁面生成的canvas高度;
var pageHeight = (contentWidth / 592.28) * 841.89
// 未生成pdf的html頁面高度
var leftHeight = contentHeight
// 頁面偏移
var position = 0
// a4紙的尺寸[595.28,841.89],html頁面生成的canvas在pdf中圖片的寬高
var imgWidth = 595.28
var imgHeight = (592.28 / contentWidth) * contentHeight
var pageData = canvas.toDataURL('image/jpeg', 1.0)
var PDF = new jsPDF('', 'pt', 'a4')
if (leftHeight < pageHeight) {
PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
} else {
while (leftHeight > 0) {
PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
leftHeight -= pageHeight
position -= 841.89
if (leftHeight > 0) {
// PDF.addPage();
}
}
}
console.log('PDF', PDF)
if (isDownload) {
PDF.save(ne + '.pdf') // 這里是導(dǎo)出的文件名
} else {
var pdfData = PDF.output('datauristring')
Vue.prototype.$baseURL = pdfData
// sessionStorage.setItem('base64URL',pdfData);
console.log(1)
}
}使用
<div class="pdf">
<div ref="pdf" class="page">你好</div>
</div>
//引入
import { downloadPDF, canvas2PDF } from '../../utils/pdf' // 工具方法,導(dǎo)出操作
handleExport() {
var zhis = this
var dsq = setTimeout(() => {
downloadPDF(this.$refs.pdf, `CUKH Tax Invoice - ${this.form.invoiceNo}`, true)
}, 100)
}二、window.print();方法打?。蓮?fù)制)
安裝
npm install vue-print-nb --save
手動下載插件到本地
插件地址:https://github.com/xyl66/vuePlugs_printjs
在src下新建文件夾plugs,將下載好的print.js放入plugs文件夾下
printjs代碼
//print.js 里的代碼
// 打印類屬性、方法定義
/* eslint-disable */
const Print =function(dom, options) {
if (!(this instanceof Print)) return new Print(dom, options);
this.options = this.extend({
'noPrint': '.no-print'
}, options);
if ((typeof dom) === "string") {
this.dom = document.querySelector(dom);
} else {
this.dom = dom;
}
this.init();
};
Print.prototype = {
init: function () {
var content = this.getStyle() + this.getHtml();
this.writeIframe(content);
},
extend: function (obj, obj2) {
for (var k in obj2) {
obj[k] = obj2[k];
}
return obj;
},
getStyle: function () {
var str = "",
styles = document.querySelectorAll('style,link');
for (var i = 0; i < styles.length; i++) {
str += styles[i].outerHTML;
}
str += "<style>" + (this.options.noPrint ? this.options.noPrint : '.no-print') + "{display:none;}</style>";
return str;
},
getHtml: function () {
var inputs = document.querySelectorAll('input');
var textareas = document.querySelectorAll('textarea');
var selects = document.querySelectorAll('select');
for (var k in inputs) {
if (inputs[k].type == "checkbox" || inputs[k].type == "radio") {
if (inputs[k].checked == true) {
inputs[k].setAttribute('checked', "checked")
} else {
inputs[k].removeAttribute('checked')
}
} else if (inputs[k].type == "text") {
inputs[k].setAttribute('value', inputs[k].value)
}
}
for (var k2 in textareas) {
if (textareas[k2].type == 'textarea') {
textareas[k2].innerHTML = textareas[k2].value
}
}
for (var k3 in selects) {
if (selects[k3].type == 'select-one') {
var child = selects[k3].children;
for (var i in child) {
if (child[i].tagName == 'OPTION') {
if (child[i].selected == true) {
child[i].setAttribute('selected', "selected")
} else {
child[i].removeAttribute('selected')
}
}
}
}
}
return this.dom.outerHTML;
},
writeIframe: function (content) {
var w, doc, iframe = document.createElement('iframe'),
f = document.body.appendChild(iframe);
iframe.id = "myIframe";
iframe.style = "position:absolute;width:0;height:0;top:-10px;left:-10px;";
w = f.contentWindow || f.contentDocument;
doc = f.contentDocument || f.contentWindow.document;
doc.open();
doc.write(content);
doc.close();
this.toPrint(w);
setTimeout(function () {
document.body.removeChild(iframe)
}, 100)
},
toPrint: function (frameWindow) {
try {
setTimeout(function () {
frameWindow.focus();
try {
if (!frameWindow.document.execCommand('print', false, null)) {
frameWindow.print();
}
} catch (e) {
frameWindow.print();
}
frameWindow.close();
}, 10);
} catch (err) {
console.log('err', err);
}
}
};
const MyPlugin = {}
MyPlugin.install = function (Vue, options) {
Vue.prototype.$print = Print
}
export default MyPluginmain.js里引入
import Print from './plugs/print' Vue.use(Print)
使用
<div class="pdf">
<div ref="pdf" class="page">你好</div>
</div>
handleExport() {
setTimeout(() => {
this.$print(this.$refs.pdf)
}, 100);
},
修改打印樣式
樣式可能會有偏差,或者位移可以添加樣式去修改
@media print {
@page {
margin: 0 !important;
}
.pdf{
position: fixed;
top: -265px !important;
left: -150px !important;
transform: translate(-50%, -50%);
transform: scale(0.7);
box-shadow:none !important;
}
}
可能遇到的問題及解決方案
①圖片占位置 ---------讓它脫離文檔流( position: absolute; 不要用fixed 這樣內(nèi)容多的時候只打印第一頁)
②頁面不想展示打印內(nèi)容 只打??;------- 給它 z-index:-1 (display:none 的話打印內(nèi)容也沒有)
③指定不打印區(qū)域
方法1. 添加no-print樣式類
方法2. 自定義類名
this.print( this.print(this.print(this.refs.print,{‘no-print':‘.do-not-print-me-xxx'}) 到此這篇關(guān)于vue實現(xiàn)html轉(zhuǎn)化pdf并復(fù)制文字的文章就介紹到這了,更多相關(guān)vue html轉(zhuǎn)pdf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue環(huán)境搭建+VSCode+Win10的詳細(xì)教程
這篇文章主要介紹了Vue環(huán)境搭建+VSCode+Win10,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Vue axios 將傳遞的json數(shù)據(jù)轉(zhuǎn)為form data的例子
今天小編就為大家分享一篇Vue axios 將傳遞的json數(shù)據(jù)轉(zhuǎn)為form data的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

