在vue和element UI联合技术栈的操控下,本专栏提供行之有效的源代码示例和信息点介绍,做到灵活运用。
(1)提供vue2的一些基本操作:安装、引用,模板使用,computed,watch,生命周期(beforeCreate,created,beforeMount,mounted, beforeUpdate,updated, beforeDestroy,destroyed,activated,deactivated,errorCaptured,components,)、 $root , $parent , $children , $slots , $refs , props, $emit , eventbus ,provide / inject, Vue.observable, $listeners, $attrs, $nextTick , v-for, v-if, v-else,v-else-if,v-on,v-pre,v-cloak,v-once,v-model, v-html, v-text, keep-alive,slot-scope, filters, v-bind,.stop, .native, directives,mixin,render,国际化,Vue Router等
(2)提供element UI的经典操作:安装,引用,国际化,el-row,el-col,el-button,el-link,el-radio,el-checkbox ,el-input,el-select, el-cascader, el-input-number, el-switch,el-slider, el-time-picker, el-date-picker, el-upload, el-rate, el-color-picker, el-transfer, el-form, el-table, el-tree, el-pagination,el-badge,el-avatar,el-skeleton, el-empty, el-descriptions, el-result, el-statistic, el-alert, v-loading, $message, $alert, $prompt, $confirm , $notify, el-breadcrumb, el-page-header,el-tabs ,el-dropdown,el-steps,el-dialog, el-tooltip, el-popover, el-popconfirm, el-card, el-carousel, el-collapse, el-timeline, el-divider, el-calendar, el-image, el-backtop,v-infinite-scroll, el-drawer等
在vue项目开发中,我们有的时候是要将页面中的内容导出为pdf格式的文件。在本示例中,我们就是如何演示将html转换为pdf文件的。
/*
* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email: 2909222303@qq.com
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2022-09-20
*/
<template>
<div class="container">
<div class="top">
<h3>vue导出页面生成pdf文件</h3>
<div class="author">大剑师兰特, 还是大剑师兰特,gis-dajianshi</div>
</div>
<h4><el-button type="primary" @click="getPdf(htmlTitle)"> 导出为pdf文件</el-button> </h4>
<div id="zjcopyPDF">
<p> vue-router传递参数分为两大类</p>
<div><strong>编程式的导航 router.push</strong></div>
<div><strong>声明式的导航 <router-link></strong></div>
<div> </div>
<h2>编程式导航</h2>
<div>1)编程式导航传递参数有两种类型:字符串、对象。</div>
<div><u><strong>字符串</strong></u></div>
<div>字符串的方式是直接将路由地址以字符串的方式来跳转,这种方式很简单但是不能传递参数:</div>
<div>this.$router.push("home");</div>
<div><u><strong>对象</strong></u></div>
<div>想要传递参数主要就是以对象的方式来写,分为两种方式:<strong>命名路由、查询参数,</strong>下面分别说明两种方式的用法和注意事项。</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
htmlTitle: 'PDF文件名'
}
},
methods: {
},
}
</script>
<style scoped>
.container {
width: 1000px;
height: 580px;
margin: 50px auto;
border: 1px solid green;
position: relative;
}
.top {
margin: 0 auto 30px;
padding: 10px 0;
background: peru;
color: #fff;
}
#zjcopyPDF{text-align: left; padding: 10px 50px;}
</style>
npm install --save html2canvas
npm install jspdf --save
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
export default{
install (Vue, options) {
Vue.prototype.getPdf = function () {
var title = this.htmlTitle
html2Canvas(document.querySelector('#zjcopyPDF'), {
allowTaint: true
}).then(function (canvas) {
let contentWidth = canvas.width
let contentHeight = canvas.height
let pageHeight = contentWidth / 592.28 * 841.89
let leftHeight = contentHeight
let position = 0
let imgWidth = 595.28
let imgHeight = 592.28 / contentWidth * contentHeight
let pageData = canvas.toDataURL('image/jpeg', 1.0)
let 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()
}
}
}
PDF.save(title + '.pdf')
}
)
}
}
}
import htmlToPdf from ‘@/components/utils/htmlToPdf’
Vue.use(htmlToPdf)