国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

js幾種打印方法

這篇具有很好參考價值的文章主要介紹了js幾種打印方法。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1、使用printJs插件(優(yōu)先使用)

printjs官網(wǎng)文章來源地址http://www.zghlxwxcb.cn/news/detail-515415.html

// 官網(wǎng)文檔很詳細(xì)!
// 舉例如下:
printJS({
   printable: [this.blobUrl],
   type: 'pdf'
})

2、使用window.open的方式

// 舉例如下:
// 其中imgUrl為base64格式:'data:image/' + type + ';base64,' + ...
 let oWin = window.open(
  '',
    'pringwindow',
    'menubar=no,location=no,resizable=yes,scrollbars=no,status=no,width=1000,height=660'
  )
  oWin.document.fn = function () {
    if (oWin) {
      oWin.print()
      oWin.close()
    }
  }
  let html =
    '<div style="height: 100%;width: 100%;">' +
    `<img src="${this.imgUrl}" οnlοad="fn()" style="max-height:100%;max-width: 100%;" />` +
    '</div>'
  oWin.document.open()
  oWin.document.write(html)
  oWin.document.close()

3、使用iframe方式

// 舉例如下:
const iframe = document.createElement('iframe')

     iframe.style.height = 0
     iframe.style.visibility = 'hidden'
     iframe.style.width = 0

     iframe.setAttribute('srcdoc', '<html><body></body></html>')
     document.body.appendChild(iframe)

     iframe.addEventListener('load', function () {
       // 這里獲取image的前提是html中有一個id為image的dom
       // 例如:<img id="image" :src="圖片路徑"/>
       const image = document.getElementById('image').cloneNode()
       image.style.maxWidth = '100%'

       const body = iframe.contentDocument.body
       body.style.textAlign = 'center'
       body.appendChild(image)
       image.addEventListener('load', function () {
         iframe.contentWindow.print()
       })
     })

	iframe.contentWindow.addEventListener('afterprint', function () {
	   iframe.parentNode.removeChild(iframe)
	})

4、在electron中封裝一個打印pdf的方法(這個一般用不到)

 const reader = new FileReader()
 // blobUrl: 一個blob流
   reader.readAsDataURL(blobUrl)
    reader.addEventListener('loadend', () => {
      nodeApi.sendnew('printResponsePdf', {
        buffer: Buffer.from(
          reader.result.split('base64,')[1],
          'base64'
        ),
    })
})

// 提供一個base64轉(zhuǎn)blob的方式吧
 base64ToBlob() {
   let imgSrc = this.imgUrl // imgUrl為base64格式的路徑

   let arr = imgSrc.split(',')
   let array = arr[0].match(/:(.*?);/)
   let mime = (array && array.length > 1 ? array[1] : type) || type
   // 去掉url的頭,并轉(zhuǎn)化為byte
   let bytes = window.atob(arr[1])
   // 處理異常,將ascii碼小于0的轉(zhuǎn)換為大于0
   let ab = new ArrayBuffer(bytes.length)
   // 生成視圖(直接針對內(nèi)存):8位無符號整數(shù),長度1個字節(jié)
   let ia = new Uint8Array(ab)
   for (let i = 0; i < bytes.length; i++) {
    ia[i] = bytes.charCodeAt(i)
   }
   return new Blob([ab], {
     type: mime,
   })
 },

到了這里,關(guān)于js幾種打印方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包