-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfExporter.js
More file actions
136 lines (114 loc) · 4.66 KB
/
pdfExporter.js
File metadata and controls
136 lines (114 loc) · 4.66 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* PDF Exporter Module
* Handles PDF generation from JSON tables
*/
/**
* Generate and export the table as PDF
* @returns {Promise<void>}
*/
async function generatePDF() {
const pdfBtn = document.getElementById('pdfBtn');
if (!currentJsonData) {
alert('Please generate a table first!');
return;
}
// Show loading state
pdfBtn.disabled = true;
pdfBtn.textContent = 'Generating...';
try {
// Get settings
const pageSize = document.getElementById('pageSize').value;
const orientation = document.getElementById('orientation').value;
const fileName = document.getElementById('fileName').value || 'json-table-export';
const quality = parseInt(document.getElementById('quality').value);
// Create a clean PDF container
const pdfContainer = document.createElement('div');
pdfContainer.className = 'pdf-container';
pdfContainer.style.position = 'absolute';
pdfContainer.style.left = '-9999px';
pdfContainer.style.background = 'white';
pdfContainer.style.padding = '20px';
// Add header
const header = document.createElement('div');
header.className = 'pdf-header';
header.style.textAlign = 'center';
header.style.marginBottom = '20px';
header.style.borderBottom = '1px solid #000';
header.style.paddingBottom = '10px';
header.innerHTML = `
<h1 style="margin: 0; font-size: 18px;">JSON Data Export</h1>
<div style="margin-top: 5px; font-size: 12px;">Generated on ${new Date().toLocaleString()}</div>
`;
// Create clean minimal table for PDF
const tableContainer = document.createElement('div');
tableContainer.innerHTML = createMinimalPDFTable(currentJsonData);
// Add elements to container
pdfContainer.appendChild(header);
pdfContainer.appendChild(tableContainer);
// Temporarily add to body for rendering
document.body.appendChild(pdfContainer);
// Generate canvas
const canvas = await html2canvas(pdfContainer, {
scale: quality,
useCORS: true,
backgroundColor: '#ffffff'
});
// Remove temporary element
document.body.removeChild(pdfContainer);
// Create PDF
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({
orientation: orientation,
unit: 'mm',
format: pageSize
});
// Get page dimensions
const pageWidth = pdf.internal.pageSize.getWidth();
const pageHeight = pdf.internal.pageSize.getHeight();
const margin = 10;
// Calculate image dimensions
const imgWidth = pageWidth - (margin * 2);
const imgHeight = (canvas.height * imgWidth) / canvas.width;
// Add image to PDF
const imgData = canvas.toDataURL('image/png');
if (imgHeight <= pageHeight - (margin * 2)) {
// Single page
pdf.addImage(imgData, 'PNG', margin, margin, imgWidth, imgHeight);
} else {
// Multiple pages handling with proper positioning
let heightLeft = imgHeight;
let position = 0;
let page = 1;
pdf.addImage(imgData, 'PNG', margin, margin, imgWidth, imgHeight);
heightLeft -= pageHeight - (margin * 2);
while (heightLeft > 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position + margin, imgWidth, imgHeight);
heightLeft -= (pageHeight - (margin * 2));
page++;
}
}
// Save PDF
pdf.save(`${fileName}.pdf`);
// Show success message
const output = document.getElementById('output');
const successMsg = document.createElement('div');
successMsg.className = 'success';
successMsg.textContent = `PDF "${fileName}.pdf" has been generated successfully!`;
output.appendChild(successMsg);
// Remove success message after 3 seconds
setTimeout(() => {
if (successMsg.parentNode) {
successMsg.parentNode.removeChild(successMsg);
}
}, 3000);
} catch (error) {
console.error('PDF generation error:', error);
alert('Error generating PDF: ' + error.message);
} finally {
// Reset button state
pdfBtn.disabled = false;
pdfBtn.textContent = 'Export PDF';
}
}