import React from 'react'; import html2Canvas from "html2canvas"; import JsPDF from "jspdf"; import { Modal, message } from 'antd'; const obj = { // pdf导出 getPdf: function (title, id) { const element = document.getElementById(id); // let width = 592.28, height = 841.89; let width = 595, height = 842; const scale = 2; let w = element.scrollWidth; let h = element.scrollHeight; let offsetTop = 100; let offsetLeft = (window.innerWidth - 874) / 2; const canvas = document.createElement('canvas'); let abs = 7; // let win_i = 1900; // let win_o = window.innerWidth; // if (win_o > win_i) { // abs = (win_o - win_i) / 2 // } canvas.width = w * scale; canvas.height = h * scale; let context = canvas.getContext("2d"); context.scale(scale, scale) context.translate(-offsetLeft - abs, -offsetTop); html2Canvas(element, { allowTaint: true, taintTest: true, canvas, dpi: 192, background: '#fff', }).then(function (canvas) { let contentWidth = canvas.width, contentHeight = canvas.height, pageHeight = contentWidth / width * height, leftHeight = contentHeight, position = 0, imgWidth = width, imgHeight = width / contentWidth * contentHeight, pageData = canvas.toDataURL('image/jpeg', 1.0), pdf = new JsPDF('', 'pt', 'a4'); if (leftHeight < pageHeight) { pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight) // 让导出的文件两边产生边距,边距是0 } else { while (leftHeight > 0) { pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight) leftHeight -= pageHeight; position -= height; if (leftHeight > 0) { pdf.addPage(); } } } pdf.save(title + ".pdf"); }); }, // 登录状态 getLoginStatus: function (data) { if (data.error[0].field == "403") { Modal.error({ title: '提示', content: (

登录超时已失效,请重新登录!

), okText: '重新登录', onOk() { window.location.href = globalConfig.context + "/RD/admin/login"; }, }); } else { message.warning(data.error[0].message); } }, } module.exports = obj