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)
      })
    }
相关推荐
我材不敲代码10 小时前
Python 函数核心:位置参数与关键字参数详解
java·前端·python
丷丩10 小时前
MapLibre GL JS第8课:禁用滚动缩放
javascript·mapbox·maplibre gl js
Kratzdisteln10 小时前
【无标题】
前端·python
Curvatureflight10 小时前
前端国际化 i18n 落地实践:语言包、动态文案和格式化问题怎么处理?
前端·c++·vue
kTR2hD1qb11 小时前
Claude Code Skill的介绍与使用
java·前端·数据库·人工智能
2501_9307077811 小时前
使用C#代码替换 PDF 文档中的文本
pdf
修己xj11 小时前
打造专属博文封面神器:一个开源免费的博文封面生成器ThisCover
前端
kyriewen12 小时前
面试8家前端岗位后,我发现了一个残酷的事实:AI不是加分项,是门槛
前端·javascript·面试
Fighting_p12 小时前
【面试 - el-select问题及解决】wujie 微前端下子系统 el-select 多选 filterable 过滤失效
前端
吃口巧乐兹12 小时前
AI 全栈时代,为什么要服务端使用 NestJs
前端