I'm creating a new browser instance in multiple worker threads to print some HTML string into PDF (headless). Each instance is build with spawn() method, yet, I cannot close the browser instances. Each run, creates the following log:
Browser was not closed manually, it will be killed automatically in the background
The full helper function to coordinate the process is shown below.
pub async fn save_pdf(input_html: String, paper_size: &PaperSize, paper_orientation: &PaperOrientation, output_pdf_path: &impl AsRef<Path>) -> Result<Vec<u8>, Error> {
let browser_config = BrowserConfig::builder().build()?
};
let (mut browser, mut handler) = Browser::launch(browser_config).await?
// spawn a new task that continuously polls the handler
let handle = tokio::task::spawn(async move {
while let Some(h) = handler.next().await {
if h.is_err() {
break;
}
}
});
// Open the HTML file in the browser
let page = browser.new_page("about:blank").await?;
// Setup PDF rendering params
let (width, height) = paper_size.value();
let pdf_params = PrintToPdfParams {
display_header_footer: false.into(),
print_background: false.into(),
paper_width: width.into(),
paper_height: height.into(),
landscape: paper_orientation.chromium_value().into(),
scale: Some(1.0),
..Default::default()
};
// Save the PDF to disk and cleanup
let pdf_params = pdf_params.clone();
let pdf_bytes = page.save_pdf(pdf_params, output_pdf_path).await?;
// close the browser instance
browser.close().await?
Ok(pdf_bytes)
}
Hi,
I'm creating a new browser instance in multiple worker threads to print some HTML string into PDF (headless). Each instance is build with spawn() method, yet, I cannot close the browser instances. Each run, creates the following log:
Browser was not closed manually, it will be killed automatically in the background
The full helper function to coordinate the process is shown below.