最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue使用html2canvas和jspdf將html轉(zhuǎn)成pdf

 更新時(shí)間:2022年03月04日 09:50:08   作者:小偉0718  
在前端開發(fā)中, html轉(zhuǎn)pdf是最常見的需求,下面這篇文章主要給大家介紹了關(guān)于vue如何使用html2canvas和jspdf將html轉(zhuǎn)成pdf的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

A4尺寸

A4紙的尺寸是210mm×297mm。

分辨率是72像素/英寸時(shí),A4紙的尺寸的圖像的像素是595×842(推薦用這個(gè)大小比例)。

分辨率是150像素/英寸時(shí),A4紙的尺寸的圖像的像素是1240×1754。

分辨率是300像素/英寸時(shí),A4紙的尺寸的圖像的像素是2479×3508。

選擇不同的分辨率圖像像素大小也會(huì)隨之變化

安裝插件html2canvas和jspdf

npm install html2canvas--save
npm install jspdf --save

html2canvas可以通過獲取HTML的某個(gè)元素,然后生成Canvas,能讓用戶保存為圖片。

jsPDF 是一個(gè)基于 HTML5 的客戶端解決方案,用于生成各種用途的 PDF 文檔。

在項(xiàng)目中引入

在utils 中 新建htmltopdf.js

htmlToPdf.js

// 導(dǎo)出頁面為PDF格式
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
export default{
  install (Vue, options) {
    Vue.prototype.getPdf = function () {
      var pdfTitle = this.pdfTitle  //pdf的名稱
      var pdfDom = document.querySelector('#pdfDom')
      html2Canvas(pdfDom, {
        allowTaint: true
      }).then(function (canvas) {
        console.log(canvas)
        const marginBottom = 34    // 項(xiàng)目頁面顯示微處理 以下用到的地方 可以忽略
        let canvasWidth = canvas.width 	//頁面生成canvas寬度
        let canvasHeight = canvas.height + marginBottom //頁面生成canvas高度
        let pageHeight = canvasWidth / 592.28 * 841.89 + marginBottom   //分頁 每頁的高度
        let allPageHeight = canvasHeight  // 所有頁面的高度
        let position = 0 //偏移量
        let imgWidth = 595.28 //生成canvas 圖片的寬度
        let imgHeight = 592.28 / canvasWidth * canvasHeight //生成canvas 圖片的高度
        let pageData = canvas.toDataURL('image/jpeg', 3.0)
        // console.log(canvasWidth)
        // console.log(canvasHeight)
        // console.log(pageHeight)
        // console.log(allPageHeight)
        // console.log(position)
        // console.log(imgWidth)
        // console.log(imgHeight)
        // console.log(pageData)
        let PDF = new JsPDF('', 'pt', 'a4')
        if (allPageHeight < pageHeight) {
          PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
        } else {
          // 循環(huán)生成分頁
          while (allPageHeight > 0) {
            PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
            allPageHeight = allPageHeight - pageHeight - marginBottom
            position = position - 841.89 - marginBottom
            if (allPageHeight > 0) {
              PDF.addPage() //添加新的一頁
            }
          }
        }
        PDF.save(pdfTitle + '.pdf')  //保存pdf
      })
    }
  }
}

在main.ts 中 全局引入

main.ts

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import 'babel-polyfill'
import Vue from 'vue'
import App from './App.vue'
import router from './router/index.ts'
import store from './store/index.js'
import * as ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import htmlToPdf from '@/utils/htmlToPdf.js'
// 使用Vue.use()方法就會(huì)調(diào)用工具方法中的install方法
Vue.use(htmlToPdf)
// import 'swiper/dist/css/swiper.css'
// import * as VueAwesomeSwiper from 'vue-awesome-swiper'
// Vue.use(VueAwesomeSwiper)
Vue.config.productionTip = false
Vue.use(ElementUI)
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

vue 頁面

<template>
  <div class="transcript-container clearfix transcript-detail" @mouseenter.stop="pdfFlag = false">
    <div class="creat-pdf clearfix" @click.stop="pdfFlag = true;getPdf('#pdfDom')">下載pdf</div>
    <div id="pdfDom" class="clearfix" style="width: 210mm;margin: auto;"> </div>
  </div>
</template>
<script lang="ts">
import { Vue, Component } from 'vue-property-decorator'
import { Getter, Action } from 'vuex-class'
@Component
export default class cousrseActivity extends Vue {
  @Getter commonData
  @Action transcriptDetail
  $refs: {
    onePage: HTMLElement,
    twoPage: HTMLElement
  }
  pdfTitle: string = ''
}
</script>
//對(duì)打印 做的 兼容
<style media="print" type="text/css">
  @page {
    size: auto;
    margin: 0mm;
  }
  /* 在chrome下可以使用background屬性 */
  body {
    -webkit-print-color-adjust: exact;
  }
  @media print {
    .transcript-container.transcript-detail .transcript-wrap {
      margin-bottom: 0;
    }
  }
</style>

遇到的問題

多行省略號(hào)

多行省略號(hào) 在html2canvas 時(shí) 由于不能解析 display: -webkit-box; 會(huì)導(dǎo)致生成的圖片 錯(cuò)誤

.ellipsis{
  overflow : hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;      /* 可以顯示的行數(shù),超出部分用...表示*/
  -webkit-box-orient: vertical;
 }

目前 我這邊正常顯示時(shí) 使用多行省略號(hào) 在打印時(shí) 將 display: -webkit-box;改成display:blcok 就能正常顯示了

圖片模糊 生成的pdf 不清楚

解決辦法: 將canvas的屬性width和height屬性放大為2倍,也就是,先將canvas高分辨率輸出,再來壓縮導(dǎo)出打印

// 導(dǎo)出頁面為PDF格式
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
export default{
  install (Vue, options) {
    Vue.prototype.getPdf = function () {
      var pdfTitle = this.pdfTitle
      var pdfDom = document.querySelector('#pdfDom')
      var c = document.createElement('canvas')
      html2Canvas(pdfDom, {
        useCORS: true,
        scale: 2,
        canvas: c,
        logging: true,
        width: pdfDom.width,
        height: pdfDom.height
      // allowTaint: true
      }).then(function (canvas) {
        console.log(canvas)
        const marginBottom = 34
        let canvasWidth = canvas.width
        let canvasHeight = canvas.height + marginBottom * 2
        console.log(canvasWidth)
        console.log(canvasHeight)
        let pageHeight = canvasWidth / 592.28 * 841.89 + marginBottom * 2
        let allPageHeight = canvasHeight
        let position = 0
        let imgWidth = 595.28
        let imgHeight = 592.28 / canvasWidth * canvasHeight
        let pageData = canvas.toDataURL('image/jpeg', 3.0)
        // console.log(canvasWidth)
        // console.log(canvasHeight)
        // console.log(pageHeight)
        // console.log(allPageHeight)
        // console.log(position)
        // console.log(imgWidth)
        // console.log(imgHeight)
        // console.log(pageData)
        let PDF = new JsPDF('', 'pt', 'a4')
        if (allPageHeight < pageHeight) {
          PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
        } else {
          while (allPageHeight > 0) {
            PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
            allPageHeight = allPageHeight - pageHeight - marginBottom
            position = position - 841.89 - marginBottom
            if (allPageHeight > 0) {
              PDF.addPage()
            }
          }
        }
        PDF.save(pdfTitle + '.pdf')
      })
    }
  }
}

處理過的圖片 能清晰一點(diǎn) 但是生成的pdf 也大了一倍

圖片跨域 Tained canvases may not be exported

在test 服務(wù)器上 一點(diǎn)問題都沒有 可以正常下載 一大包到線上 就開始報(bào)跨域的錯(cuò)誤

百度了一下 基本都是一樣的 復(fù)制來 復(fù)制去 給的辦法 還是沒發(fā)處理跨域的問題

看了一下html2canvas api 發(fā)現(xiàn)了 一個(gè)屬性 proxy 代理完的圖片 但是還是報(bào)跨域的問題 生成的pdf 還是沒有圖片

最后發(fā)現(xiàn) 頁面里邊的圖片可以正產(chǎn)顯示 只有外域的圖片不能顯示 本域的圖片用base64顯示的 外域的圖片是不是也能用base64顯示

base64 Data URL scheme 支持的類型:

  • data:,文本數(shù)據(jù)
  • data:text/plain,文本數(shù)據(jù)
  • data:text/html,HTML代碼
  • data:text/html;base64,base64編碼的HTML代碼
  • data:text/css,CSS代碼
  • data:text/css;base64,base64編碼的CSS代碼
  • data:text/JavaScript,Javascript代碼
  • data:text/javascript;base64,base64編碼的Javascript代碼
  • data:image/gif;base64,base64編碼的gif圖片數(shù)據(jù)
  • data:image/png;base64,base64編碼的png圖片數(shù)據(jù)
  • data:image/jpeg;base64,base64編碼的jpeg圖片數(shù)據(jù)

將外域 的圖片弄成base64 后 生成的pdf里邊的圖片 可以正常顯示了 也不報(bào)跨域的問題了

總結(jié)

到此這篇關(guān)于vue使用html2canvas和jspdf將html轉(zhuǎn)成pdf的文章就介紹到這了,更多相關(guān)html2canvas jspdf將html轉(zhuǎn)pdf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何在vue3中同時(shí)使用tsx與setup語法糖

    如何在vue3中同時(shí)使用tsx與setup語法糖

    這篇文章主要介紹了如何在vue3中同時(shí)使用tsx與setup語法糖,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • vue3導(dǎo)入excel并解析excel數(shù)據(jù)渲染到表格中(純前端實(shí)現(xiàn))

    vue3導(dǎo)入excel并解析excel數(shù)據(jù)渲染到表格中(純前端實(shí)現(xiàn))

    在Vue中實(shí)現(xiàn)導(dǎo)出Excel有多種方式,可以通過前端實(shí)現(xiàn),也可以通過前后端配合實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于vue3導(dǎo)入excel并解析excel數(shù)據(jù)渲染到表格中的相關(guān)資料,文中介紹的方法是純前端實(shí)現(xiàn),需要的朋友可以參考下
    2024-04-04
  • iview tabs 頂部導(dǎo)航欄和模塊切換欄的示例代碼

    iview tabs 頂部導(dǎo)航欄和模塊切換欄的示例代碼

    這篇文章主要介紹了iview tabs 頂部導(dǎo)航欄和模塊切換欄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 詳解VUE Element-UI多級(jí)菜單動(dòng)態(tài)渲染的組件

    詳解VUE Element-UI多級(jí)菜單動(dòng)態(tài)渲染的組件

    這篇文章主要介紹了VUE Element-UI多級(jí)菜單動(dòng)態(tài)渲染的組件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • vue3中輕松實(shí)現(xiàn)switch功能組件的全過程

    vue3中輕松實(shí)現(xiàn)switch功能組件的全過程

    這篇文章主要給大家介紹了關(guān)于vue3中輕松實(shí)現(xiàn)switch功能組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • vuejs如何解決瀏覽器切換頁面后setInterval計(jì)時(shí)器停止執(zhí)行的問題

    vuejs如何解決瀏覽器切換頁面后setInterval計(jì)時(shí)器停止執(zhí)行的問題

    setinterval()是定時(shí)調(diào)用的函數(shù),可按照指定的周期(以毫秒計(jì))來調(diào)用函數(shù)或計(jì)算表達(dá)式,這篇文章主要給大家介紹了關(guān)于vuejs如何解決瀏覽器切換頁面后setInterval計(jì)時(shí)器停止執(zhí)行的問題,需要的朋友可以參考下
    2024-01-01
  • VUE前端刪除和批量刪除實(shí)現(xiàn)代碼

    VUE前端刪除和批量刪除實(shí)現(xiàn)代碼

    這篇文章主要給大家介紹了關(guān)于VUE前端刪除和批量刪除的相關(guān)資料, 在實(shí)際的開發(fā)中,我們可以使用Vue.js來快速實(shí)現(xiàn)批量刪除功能,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-07-07
  • vue做移動(dòng)端適配最佳解決方案(親測(cè)有效)

    vue做移動(dòng)端適配最佳解決方案(親測(cè)有效)

    這篇文章主要介紹了vue做移動(dòng)端適配最佳解決方案(親測(cè)有效),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • vue中記錄滾動(dòng)條位置的兩種方法

    vue中記錄滾動(dòng)條位置的兩種方法

    最近用 Vue 做移動(dòng)端頁面遇到一個(gè)問題,需要記住滾動(dòng)條的位置,所以下面這篇文章主要給大家介紹了關(guān)于vue中記錄滾動(dòng)條位置的兩種方法,文中給出了詳細(xì)的實(shí)例,需要的朋友可以參考下
    2023-01-01
  • vue實(shí)現(xiàn)彈窗拖拽效果

    vue實(shí)現(xiàn)彈窗拖拽效果

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)彈窗拖拽效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09

最新評(píng)論

广德县| 卢湾区| 德惠市| 凤庆县| 永泰县| 芦溪县| 吴旗县| 民县| 宾川县| 连山| 宜兴市| 海盐县| 文登市| 阿图什市| 北流市| 平乡县| 中阳县| 大荔县| 独山县| 嵊泗县| 左贡县| 浠水县| 兴和县| 平潭县| 阳城县| 三台县| 灵川县| 西乌珠穆沁旗| 扎鲁特旗| 大理市| 禹城市| 太和县| 肃南| 霍邱县| 故城县| 南阳市| 义乌市| 平武县| 南宁市| 托克托县| 通河县|