Skip to content

Commit da84bc7

Browse files
committed
the pdf export works again, but there is something wrong with the
cropping Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 24ea40a commit da84bc7

File tree

3 files changed

+82
-39
lines changed

3 files changed

+82
-39
lines changed

plotly/src/plot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,8 @@ mod tests {
10621062
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
10631063
let file_size = metadata.len();
10641064
assert!(file_size > 0,);
1065-
assert!(std::fs::remove_file(&dst).is_ok());
1066-
assert!(!dst.exists());
1065+
// assert!(std::fs::remove_file(&dst).is_ok());
1066+
// assert!(!dst.exists());
10671067
}
10681068

10691069
#[test]

plotly_static/src/lib.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@ impl StaticExporter {
10071007
ImageFormat::SVG.to_string().into(),
10081008
plot.width.into(),
10091009
plot.height.into(),
1010+
plot.scale.into(),
10101011
];
10111012

10121013
(PDF_EXPORT_JS_SCRIPT, args)
@@ -1017,6 +1018,7 @@ impl StaticExporter {
10171018
plot.format.to_string().into(),
10181019
plot.width.into(),
10191020
plot.height.into(),
1021+
plot.scale.into(),
10201022
];
10211023

10221024
(IMAGE_EXPORT_JS_SCRIPT, args)
@@ -1186,14 +1188,34 @@ mod tests {
11861188
]
11871189
}
11881190
],
1189-
"layout": {},
1190-
"config": {},
1191+
"layout": {
1192+
"autosize": false,
1193+
"width": 1200,
1194+
"height": 900,
1195+
"scene": {
1196+
"domain": {
1197+
"x": [0.15, 0.95],
1198+
"y": [0.15, 0.95]
1199+
},
1200+
"aspectmode": "data",
1201+
"aspectratio": {
1202+
"x": 1,
1203+
"y": 1,
1204+
"z": 1
1205+
},
1206+
"camera": {
1207+
"eye": {"x": 1.5, "y": 1.5, "z": 1.5}
1208+
}
1209+
},
1210+
"config": {
1211+
"responsive": false
1212+
},
1213+
},
11911214
}))
11921215
.unwrap()
11931216
}
11941217

11951218
#[test]
1196-
// #[ignore]
11971219
fn save_png() {
11981220
init();
11991221
let test_plot = create_test_plot();
@@ -1211,7 +1233,7 @@ mod tests {
12111233
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
12121234
let file_size = metadata.len();
12131235
assert!(file_size > 0,);
1214-
assert!(std::fs::remove_file(dst.as_path()).is_ok());
1236+
// assert!(std::fs::remove_file(dst.as_path()).is_ok());
12151237
}
12161238

12171239
#[test]
@@ -1231,7 +1253,7 @@ mod tests {
12311253
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
12321254
let file_size = metadata.len();
12331255
assert!(file_size > 0,);
1234-
assert!(std::fs::remove_file(dst.as_path()).is_ok());
1256+
// assert!(std::fs::remove_file(dst.as_path()).is_ok());
12351257
}
12361258

12371259
#[test]
@@ -1252,7 +1274,7 @@ mod tests {
12521274
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
12531275
let file_size = metadata.len();
12541276
assert!(file_size > 0,);
1255-
assert!(std::fs::remove_file(dst.as_path()).is_ok());
1277+
// assert!(std::fs::remove_file(dst.as_path()).is_ok());
12561278

12571279
let dst = PathBuf::from("example2.jpeg");
12581280
export
@@ -1262,7 +1284,7 @@ mod tests {
12621284
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
12631285
let file_size = metadata.len();
12641286
assert!(file_size > 0,);
1265-
assert!(std::fs::remove_file(dst.as_path()).is_ok());
1287+
// assert!(std::fs::remove_file(dst.as_path()).is_ok());
12661288
}
12671289

12681290
#[test]
@@ -1282,7 +1304,7 @@ mod tests {
12821304
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
12831305
let file_size = metadata.len();
12841306
assert!(file_size > 0,);
1285-
assert!(std::fs::remove_file(dst.as_path()).is_ok());
1307+
// assert!(std::fs::remove_file(dst.as_path()).is_ok());
12861308
}
12871309

12881310
#[test]
@@ -1302,7 +1324,7 @@ mod tests {
13021324
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
13031325
let file_size = metadata.len();
13041326
assert!(file_size > 0,);
1305-
assert!(std::fs::remove_file(dst.as_path()).is_ok());
1327+
// assert!(std::fs::remove_file(dst.as_path()).is_ok());
13061328
}
13071329

13081330
#[test]
@@ -1322,7 +1344,7 @@ mod tests {
13221344
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
13231345
let file_size = metadata.len();
13241346
assert!(file_size > 0,);
1325-
assert!(std::fs::remove_file(dst.as_path()).is_ok());
1347+
// assert!(std::fs::remove_file(dst.as_path()).is_ok());
13261348
}
13271349

13281350
#[test]

plotly_static/src/template.rs

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)