-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDownload_Script_3.js
More file actions
180 lines (147 loc) · 5.54 KB
/
Download_Script_3.js
File metadata and controls
180 lines (147 loc) · 5.54 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Created on Tue Sep 4 12:53:35 2020
@author: Erdos1729
*/
// Configuration
const CONFIG = {
documentName: "Document", // Change this to your desired filename
maxRetries: 3,
retryDelay: 1000,
imageQuality: 0.8
};
// Utility functions
function showMessage(message, type = 'info') {
const div = document.createElement('div');
div.style.cssText = `
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: ${type === 'error' ? 'rgba(255, 0, 0, 0.8)' : 'rgba(0, 0, 0, 0.8)'};
color: white;
padding: 20px;
border-radius: 10px;
z-index: 9999;
font-family: Arial, sans-serif;
text-align: center;
box-shadow: 0 2px 10px rgba(0,0,0,0.2);
`;
div.innerHTML = message;
document.body.appendChild(div);
return div;
}
function removeMessage(div) {
if (div && div.parentNode) {
div.parentNode.removeChild(div);
}
}
// Load jsPDF library
function loadJSPDF() {
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js';
script.onload = resolve;
script.onerror = reject;
document.body.appendChild(script);
});
}
// Main PDF generation function
async function generatePDF() {
const loadingDiv = showMessage('Preparing PDF... Please wait...');
let retryCount = 0;
try {
console.log("Starting PDF generation process...");
const elements = document.getElementsByTagName("img");
let validImages = 0;
// Count valid images
for (let i = 0; i < elements.length; i++) {
if (/^blob:/.test(elements[i].src)) {
validImages++;
}
}
if (validImages === 0) {
throw new Error("No valid images found on the page!");
}
console.log(`Found ${validImages} valid images`);
// Create PDF
const pdf = new jsPDF();
let pageCount = 0;
// Process each image
for (let i = 0; i < elements.length; i++) {
const img = elements[i];
if (!/^blob:/.test(img.src)) {
console.log("Skipping invalid image source");
continue;
}
try {
console.log(`Processing image ${pageCount + 1}/${validImages}`);
// Create canvas and draw image
const canvas = document.createElement('canvas');
const context = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0, img.width, img.height);
// Convert to JPEG with specified quality
const imgData = canvas.toDataURL("image/jpeg", CONFIG.imageQuality);
// Add to PDF
if (pageCount > 0) {
pdf.addPage();
}
// Calculate dimensions to fit page
const pageWidth = pdf.internal.pageSize.getWidth();
const pageHeight = pdf.internal.pageSize.getHeight();
const imgWidth = img.width;
const imgHeight = img.height;
// Calculate scaling to fit page while maintaining aspect ratio
const scale = Math.min(
pageWidth / imgWidth,
pageHeight / imgHeight
);
const scaledWidth = imgWidth * scale;
const scaledHeight = imgHeight * scale;
// Center the image on the page
const x = (pageWidth - scaledWidth) / 2;
const y = (pageHeight - scaledHeight) / 2;
pdf.addImage(imgData, 'JPEG', x, y, scaledWidth, scaledHeight);
pageCount++;
} catch (imgError) {
console.error(`Error processing image ${i + 1}:`, imgError);
continue;
}
}
if (pageCount === 0) {
throw new Error("No images were successfully processed!");
}
// Save the PDF
const filename = CONFIG.documentName + ".pdf";
pdf.save(filename);
removeMessage(loadingDiv);
showMessage(`PDF generation completed! Processed ${pageCount} images.`, 'info');
console.log("PDF generation completed!");
} catch (error) {
console.error("Error generating PDF:", error);
removeMessage(loadingDiv);
showMessage(error.message || 'Error generating PDF. Please try again.', 'error');
// Retry logic
if (retryCount < CONFIG.maxRetries) {
retryCount++;
console.log(`Retrying... Attempt ${retryCount}/${CONFIG.maxRetries}`);
setTimeout(generatePDF, CONFIG.retryDelay);
}
}
}
// Initialize the PDF generation process
async function initializePDFGeneration() {
try {
console.log("Initializing PDF generation...");
// Load jsPDF library
await loadJSPDF();
// Generate PDF
await generatePDF();
} catch (error) {
console.error("Error initializing PDF generation:", error);
showMessage('Error initializing PDF generation. Please refresh the page.', 'error');
}
}
// Start the initialization
initializePDFGeneration();