zoukankan      html  css  js  c++  java
  • js 导出pdf

    首先需要两个插件:

    html2canvas:http://html2canvas.hertzen.com/documentation
    jsPDF:http://www.rotisedapsales.com/snr/cloud2/website/jsPDF-master/docs/jsPDF.html  

    导出代码:DOM是要导出的html节点
    html2canvas(DOM).then(canvas => {
            // document.body.appendChild(canvas);
            const imageData = canvas.toDataURL("image/jpeg", 1);
            // eslint-disable-next-line new-cap
            const doc = new jsPDF({ userUnit: 2, unit: "px", format: [canvas.width, canvas.height] });
            doc.addImage(imageData, "jpeg", 0, 0, canvas.width, canvas.height);
            doc.save("应对报告.pdf");
          });

     a4纸分页:

    // const domList = document.querySelectorAll('.pdf-report .export-pdf-class')
          // console.log(domList)
          // eslint-disable-next-line new-cap
          // const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' })
          // domList.forEach(q => {
          //   console.log(q)
          //   html2canvas(q).then(canvas => {
          //     const imageData = canvas.toDataURL('image/jpeg', 1)
          //     doc.addImage(imageData, 'jpeg', 0, 0, canvas.width, canvas.height)
          //   })
          // })
          // doc.save(`${row.surveyObject}.pdf`)
          // const DOM = this.$refs.webReport
          // html2canvas(DOM).then(canvas => {
          //   const contentWidth = canvas.width
          //   const contentHeight = canvas.height
          //   const pageHeight = contentWidth / 595.23 * 841.89
          //   let leftHeight = contentHeight
          //   let position = 0
          //   const imgWidth = 595.23
          //   const imgHeight = 595.23 / contentWidth * contentHeight
          //   // document.body.appendChild(canvas);
          //   const imageData = canvas.toDataURL('image/jpeg', 1)
          //   // eslint-disable-next-line new-cap
          //   const doc = new jsPDF({ unit: 'pt', format: 'a4' })
          //   if (leftHeight < pageHeight) {
          //     doc.addImage(imageData, 'jpeg', 0, 0, imgWidth, imgHeight)
          //   } else {
          //     while (leftHeight > 0) {
          //       doc.addImage(imageData, 'jpeg', 0, position, imgWidth, imgHeight)
          //       leftHeight -= pageHeight
          //       position -= 841.89
          //       if (leftHeight > 0) {
          //         doc.addPage()
          //       }
          //     }
          //   }
          //   // doc.addImage(imageData, 'jpeg', 0, 0, canvas.width, canvas.height)
          //   doc.save(`${row.surveyObject}.pdf`)
          // })
    a0: [1189, 841],
    a1: [841, 594],
    a2: [594, 420],
    a3: [420, 297],
    a4: [297, 210], // [841.83, 595.23] unit: 'pt'
    a5: [210, 148]
    
    1pt=0.3528mm。
    1pt=0.35146mm。

    记录学习每一天。

  • 相关阅读:
    ubuntu的apt
    sudo命令
    MySQL导出数据到csv文件
    MySQL导出数据到文件报错
    git_backup.py gitlab项目备份
    java中图像与数组转换
    mongodb转elasticsearch
    impyla-0.14.2.2安装注意事项
    python3.7.3升级 with-openssl openssl-1.0.2a
    hadoop自带性能测试
  • 原文地址:https://www.cnblogs.com/sxdjy/p/14344919.html
Copyright © 2011-2022 走看看