File size: 2,839 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import html2canvas from 'html2canvas';
import jsPDF from 'jspdf';
import ReactDOM from 'react-dom/client';

import CentralizedView from '../components/dashboard/CentralizedView';

export const exportToPDF = async (
  auditName: string,
  selectedDisplays: string[],
  auditId: string,
) => {
  const container = document.createElement('div');
  container.style.position = 'absolute';
  container.style.left = '-9999px';
  container.style.top = '-9999px';
  document.body.appendChild(container);

  const root = ReactDOM.createRoot(container);
  root.render(
    <CentralizedView auditId={auditId} selectedDisplays={selectedDisplays} />,
  );

  await new Promise(resolve => setTimeout(resolve, 2000));

  const pdf = new jsPDF('p', 'mm', 'a4');
  const pageWidth = pdf.internal.pageSize.getWidth();
  const pageHeight = pdf.internal.pageSize.getHeight();

  pdf.setFillColor(31, 41, 55);
  pdf.rect(0, 0, pageWidth, pageHeight, 'F');

  pdf.setTextColor(255, 255, 255);
  pdf.setFontSize(24);
  pdf.text(auditName, pageWidth / 2, 20, { align: 'center' });

  let yOffset = 30;
  const margin = 10;
  const availableWidth = pageWidth - 2 * margin;
  const maxHeight = (pageHeight - yOffset - 3 * margin) / 2;

  const captureAndAddChart = async (displayId: string, index: number) => {
    const element: HTMLElement | null = container.querySelector(
      `#${displayId}`,
    );
    if (element) {
      try {
        const canvas = await html2canvas(element, {
          backgroundColor: '#1f2937',
          scale: 2,
        });
        const imgData = canvas.toDataURL('image/png');

        const aspectRatio = canvas.width / canvas.height;
        let imgWidth = availableWidth;
        let imgHeight = imgWidth / aspectRatio;

        if (imgHeight > maxHeight) {
          imgHeight = maxHeight;
          imgWidth = imgHeight * aspectRatio;
        }

        if (index % 2 === 0 && index !== 0) {
          pdf.addPage();
          yOffset = 10;
          pdf.setFillColor(31, 41, 55);
          pdf.rect(0, 0, pageWidth, pageHeight, 'F');
        }

        const xOffset = (pageWidth - imgWidth) / 2;

        pdf.addImage(
          imgData,
          'PNG',
          xOffset,
          yOffset,
          imgWidth,
          imgHeight,
          undefined,
          'FAST',
        );
        yOffset += imgHeight + margin;

        if (index % 2 === 1) {
          yOffset = 10;
        }
      } catch (error) {
        console.error('Error capturing element:', error);
        pdf.setTextColor(255, 255, 255);
        pdf.text('Error capturing chart', margin, yOffset);
        yOffset += 20;
      }
    }
  };

  for (let i = 0; i < selectedDisplays.length; i++) {
    await captureAndAddChart(selectedDisplays[i], i);
  }

  pdf.save(`${auditName}.pdf`);

  root.unmount();
  document.body.removeChild(container);
};