@@ -17,15 +17,16 @@ pub(crate) const IMAGE_EXPORT_JS_SCRIPT: &str = r#"
1717 const format = arguments[1];
1818 const width = arguments[2];
1919 const height = arguments[3];
20+ const scale = arguments[4];
2021 const callback = arguments[arguments.length - 1];
2122 console.log(plot);
2223 const graph_div = document.getElementById("plotly-html-element");
23- Plotly.newPlot(graph_div, plot)
24- const img_element = document.getElementById("plotly-img-element");
25- Plotly.toImage(graph_div, {
24+ Plotly.newPlot(graph_div, plot).then(function() {
25+ return Plotly.toImage(graph_div, {
2626 format: format,
2727 width: width,
2828 height: height,
29+ });
2930 }).then(function(dataUrl) {
3031 callback(dataUrl);
3132 }).catch(function(err) {
@@ -39,6 +40,7 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
3940 const format = arguments[1];
4041 const width = arguments[2];
4142 const height = arguments[3];
43+ const scale = arguments[4];
4244 const callback = arguments[arguments.length - 1];
4345
4446 console.log('Starting PDF export process...');
@@ -62,13 +64,16 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
6264 Plotly.newPlot(graph_div, plot).then(function() {
6365 console.log('Plotly plot created successfully');
6466
67+ // Force PNG format for PDF export to test if SVG is the issue
6568 return Plotly.toImage(graph_div, {
66- format: format ,
69+ format: 'png' ,
6770 width: width,
6871 height: height,
6972 });
7073 }).then(function(dataUrl) {
7174 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) + '...');
7277
7378 // Convert px to mm: 1px = 0.264583 mm
7479 mmWidth = width * 0.264583;
@@ -81,33 +86,43 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
8186 tempDiv.style.width = width + 'px';
8287 tempDiv.style.height = height + 'px';
8388 tempDiv.style.background = 'white';
84- tempDiv.style.position = 'absolute';
85- tempDiv.style.left = '-9999px';
86- tempDiv.style.top = '-9999px';
89+ tempDiv.style.position = 'fixed';
90+ tempDiv.style.left = '0px';
91+ tempDiv.style.top = '0px';
92+ tempDiv.style.overflow = 'hidden';
93+ tempDiv.style.boxSizing = 'border-box';
94+ tempDiv.style.zIndex = '9999';
95+ tempDiv.style.display = 'block';
8796 document.body.appendChild(tempDiv);
8897
89- // Create and set the image
98+ // Use simple img approach with SVG data URL
99+ console.log('Using SVG data URL directly with img element');
90100 const img = document.createElement('img');
91101 img.src = dataUrl;
92102 img.style.width = '100%';
93103 img.style.height = '100%';
94104 img.style.display = 'block';
105+ img.style.objectFit = 'contain';
106+ img.style.maxWidth = '100%';
107+ img.style.maxHeight = '100%';
95108 tempDiv.appendChild(img);
96109
97- console.log('Temporary div created with image');
98-
99- // Wait a bit for the image to load
110+ // Wait for the image to load
100111 return new Promise(function(resolve) {
101112 img.onload = function() {
102- console.log('Image loaded successfully');
103- resolve();
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);
104119 };
105120 img.onerror = function() {
106- console.error('Image failed to load');
121+ console.error('Failed to load SVG image ');
107122 if (tempDiv) {
108123 document.body.removeChild(tempDiv);
109124 }
110- callback('ERROR:Image failed to load');
125+ callback('ERROR:Failed to load SVG image ');
111126 };
112127 });
113128 }).then(function() {
@@ -117,13 +132,18 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
117132 return html2pdf().from(tempDiv).set({
118133 margin: 0,
119134 filename: 'plotly-plot.pdf',
120- image: { type: 'jpeg', quality: 1 },
135+ image: { type: 'jpeg', quality: 1},
121136 html2canvas: {
122- scale: 1 ,
137+ scale: scale ,
123138 backgroundColor: '#fff',
124139 useCORS: true,
125140 allowTaint: true,
126- logging: true
141+ logging: true,
142+ width: width,
143+ height: height,
144+ imageTimeout: 15000,
145+ removeContainer: true,
146+ foreignObjectRendering: true
127147 },
128148 jsPDF: {
129149 unit: 'mm',
@@ -163,17 +183,18 @@ pub(crate) fn html_body(js_source: &str) -> String {
163183 // HTML with embedded script
164184 let html = format ! (
165185 r#"
186+ <!doctype html>
166187 <html lang="en">
167188 <head>
168- <style>
169- /* Ensures the image has no extra spacing */
170- #plotly-img-element {{
171- display: block;
172- margin: 0;
173- padding: 0;
174- background: white;
175- }}
176- </style>
189+ <style>
190+ /* Ensures the image has no extra spacing */
191+ #plotly-img-element {{
192+ display: block;
193+ margin: 0;
194+ padding: 0;
195+ background: white;
196+ }}
197+ </style>
177198 {js_source}
178199 </head>
179200 <body>
0 commit comments