chromiumoxide
chromiumoxide copied to clipboard
Create pdf from static html content instead of URL
Hi Team,
Need to generate pdf
file from the static HTML
content instead of URL
.
You can use Page::set_content("<html_str>")
for this.
Here is a full example using tokio
:
use futures::StreamExt;
use chromiumoxide::{
browser::{Browser, BrowserConfig},
cdp::browser_protocol::page::PrintToPdfParams,
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (browser, mut handler) = Browser::launch(BrowserConfig::builder().build()?).await?;
async_std::task::spawn(async move {
loop {
let _ = handler.next().await.unwrap();
}
});
let page = browser.new_page("about:blank").await?;
page.set_content(r"
<body>
<h1>This was set via chromiumoxide</h1>
</body>
").await?;
page.save_pdf(PrintToPdfParams::default(), "mypdf.pdf").await?;
Ok(())
}
Nice, the set_content is a lot faster than using a data url !