vue前端怎么导出图片_VUE中将页面导出为图片或者PDF

vue前端怎么导出图片_VUE中将页面导出为图片或者PDF导出为图片1.将页面html转换成图片npminstallhtml2canvas–save2.在需要导出的页面引入importhtml2canvasfrom’html2canvas’;3.在methods中添加方法dataURLToBlob(dataurl){//ie图片转格式vararr=dataurl.split(‘,’),mime=arr[0].m…

大家好,又见面了,我是你们的朋友全栈君。

导出为图片

1.将页面html转换成图片   npm install html2canvas –save

2.在需要导出的页面引入  import html2canvas from ‘html2canvas’;

3.在 methods 中添加方法

dataURLToBlob(dataurl) {//ie 图片转格式

var arr = dataurl.split(‘,’), mime = arr[0].match(/:(.*?);/)[1],

bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);

while (n–) {

u8arr[n] = bstr.charCodeAt(n);

}

return new Blob([u8arr], {type: mime})

},

downloadResult(name) {

let canvasID = document.body

let a = document.createElement(‘a’);

html2canvas(canvasID).then(canvas => {

let dom = document.body.appendChild(canvas);

dom.style.display = “none”;

a.style.display = “none”;

document.body.removeChild(dom);

let blob = this.dataURLToBlob(dom.toDataURL(“image/png”));

a.setAttribute(“href”, URL.createObjectURL(blob));

a.setAttribute(“download”, name + “.png”)

document.body.appendChild(a);

a.click();

URL.revokeObjectURL(blob);

document.body.removeChild(a);

});

},

printOut(name) {

// 个人观察只是截取可见范围以及以下的区域,所以先将滚动条置顶

$(window).scrollTop(0); // jQuery 的方法

document.body.scrollTop = 0; // IE的

document.documentElement.scrollTop = 0; // 其他

this.downloadResult(name)

},

导出为PDF

1.将页面html转换成图片  npm install html2canvas –save

2.将图片生成pdf  npm install jspdf –save

3.在需要导出的页面引入  import html2canvas from ‘html2canvas’;   import JsPDF from ‘jspdf’

4.在 methods 中添加方法

printOut(name) {

let shareContent = document.body,//需要截图的包裹的(原生的)DOM 对象

width = shareContent.clientWidth, //获取dom 宽度

height = shareContent.clientHeight, //获取dom 高度

canvas = document.createElement(“canvas”), //创建一个canvas节点

scale = 2; //定义任意放大倍数 支持小数

canvas.width = width * scale; //定义canvas 宽度 * 缩放

canvas.height = height * scale; //定义canvas高度 *缩放

canvas.style.width = shareContent.clientWidth * scale + “px”;

canvas.style.height = shareContent.clientHeight * scale + “px”;

canvas.getContext(“2d”).scale(scale, scale); //获取context,设置scale

let opts = {

scale: scale, // 添加的scale 参数

canvas: canvas, //自定义 canvas

logging: false, //日志开关,便于查看html2canvas的内部执行流程

width: width, //dom 原始宽度

height: height,

useCORS: true, // 【重要】开启跨域配置

};

html2Canvas(shareContent, opts).then(() => {

var contentWidth = canvas.width;

var contentHeight = canvas.height;

//一页pdf显示html页面生成的canvas高度;

var pageHeight = (contentWidth / 592.28) * 841.89;

//未生成pdf的html页面高度

var leftHeight = contentHeight;

//页面偏移

var position = 0;

//a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高

var imgWidth = 595.28;

var imgHeight = (592.28 / contentWidth) * contentHeight;

var pageData = canvas.toDataURL(“image/jpeg”, 1.0);

var 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.save(name + “.pdf”); // 这里是导出的文件名

});

},

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/141618.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 单据保存后的存储过程称为_原始单据保存多少年

    单据保存后的存储过程称为_原始单据保存多少年(一)报关单证保存后的存储过程更新T_modulelist的StorageProductAfterSave值UPDATET_ModuleListSETStorageProductAfterSave=‘SaveInvoiceAfter’wheremodulename=‘报关单证’2.创建存储过程CREATEPROCSaveInvoiceAfter(@InvoiceNumvarchar(30))AsBegin…写你的逻辑,其中参数@InvoiceNum是单据号end(二)出

    2022年9月21日
    3
  • DNS域名解析过程_谈谈域名解析DNS的工作原理

    DNS域名解析过程_谈谈域名解析DNS的工作原理一、主机解析域名的顺序1、找缓存2、找本机的hosts文件3、找DNS服务器注意:配置IP和主机名时,要记得修改/etc/hosts文件,因为有些应用程序在主机内的进程之间通信的时候,会本机

    2022年8月2日
    9
  • linux内外网配置_服务器内网ip

    linux内外网配置_服务器内网ip1、/etc/sysconfig/network-scripts/ifcfg-eth0创建这个文件里面的内容如下TYPE=“Ethernet”  BOOTPROTO=“none”  DEFROUTE=“yes”  IPV4_FAILURE_FATAL=“no”  NAME=“eth1”  DEVICE=“eth1”  ONBOOT=“yes”  IPADDR=“192.168…

    2025年11月15日
    3
  • Python任务调度模块 – APScheduler

    Python任务调度模块 – APScheduler

    2021年9月14日
    72
  • QT设置活动窗口「建议收藏」

    QT设置活动窗口「建议收藏」QT设置活动窗口遇到一个QT窗口问题记录下,已经显示的窗口被其他窗口遮挡。再调用show无法将窗口激活显示到最前面。解决方式如下:if(!isActiveWindow())//判断是否是活动窗口{activateWindow();//设置成活动窗口}这样窗口就会被激活。…

    2025年8月11日
    3
  • JSONPath浅析

    JSONPath浅析 在日常的项目开发中,我们常常会为了避免在代码中hardcoding而对某些参数进行配置化,进而提高开发效率和灵活性。而常用的数据结构就是JSON。而对配置的数据的获取的方式常常有以下几种方式:第一种:JSONPath:@TestpublicvoidtestJsonPath(){JSONObjectdata=JSONObject…

    2022年6月16日
    114

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号