Skip to content

Commit 7ec603e

Browse files
committed
wip
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 9fbe2b1 commit 7ec603e

File tree

6 files changed

+37
-227
lines changed

6 files changed

+37
-227
lines changed

plotly_static/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ fn main() -> Result<()> {
325325
} else {
326326
#[cfg(feature = "chromedriver")]
327327
{
328-
let msg = "'webdriver_download' feature disabled. Please install a '{CHROMEDRIVER_NAME}' version manually and make the environment variable 'WEBDRIVER_PATH' point to it.".to_string();
328+
let msg = format!("'webdriver_download' feature disabled. Please install a '{CHROMEDRIVER_NAME}' version manually and make the environment variable 'WEBDRIVER_PATH' point to it.");
329329
println!("cargo::warning={msg}");
330330
}
331331
#[cfg(feature = "geckodriver")]

plotly_static/examples/main.rs

Lines changed: 0 additions & 135 deletions
This file was deleted.

plotly_static/examples/test_chrome_binary.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

plotly_static/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ impl StaticExporter {
958958
}
959959

960960
fn static_export(&mut self, plot: &PlotData<'_>) -> Result<String> {
961-
let data_uri = template::html_body(self.offline_mode);
961+
let data_uri = template::get_html_body(self.offline_mode);
962962
let runtime = self.runtime.clone();
963963
runtime
964964
.block_on(self.extract(&data_uri, plot))

plotly_static/src/template.rs

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,27 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
5353
callback('ERROR:html2pdf library not loaded');
5454
return;
5555
}
56-
56+
5757
console.log('html2pdf library is available');
5858
5959
let tempDiv = null;
6060
let mmWidth, mmHeight;
6161
6262
Plotly.newPlot(graph_div, plot).then(function() {
6363
console.log('Plotly plot created successfully');
64-
64+
6565
return Plotly.toImage(graph_div, {
6666
format: format,
6767
width: width,
6868
height: height,
6969
});
7070
}).then(function(dataUrl) {
7171
console.log('Plotly image generated successfully');
72-
72+
7373
// Convert px to mm: 1px = 0.264583 mm
7474
mmWidth = width * 0.264583;
7575
mmHeight = height * 0.264583;
76-
76+
7777
console.log('PDF dimensions (mm):', mmWidth, 'x', mmHeight);
7878
7979
// Create a temporary div for the image
@@ -112,22 +112,22 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
112112
});
113113
}).then(function() {
114114
console.log('Starting PDF generation...');
115-
115+
116116
// Generate PDF with more robust configuration
117117
return html2pdf().from(tempDiv).set({
118118
margin: 0,
119119
filename: 'plotly-plot.pdf',
120120
image: { type: 'jpeg', quality: 1 },
121-
html2canvas: {
122-
scale: 1,
123-
backgroundColor: '#fff',
121+
html2canvas: {
122+
scale: 1,
123+
backgroundColor: '#fff',
124124
useCORS: true,
125125
allowTaint: true,
126126
logging: true
127127
},
128-
jsPDF: {
129-
unit: 'mm',
130-
format: [mmWidth, mmHeight],
128+
jsPDF: {
129+
unit: 'mm',
130+
format: [mmWidth, mmHeight],
131131
orientation: mmWidth > mmHeight ? 'landscape' : 'portrait',
132132
compress: true
133133
}
@@ -149,16 +149,20 @@ pub(crate) const PDF_EXPORT_JS_SCRIPT: &str = r##"
149149
});
150150
"##;
151151

152-
pub(crate) fn html_body(offline: bool) -> String {
152+
pub(crate) fn get_html_body(offline: bool) -> String {
153153
let offline_js = offline_js_sources();
154154
let cdn_js = online_js_cdn();
155+
if offline {
156+
html_body(&offline_js)
157+
} else {
158+
html_body(&cdn_js)
159+
}
160+
}
155161

162+
pub(crate) fn html_body(js_source: &str) -> String {
156163
// HTML with embedded script
157-
let html = if offline {
158-
format!(
159-
r#"
160-
<!doctype html>
161-
164+
let html = format!(
165+
r#"
162166
<html lang="en">
163167
<style>
164168
/* Ensures the image has no extra spacing */
@@ -167,41 +171,16 @@ pub(crate) fn html_body(offline: bool) -> String {
167171
margin: 0;
168172
padding: 0;
169173
background: white;
170-
}}
174+
}}
171175
</style>
172-
<head>
173-
{offline_js}
174-
</head>
175-
<body>
176-
<div id="plotly-html-element" hidden></div>
177-
</body>
176+
<head>
177+
{js_source}
178+
</head>
179+
<body>
180+
<div id="plotly-html-element" hidden></div>
181+
</body>
178182
</html>"#
179-
)
180-
} else {
181-
format!(
182-
r#"
183-
<!doctype html>
184-
<html lang="en">
185-
<style>
186-
/* Ensures the image has no extra spacing */
187-
#plotly-img-element {{
188-
display: block;
189-
margin: 0;
190-
padding: 0;
191-
background: white;
192-
}}
193-
</style>
194-
<head>
195-
{cdn_js}
196-
</head>
197-
<body>
198-
<div>
199-
</div>
200-
<div id="plotly-html-element" hidden></div>
201-
</body>
202-
</html>"#
203-
)
204-
};
183+
);
205184

206185
#[cfg(not(test))]
207186
if log_enabled!(Debug) {
@@ -216,7 +195,6 @@ pub(crate) fn html_body(offline: bool) -> String {
216195
/// Save the html file to a temporary file
217196
#[allow(unused)]
218197
pub(crate) fn to_file(data: &str) -> Result<PathBuf> {
219-
debug!("Generate plotly html file");
220198
use std::env;
221199
// Set up the temp file with a unique filename.
222200
let mut tmp_path = env::temp_dir();
@@ -225,6 +203,8 @@ pub(crate) fn to_file(data: &str) -> Result<PathBuf> {
225203
plot_name = format!("plotly_{plot_name}");
226204
tmp_path.push(plot_name);
227205

206+
debug!("Write template plotly html file to {tmp_path:?}");
207+
228208
// Save the rendered plot to the temp file.
229209
let temp_path = tmp_path
230210
.to_str()

0 commit comments

Comments
 (0)