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

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

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

A4尺寸

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

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

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

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

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

安裝插件html2canvas和jspdf

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

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

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

在項目中引入

在utils 中 新建htmltopdf.js

htmlToPdf.js

// 導出頁面為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    // 項目頁面顯示微處理 以下用到的地方 可以忽略
        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()方法就會調(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>
//對打印 做的 兼容
<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>

遇到的問題

多行省略號

多行省略號 在html2canvas 時 由于不能解析 display: -webkit-box; 會導致生成的圖片 錯誤

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

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

圖片模糊 生成的pdf 不清楚

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

// 導出頁面為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')
      })
    }
  }
}

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

圖片跨域 Tained canvases may not be exported

在test 服務器上 一點問題都沒有 可以正常下載 一大包到線上 就開始報跨域的錯誤

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

看了一下html2canvas api 發(fā)現(xiàn)了 一個屬性 proxy 代理完的圖片 但是還是報跨域的問題 生成的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里邊的圖片 可以正常顯示了 也不報跨域的問題了

總結(jié)

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

相關文章

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

    如何在vue3中同時使用tsx與setup語法糖

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

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

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

    iview tabs 頂部導航欄和模塊切換欄的示例代碼

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

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

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

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

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

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

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

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

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

    vue做移動端適配最佳解決方案(親測有效)

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

    vue中記錄滾動條位置的兩種方法

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

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

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

最新評論

广州市| 土默特右旗| 博客| 灵台县| 犍为县| 梁河县| 买车| 万安县| 凤凰县| 南召县| 昭苏县| 孙吴县| 文登市| 台江县| 罗城| 承德市| 前郭尔| 苏州市| 英超| 布拖县| 光泽县| 蒙阴县| 教育| 万宁市| 肥城市| 武山县| 临城县| 定州市| 中牟县| 昌乐县| 济南市| 称多县| 富锦市| 北海市| 封开县| 阳谷县| 敦化市| 开封市| 天水市| 和林格尔县| 甘谷县|