Skip to content

Commit 1c4b787

Browse files
committed
block in place
1 parent e5b0e09 commit 1c4b787

3 files changed

Lines changed: 27 additions & 32 deletions

File tree

src/template/render.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ async fn html_to_pdf<T: Serialize>(templ: &[u8], templ_ctx: &T) -> Result<Vec<u8
101101

102102
tracing::debug!("{}", String::from_utf8_lossy(templ));
103103
let html = engine.render_str(std::str::from_utf8(templ)?, templ_ctx)?;
104-
let tab = get_chromium_tab()?;
105104

106105
let temp_html_file_path = dirs::home_dir()
107106
.unwrap_or_else(std::env::temp_dir)
@@ -113,10 +112,16 @@ async fn html_to_pdf<T: Serialize>(templ: &[u8], templ_ctx: &T) -> Result<Vec<u8
113112
tracing::debug!("{page}");
114113

115114
tracing::info!("generate pdf from html page {page}");
116-
let pdf = tab
117-
.navigate_to(&page)?
118-
.wait_until_navigated()?
119-
.print_to_pdf(Default::default())?;
115+
let pdf = tokio::task::block_in_place(|| {
116+
let tab = get_chromium_tab().map_err(ServiceError::new)?;
117+
118+
tab.navigate_to(&page)
119+
.map_err(ServiceError::new)?
120+
.wait_until_navigated()
121+
.map_err(ServiceError::new)?
122+
.print_to_pdf(Default::default())
123+
.map_err(ServiceError::new)
124+
})?;
120125
tokio::fs::remove_file(temp_html_file_path).await?;
121126

122127
Ok(pdf)

src/upload/service.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
use async_zip::{Compression, ZipEntryBuilder, base::write::ZipFileWriter};
88
use axum::extract::multipart::Field;
99
use futures::TryStreamExt;
10-
use headless_chrome::protocol::cdp::Page::{self, Viewport};
10+
use headless_chrome::protocol::cdp::Page::{self};
1111
use image::{EncodableLayout, ImageFormat};
1212
use mime_guess::mime::IMAGE_PNG;
1313
use mongodb::bson::{Document, doc};
@@ -81,40 +81,24 @@ impl FileService {
8181
temp_file_path: &PathBuf,
8282
) -> Result<Option<String>, ServiceError> {
8383
let (extension, thumb) = {
84-
async fn chrome_proc(temp_file_path: &Path) -> Result<Vec<u8>, ServiceError> {
84+
fn chrome_proc(temp_file_path: &Path) -> Result<Vec<u8>, ServiceError> {
8585
// first try with chromium as it seems faster
8686
let tab = get_chromium_tab().map_err(ServiceError::new)?;
8787
let file_url = format!("file://{}", temp_file_path.display());
8888
tab.navigate_to(&file_url).map_err(ServiceError::new)?;
8989
tab.wait_until_navigated().map_err(ServiceError::new)?;
9090

91-
let first_page_height: f64 = tab
92-
.evaluate("document.querySelector('body').scrollHeight", false)
93-
.map_err(|e| ServiceError::new(e.to_string()))?
94-
.value
95-
.ok_or_else(|| ServiceError::new("missing height viewport"))?
96-
.as_f64()
97-
.ok_or_else(|| ServiceError::new("could not parse height viewport"))?;
98-
let first_page_width: f64 = tab
99-
.evaluate("document.querySelector('body').scrollWidth", false)
100-
.map_err(|e| ServiceError::new(e.to_string()))?
101-
.value
102-
.ok_or_else(|| ServiceError::new("missing width viewport"))?
103-
.as_f64()
104-
.ok_or_else(|| ServiceError::new("could not parse width viewport"))?;
105-
106-
// Capture screenshot with the specific viewport
91+
let viewport = tab
92+
.wait_for_element("body")
93+
.map_err(ServiceError::new)?
94+
.get_box_model()
95+
.map_err(ServiceError::new)?
96+
.margin_viewport(); // Capture screenshot with the specific viewport
10797
let png_data = tab
10898
.capture_screenshot(
10999
Page::CaptureScreenshotFormatOption::Png,
110100
None,
111-
Some(Page::Viewport {
112-
x: 0.,
113-
y: 0.,
114-
width: first_page_width,
115-
height: first_page_height,
116-
scale: 1.0,
117-
}),
101+
Some(viewport),
118102
true,
119103
)
120104
.map_err(ServiceError::new)?;
@@ -131,7 +115,7 @@ impl FileService {
131115
.map_err(ServiceError::new)
132116
.map(|im| (upl.content_type.clone(), im))
133117
} else {
134-
match chrome_proc(temp_file_path).await.map(|bytes| {
118+
match tokio::task::block_in_place(|| chrome_proc(temp_file_path)).map(|bytes| {
135119
image::load_from_memory(&bytes)
136120
.map_err(ServiceError::new)
137121
.map(|im| (Some(IMAGE_PNG.to_string()), im))

src/upload/soffice.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub async fn convert_to(
2222
input_path_str,
2323
])
2424
.stdout(Stdio::piped())
25+
.stderr(Stdio::piped())
2526
.output()
2627
.await?;
2728
if output.status.success() {
@@ -39,7 +40,12 @@ pub async fn convert_to(
3940
});
4041
Ok(bytes)
4142
} else {
42-
Err(format!("error {}", String::from_utf8_lossy(&output.stdout)).into())
43+
Err(format!(
44+
"error! stdout: {}\nstderr: {}",
45+
String::from_utf8_lossy(&output.stdout),
46+
String::from_utf8_lossy(&output.stderr),
47+
)
48+
.into())
4349
}
4450
}
4551

0 commit comments

Comments
 (0)