jspdf 插件 -- 导出 pdf 文件

let contentWidthPage = document.getElementsByClassName('warning-wrapper')[0].offsetWidth
let aaa = contentWidthPage
let contentItemHeight = document.getElementsByClassName('warning-item')[0].offsetHeight
let bbb = contentItemHeight*4

// 以上代码无用

/**
 * 导出 pdf 文件
 */
import html2canvas from 'html2canvas'
import JsPDF from 'jspdf'

export default function exportPdf(title, dom) {
    
    
  html2canvas(document.querySelector(dom), {
    
     allowTaint: true }).then(
 (canvas) => {
    
    
   let contentWidth = canvas.width
   let contentHeight = canvas.height
   let pageHeight = (contentWidth / 592.28) * 696
   let leftHeight = contentHeight
   // let position = 0
   let position = 30
   let imgWidth = 595.28
   let imgHeight = (592.28 / contentWidth) * contentHeight
   let pageData = canvas.toDataURL('image/jpeg', 1.0)
   // let PDF = new JsPDF('', 'pt', 'a4')
   let PDF = new JsPDF('', 'pt', [590, 696])
   if (leftHeight < pageHeight) {
    
    
     // PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
     PDF.addImage(pageData, 'JPEG', 10, 0, imgWidth, imgHeight)
   } else {
    
    
     while (leftHeight > 0) {
    
    
       // PDF.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
       PDF.addImage(pageData, 'JPEG', 10, position, imgWidth, imgHeight)
       leftHeight -= pageHeight
       // position -= 841.89
       position -= 696
       if (leftHeight > 0) {
    
    
         PDF.addPage()
       }
     }
   }
   PDF.save('测试' + '.pdf')
 })
}

jspdf 的 addImage() 函数的参数及其描述的确切列表是什么?
https://qa.1r1g.com/sf/ask/3132893151/

jspdf用法
https://blog.csdn.net/weixin_42333548/article/details/107630706

猜你喜欢

转载自blog.csdn.net/m0_53562074/article/details/127964259