js 生成pdf 并上传文件

js 生成pdf 并上传文件

使用 JsPDF + html2Canvas 代码直接使用 注意注释

复制代码
import JsPDF from 'jspdf'
import html2Canvas from 'html2canvas'
// 上传文件的方法
import { handleUploadImage } from '@/utils/uploadQuillEdit'

downPDF() {
	//	 要打印元素的id
      const cloneDom = document.getElementById('rentArea').cloneNode(true)
      document.getElementsByTagName('body')[0].appendChild(cloneDom)
      html2Canvas(
        cloneDom,
        { allowTaint: true }
      ).then((canvas) => {
        let contentWidth = canvas.width
        let contentHeight = canvas.height
        let pageHeight = contentWidth / 592.28 * 841.89
        let leftHeight = contentHeight
        let position = 0
        let imgWidth = 595.28
        let imgHeight = 592.28 / contentWidth * contentHeight
        let pageData = canvas.toDataURL('image/jpeg', 1.0)
        let 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()
            }
          }
        }

        // 将 PDF 转换为 Blob 对象
        const pdfBlob = PDF.output('blob')
        // file对象
        const pdfFile = new File([pdfBlob], `面积、期限及租金${new Date().getTime()}.pdf`, {
          type: 'application/pdf',
          lastModified: new Date().getTime()
        })
		
		//上传文件
        handleUploadImage(pdfFile).then(data => {
          console.log(data)
        })
		
		//这里是直接保存pdf
        // PDF.save(`${this.dataForm.customerNameShow}项目智能分仓方案书(${timeToTime(new Date(), false)}).pdf`)
        document.getElementsByTagName('body')[0].removeChild(cloneDom)
      })
    }
相关推荐
摸鱼仙人~25 分钟前
校招帮简历字段匹配架构分析报告
java·前端·架构
陈随易1 小时前
副业搞钱点子之赛事与活动
前端·后端·程序员
电梯界知识分子1 小时前
景德镇老房重新设计阶段规划楼梯与电梯,1.7m×1.6m空间装400kg观光电梯,棕色框架茶色玻璃适配中式装修
大数据·服务器·前端·网络·家用电梯
Shell运维手记1 小时前
Shell 脚本入门课堂讲解
前端·chrome
Profile排查笔记1 小时前
有哪些开源指纹浏览器项目?先核对客户端、内核与许可证
前端·人工智能·后端·自动化
CodeGL1 小时前
16、Python Web开发
开发语言·前端·python
FRing031 小时前
JavaScript入门到精通二十九(内置构造函数(Object、Array))
开发语言·前端·javascript
AAA程序技术1 小时前
从 DOM 到屏幕:浏览器渲染管线六步拆解
前端·浏览器
zach1 小时前
useContext 深入讲解:组件透传、原理与最佳实践
前端
张一凡932 小时前
AI 改完代码,人却不知道它改了什么:架构漂移才是真正的成本
前端·后端·agent