Skip to content

Commit e55f829

Browse files
committed
cleanup js script
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 6247faa commit e55f829

File tree

1 file changed

+32
-68
lines changed

1 file changed

+32
-68
lines changed

plotly_static/src/template.rs

Lines changed: 32 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -43,92 +43,62 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
4343
const scale = arguments[4];
4444
const callback = arguments[arguments.length - 1];
4545
46-
console.log('Starting PDF export process...');
47-
console.log('Plot data:', plot);
48-
console.log('Dimensions:', width, 'x', height);
49-
5046
const graph_div = document.getElementById('plotly-html-element');
5147
52-
// Check if html2pdf is available
5348
if (typeof html2pdf === 'undefined') {
54-
console.error('html2pdf library not available');
5549
callback('ERROR:html2pdf library not loaded');
5650
return;
5751
}
5852
59-
console.log('html2pdf library is available');
60-
6153
let tempDiv = null;
62-
let mmWidth, mmHeight;
6354
64-
Plotly.newPlot(graph_div, plot).then(function() {
65-
console.log('Plotly plot created successfully');
55+
const cleanup = () => {
56+
if (tempDiv) {
57+
document.body.removeChild(tempDiv);
58+
}
59+
};
6660
67-
// Force PNG format for PDF export to test if SVG is the issue
61+
Plotly.newPlot(graph_div, plot).then(function() {
6862
return Plotly.toImage(graph_div, {
69-
format: 'png',
70-
width: width,
71-
height: height,
63+
format: format,
64+
width: width,
65+
height: height,
7266
});
7367
}).then(function(dataUrl) {
74-
console.log('Plotly image generated successfully');
75-
console.log('SVG data URL length:', dataUrl.length);
76-
console.log('SVG data URL preview:', dataUrl.substring(0, 200) + '...');
77-
78-
// Convert px to mm: 1px = 0.264583 mm
79-
mmWidth = width * 0.264583;
80-
mmHeight = height * 0.264583;
81-
82-
console.log('PDF dimensions (mm):', mmWidth, 'x', mmHeight);
83-
84-
// Create a temporary div for the image
8568
tempDiv = document.createElement('div');
8669
tempDiv.style.width = width + 'px';
8770
tempDiv.style.height = height + 'px';
8871
tempDiv.style.background = 'white';
8972
tempDiv.style.position = 'fixed';
90-
tempDiv.style.left = '0px';
91-
tempDiv.style.top = '0px';
92-
tempDiv.style.overflow = 'hidden';
73+
tempDiv.style.top = '0';
74+
tempDiv.style.left = '0';
75+
tempDiv.style.margin = '0';
76+
tempDiv.style.padding = '0';
9377
tempDiv.style.boxSizing = 'border-box';
94-
tempDiv.style.zIndex = '9999';
95-
tempDiv.style.display = 'block';
9678
document.body.appendChild(tempDiv);
9779
98-
// Use simple img approach with SVG data URL
99-
console.log('Using SVG data URL directly with img element');
10080
const img = document.createElement('img');
10181
img.src = dataUrl;
82+
img.style.display = 'block';
83+
img.style.margin = '0';
84+
img.style.padding = '0';
10285
img.style.width = '100%';
10386
img.style.height = '100%';
104-
img.style.display = 'block';
105-
img.style.objectFit = 'contain';
106-
img.style.maxWidth = '100%';
107-
img.style.maxHeight = '100%';
87+
img.style.verticalAlign = 'top';
88+
img.style.boxSizing = 'border-box';
10889
tempDiv.appendChild(img);
10990
110-
// Wait for the image to load
11191
return new Promise(function(resolve) {
11292
img.onload = function() {
113-
console.log('SVG image loaded successfully');
114-
// Additional delay to ensure image is fully rendered
115-
setTimeout(function() {
116-
console.log('SVG rendering delay completed');
117-
resolve();
118-
}, 500);
93+
// Brief delay to ensure image is fully rendered
94+
setTimeout(resolve, 150);
11995
};
12096
img.onerror = function() {
121-
console.error('Failed to load SVG image');
122-
if (tempDiv) {
123-
document.body.removeChild(tempDiv);
124-
}
125-
callback('ERROR:Failed to load SVG image');
97+
cleanup();
98+
callback('ERROR:Failed to load image');
12699
};
127100
});
128101
}).then(function() {
129-
console.log('Starting PDF generation...');
130-
131-
// Generate PDF with more robust configuration
132102
return html2pdf().from(tempDiv).set({
133103
margin: 0,
134104
filename: 'plotly-plot.pdf',
@@ -143,28 +113,22 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
143113
height: height,
144114
imageTimeout: 15000,
145115
removeContainer: true,
146-
foreignObjectRendering: true
116+
foreignObjectRendering: false,
117+
scrollY: 0,
118+
scrollX: 0
147119
},
148120
jsPDF: {
149-
unit: 'mm',
150-
format: [mmWidth, mmHeight],
151-
orientation: mmWidth > mmHeight ? 'landscape' : 'portrait',
121+
unit: 'px',
122+
format: [width, height],
123+
orientation: width > height ? 'landscape' : 'portrait',
152124
compress: true
153125
}
154126
}).toPdf().output('datauristring');
155127
}).then(function(dataUri) {
156-
console.log('PDF generation completed successfully');
157-
// Clean up
158-
if (tempDiv) {
159-
document.body.removeChild(tempDiv);
160-
}
161-
callback(dataUri);
162-
}).catch(function(err) {
163-
console.error('PDF generation error:', err);
164-
// Clean up if tempDiv exists
165-
if (tempDiv) {
166-
document.body.removeChild(tempDiv);
167-
}
128+
cleanup();
129+
callback(dataUri);
130+
}).catch(function(err) {
131+
cleanup();
168132
callback('ERROR:' + err.toString());
169133
});
170134
"##;

0 commit comments

Comments
 (0)