tools.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React from 'react';
  2. import html2Canvas from "html2canvas";
  3. import JsPDF from "jspdf";
  4. import { Modal, message } from 'antd';
  5. const obj = {
  6. // pdf导出
  7. getPdf: function (title, id) {
  8. const element = document.getElementById(id);
  9. // let width = 592.28, height = 841.89;
  10. let width = 595, height = 842;
  11. const scale = 2;
  12. let w = element.scrollWidth;
  13. let h = element.scrollHeight;
  14. let offsetTop = 100;
  15. let offsetLeft = (window.innerWidth - 874) / 2;
  16. const canvas = document.createElement('canvas');
  17. let abs = 7;
  18. // let win_i = 1900;
  19. // let win_o = window.innerWidth;
  20. // if (win_o > win_i) {
  21. // abs = (win_o - win_i) / 2
  22. // }
  23. canvas.width = w * scale;
  24. canvas.height = h * scale;
  25. let context = canvas.getContext("2d");
  26. context.scale(scale, scale)
  27. context.translate(-offsetLeft - abs, -offsetTop);
  28. html2Canvas(element, {
  29. allowTaint: true,
  30. taintTest: true,
  31. canvas,
  32. dpi: 192,
  33. background: '#fff',
  34. }).then(function (canvas) {
  35. let contentWidth = canvas.width,
  36. contentHeight = canvas.height,
  37. pageHeight = contentWidth / width * height,
  38. leftHeight = contentHeight,
  39. position = 0,
  40. imgWidth = width,
  41. imgHeight = width / contentWidth * contentHeight,
  42. pageData = canvas.toDataURL('image/jpeg', 1.0),
  43. pdf = new JsPDF('', 'pt', 'a4');
  44. if (leftHeight < pageHeight) {
  45. pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight) // 让导出的文件两边产生边距,边距是0
  46. } else {
  47. while (leftHeight > 0) {
  48. pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
  49. leftHeight -= pageHeight;
  50. position -= height;
  51. if (leftHeight > 0) {
  52. pdf.addPage();
  53. }
  54. }
  55. }
  56. pdf.save(title + ".pdf");
  57. });
  58. },
  59. // 登录状态
  60. getLoginStatus: function (data) {
  61. if (data.error[0].field == "403") {
  62. Modal.error({
  63. title: '提示',
  64. content: (
  65. <div>
  66. <p>登录超时已失效,请重新登录!</p>
  67. </div>
  68. ),
  69. okText: '重新登录',
  70. onOk() {
  71. window.location.href = globalConfig.context + "/RD/admin/login";
  72. },
  73. });
  74. } else {
  75. message.warning(data.error[0].message);
  76. }
  77. },
  78. }
  79. module.exports = obj