欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實(shí)現(xiàn)html轉(zhuǎn)化pdf并復(fù)制文字

 更新時(shí)間:2024年10月22日 09:56:41   作者:Z_Xshan  
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)html轉(zhuǎn)化pdf的兩種方式,分別為能復(fù)制文字和不能復(fù)制文字的方法,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、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 MyPlugin

main.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)容多的時(shí)候只打印第一頁)

②頁面不想展示打印內(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實(shí)現(xiàn)html轉(zhuǎn)化pdf并復(fù)制文字的文章就介紹到這了,更多相關(guān)vue html轉(zhuǎn)pdf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決vue-cli 打包后自定義動畫未執(zhí)行的問題

    解決vue-cli 打包后自定義動畫未執(zhí)行的問題

    今天小編就為大家分享一篇解決vue-cli 打包后自定義動畫未執(zhí)行的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue-cli常用設(shè)置總結(jié)

    vue-cli常用設(shè)置總結(jié)

    本文給大家總結(jié)了vue-cli常用設(shè)置,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • Vue環(huán)境搭建+VSCode+Win10的詳細(xì)教程

    Vue環(huán)境搭建+VSCode+Win10的詳細(xì)教程

    這篇文章主要介紹了Vue環(huán)境搭建+VSCode+Win10,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • vue-Split實(shí)現(xiàn)面板分割

    vue-Split實(shí)現(xiàn)面板分割

    這篇文章主要為大家詳細(xì)介紹了vue-Split實(shí)現(xiàn)面板分割,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 淺談mint-ui loadmore組件注意的問題

    淺談mint-ui loadmore組件注意的問題

    下面小編就為大家?guī)硪黄獪\談mint-ui loadmore組件注意的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • Vue駝峰與短橫線分割命名中有哪些坑

    Vue駝峰與短橫線分割命名中有哪些坑

    這篇文章主要介紹了Vue駝峰與短橫線分割命名中的注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-02-02
  • 關(guān)于pinia的簡單使用方式

    關(guān)于pinia的簡單使用方式

    這篇文章主要介紹了關(guān)于pinia的簡單使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • vue中使用echarts的方法實(shí)例詳解

    vue中使用echarts的方法實(shí)例詳解

    這篇文章主要介紹了vue中使用echarts的方法,結(jié)合實(shí)例形式詳細(xì)分析了vue中使用echarts的包安裝、引入、生命周期函數(shù)元素掛載等相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下
    2023-05-05
  • vue 封裝面包屑組件教程

    vue 封裝面包屑組件教程

    這篇文章主要介紹了vue 封裝面包屑組件教程,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Vue axios 將傳遞的json數(shù)據(jù)轉(zhuǎn)為form data的例子

    Vue axios 將傳遞的json數(shù)據(jù)轉(zhuǎn)為form data的例子

    今天小編就為大家分享一篇Vue axios 將傳遞的json數(shù)據(jù)轉(zhuǎn)為form data的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10

最新評論