• vue纯前端页面pdf导出下载


    下载插件

    npm install html2canvas --save
    npm install jspdf --save
    
    • 1
    • 2

    创建导出函数文件htmlToPdf.js

    // 导出页面为PDF格式
    import html2Canvas from 'html2canvas'
    import JsPDF from 'jspdf'
    export default {
      install(Vue, options) {
        Vue.prototype.getPdf = function(fileName, divId) {
          var element = document.getElementById(divId)
          var title = fileName // PDF文件标题
          var c = document.createElement('canvas')// 创建照片
          var opts = {
            scale: 1,
            canvas: c,
            logging: true,
            pageX: 0,
            width: element.clientWidth,
            height: element.clientHeight
          }
          // 照片高度和宽度是页面元素的两倍
          c.width = element.clientWidth * 1
          c.height = element.clientHeight * 1
          c.getContext('2d').scale(1, 1)
          html2Canvas(element, opts)
            .then(function(canvas) {
              const contentWidth = canvas.width
              const contentHeight = canvas.height
              const pageHeight = contentWidth / 592.28 * 841.89
              let leftHeight = contentHeight
              let position = 0
              const imgWidth = 595.28
              const imgHeight = 592.28 / contentWidth * contentHeight
              const pageData = canvas.toDataURL('image/jpeg', 1.0)
              const PDF = new JsPDF('', 'pt', 'a4')
              if (leftHeight < pageHeight) {
                // 第一个20是img和pdf页面的左边距,第二个20是img与pdf页面的上边距
                PDF.addImage(pageData, 'JPEG', 20, 20, imgWidth, imgHeight)
              } else {
                while (leftHeight > 0) {
                  PDF.addImage(pageData, 'JPEG', 20, position, imgWidth, imgHeight)
                  leftHeight -= pageHeight
                  position -= 841.89
                  if (leftHeight > 0) {
                    PDF.addPage()
                  }
                }
              }
              PDF.save(title + '.pdf')
            })
        }
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    在 main.js 文件里引入并全局注册

    import htmlToPdf from '@/utils/htmlToPdf'
    Vue.use(htmlToPdf)
    
    • 1
    • 2

    在页面上使用

    <template>
      <div class="div-container">
        <div>
          <el-button @click="pdf()">导出为PDF</el-button>
        </div>
        <!--    打印画布-->
        <div id="content" style="width: 600px">
          <div style="margin-top: 20px;height: 824px;width: 550px; border: black solid 1px">
            <!--        封面-->
            <div>
              <img src="/img/hnBackGroudTop.png" >
            </div>
            <div style="text-align: center">
              <h1 style="color: #6a3b50">个人健康风险评估报告</h1>
            </div>
            <div>
              <el-form ref="formData" :model="form" label-width="auto" class="form-container">
                <div>
                  <div style="display: flex;width: 100%" class="form-container">
                    <p style="font-size: 20px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</p><p class="underline">&nbsp;&nbsp;&nbsp;&nbsp;{{ form.name }}&nbsp;&nbsp;&nbsp;&nbsp;</p>
                  </div>
                  <div style="display: flex;width: 100%" class="form-container">
                    <p style="font-size: 20px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</p><p class="underline">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ form.sex }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
                  </div>
                  <div style="display: flex;width: 100%" class="form-container">
                    <p style="font-size: 20px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</p><p class="underline">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ form.age }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
                  </div>
                  <div style="display: flex;width: 100%" class="form-container">
                    <p style="font-size: 20px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</p><p class="underline">&nbsp;&nbsp;{{ form.lorgName }}&nbsp;&nbsp;</p>
                  </div>
                  <div style="display: flex;width: 100%" class="form-container">
                    <p style="font-size: 20px">评估日期:</p><p class="underline">&nbsp;&nbsp;{{ form.checkDate }}&nbsp;&nbsp;</p>
                  </div>
                </div>
              </el-form>
            </div>
            <div style="margin-top: 95px">
              <img src="/img/hnBackGroudFoot.png" style="width: 100%">
            </div>
          </div>
          <div style="height: 824px;width: 550px ;margin-top: 20px; border: black solid 1px">
            <!--        目录-->
            <div style="display:flex;margin-top: 20px">
              <div style="width: 20%">
                <div class="line" />
                <div class="line2" />
              </div>
              <div><i class="el-icon-tickets" style="color: #78aa59;font-size: 28px" /><span style="font-size: 28px;color: #78aa59"> 目 录 </span></div>
              <div style="width: 61%">
                <div class="line1" />
                <div class="line3" />
              </div>
            </div>
            <div style="display: flex;margin-top: 10px" v-for="(item,index) in form.directory" :key="index">
              <div style="font-size: 20px;width: 90%">&nbsp;&nbsp;&nbsp;{{item.directoryName}}</div>
              <div class="line-container"></div>
              <div style="font-size: 20px;width: 10%">{{index}}</div>
            </div>
          </div>
          <div style="height: 824px;width: 550px ;margin-top: 20px; border: black solid 1px">
            <!--        个人信息汇总-->
            <div style="display:flex;margin-top: 20px">
              <div style="width: 20%">
                <div class="grLine1" />
                <div class="grLine2" />
              </div>
              <div><span style="font-size: 28px;color: #78aa59"> 个人信息汇总 </span></div>
              <div style="width: 49%">
                <div class="grLine3" />
                <div class="grLine4" />
              </div>
            </div>
            <div style="margin-top: 50px;display: flex;justify-content: center;align-items: center;">
              <table style="border: 1px solid black;border-collapse:collapse;" width="80%">
                <tr style="height: 34px">
                   <td class="tableTdOne">
                     姓名
                   </td>
                   <td  class="tableTd">
                     马冬梅
                   </td>
                   <td  class="tableTdOne">
                     性别
                   </td>
                   <td  class="tableTd"></td>
                   <td  class="tableTdOne">
                     年龄
                   </td>
                   <td  class="tableTd">
                     33
                   </td>
                 </tr>
                <tr style="height: 34px">
                  <td class="tableTdOne">
                    身高
                  </td>
                  <td  class="tableTd">
                    163
                  </td>
                  <td  class="tableTdOne">
                    体重
                  </td>
                  <td  class="tableTd">
                    62
                  </td>
                  <td  class="tableTdOne">
                    BIM
                  </td>
                  <td  class="tableTd">
                    23.3
                  </td>
                </tr>
                <tr style="height: 34px">
                  <td class="tableTdOne">
                    婚否
                  </td>
                  <td  class="tableTd">
                    已婚
                  </td>
                  <td  class="tableTdOne">
                    手机号
                  </td>
                  <td  class="tableTd">
                    13589659579
                  </td>
                  <td  class="tableTdOne">
    
                  </td>
                  <td  class="tableTd">
    
                  </td>
                </tr>
              </table>
            </div>
          </div>
        </div>
      </div>
    </template>
    
    <script>
    // import html2canvas from 'html2canvas'
    // import jsPDF from 'jspdf'
    import { getTime } from '@/utils'
    
    export default {
      name: 'PdfPreview',
      components: {},
      data () {
        return {
          form: {
            name: '张三',
            sex: '女',
            age: '20',
            lorgName: '运营部门',
            checkDate: '2021-09-01',
            directory: [
              { directoryName: '个人健康信息汇总', numberOfPages: '01' },
              { directoryName: '主要生理指标', numberOfPages: '03' },
              { directoryName: '行为与生活方式评价', numberOfPages: '06' },
              { directoryName: '肥胖症风险评估报告', numberOfPages: '08' },
              { directoryName: '高血压风险评估报告', numberOfPages: '10' },
              { directoryName: '糖尿病风险评估报告', numberOfPages: '12' },
              { directoryName: '代谢综合征风险评估报告', numberOfPages: '14' },
              { directoryName: '缺血性心血管病风险评估报告', numberOfPages: '16' },
              { directoryName: '脑卒中风险评估报告', numberOfPages: '18' },
              { directoryName: '血脂异常危险评估报告', numberOfPages: '20' },
              { directoryName: '骨质疏松性骨折风险评估报告', numberOfPages: '22' },
              { directoryName: '抑郁症风险评估报告', numberOfPages: '24' },
              { directoryName: '睡眠呼吸暂停综合征风险评估报告', numberOfPages: '25' },
              { directoryName: '运动风险评估报告', numberOfPages: '27' },
              { directoryName: '肺癌风险评估报告', numberOfPages: '29' },
              { directoryName: '乳腺癌风险评估报告', numberOfPages: '31' },
              { directoryName: '推荐膳食方窝', numberOfPages: '33' },
              { directoryName: '推荐运动方案', numberOfPages: '35' }
            ]
          }
        }
      },
      computed: {},
      watch: {},
      mounted() {},
      methods: {
        pdf() {
          var fileName = this.form.name + '个人健康风险评估报告'
          this.getPdf(fileName, 'content')
        }
    
      }
    }
    </script>
    
    <style lang="scss">
      .underline {
        font-size: 20px;
        text-decoration: underline;
      }
      .form-container {
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .div-container{
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
      }
      .centered-div {
        /* 样式设置 */
      }
      .line {
        margin-top: 5px;
        width: 100%;
        height: 6px;
        background: #78aa59;
        position: relative;
        text-align: center;
      }
      .grLine1 {
        margin-top: 5px;
        width: 100%;
        height: 9px;
        background: #78aa59;
        position: relative;
        text-align: center;
      }
      .line2 {
        margin-top: 5px;
        width: 100%;
        height: 3px;
        background: #78aa59;
        position: relative;
        text-align: center;
      }
      .grLine2 {
        margin-top: 5px;
        width: 100%;
        height: 2px;
        background: #78aa59;
        position: relative;
        text-align: center;
      }
      .line1 {
        margin-top: 5px;
        width: 100%;
        height: 6px;
        background: #78aa59;
        position: relative;
        text-align: center;
      }
      .grLine3 {
        margin-top: 5px;
        width: 100%;
        height: 9px;
        background: #78aa59;
        position: relative;
        text-align: center;
      }
      .line3 {
        margin-top: 5px;
        width: 100%;
        height: 3px;
        background: #78aa59;
        position: relative;
        text-align: center;
      }
      .grLine4 {
        margin-top: 5px;
        width: 100%;
        height: 2px;
        background: #78aa59;
        position: relative;
        text-align: center;
      }
      .line-container {
        border: 1px dashed #000; /* 设置边框为1像素宽、虚线样式和颜色 */
        margin-top: 16px;
        height: 0;
        width: 55%;
      }
      .tableTdOne {
        border: 1px solid black;
        background-color: #edf5e3;
      }
      .tableTd {
        border: 1px solid black;
      }
    
    </style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
  • 相关阅读:
    bash例子-source进程替换、alias不生效处理
    【计算机网络】超详细——VLAN、Trunk的配置
    微信小程序开发---事件的绑定
    anaconda下载与spyder的报错解决
    Next.js 热更新 Markdown 文件变更
    Linux 内核参数:min_free_kbytes
    10.27 知识总结(前端)
    Serialiable接口和serialVersionUID的作用及其在redisson中的应用
    (CVE-2022-21661)WordPress SQL 注入漏洞分析和复现
    P21升级后,用旧的spec模板生成的defime.xml中WhereClauses(VLM)里的逗号不能正常显示
  • 原文地址:https://blog.csdn.net/weixin_49498516/article/details/133200650