web建站教程
     
  1. 首页
  2. 前端UI组件库
  3. AI项目和框架
  4. AIGC工具
  5. 百度echarts
  6. 地图大全
  7. 前端知识
  8. 更多
    vuejs
    js入门
    php入门
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    AI应用

vuejs将网页中指定div转成pdf并下载

1496 ℃

1、思路:先将页面html转换成图片,再将图片生成pdf后下载。

2、安装:

//页面html转换成图片
npm install --save html2canvas
//图片生成pdf
npm install jspdf --save

3、组件引入

import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'

4、方法

getHtmlCanvas(){
  var title = this.title;
  html2Canvas(document.querySelector('#divPdf'), {
    allowTaint: true
  }).then(function (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 divPdf = new JsPDF('', 'pt', 'a4')
    if (leftHeight < pageHeight) {
      divPdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight)
    } else {
      while (leftHeight > 0) {
        divPdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
        leftHeight -= pageHeight
        position -= 841.89
        if (leftHeight > 0) {
          divPdf.addPage()
        }
      }
    }
    divPdf.save(title + '.pdf')
  }
  )
}

QRCanvas动态生成二维码js插件库

html2canvas动态生成微信分享海报文档官网介绍

html2canvas生成图片后下载太慢解决方法(dom-to-image)

javascript根据当前div样式导出pdf

php利用微信小程序实现pdf文件的预览功能

标签: html2canvas pdf

上面是“vuejs将网页中指定div转成pdf并下载”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_2175.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 首页 > vuejs
Trae:新一代免费的AI编程工具

呱呱工具箱

一款免费的在线小工具,无需下载,打开即可使用!呱呱工具箱

在线育儿补贴计算器

快来看看你到底可以领到多少补贴!生活小工具
上一篇:
下一篇:
x 打工人ai神器