#screenshot #html #cdp #html2image #html-to-image

cdp-html-shot

A Rust library for capturing HTML screenshots using CDP

24 releases

0.1.23 Nov 20, 2024
0.1.22 Nov 20, 2024
0.1.7 Oct 31, 2024

#136 in Images

Download history 999/week @ 2024-10-28 366/week @ 2024-11-04 251/week @ 2024-11-11 808/week @ 2024-11-18 37/week @ 2024-11-25 7/week @ 2024-12-02 8/week @ 2024-12-09

873 downloads per month
Used in 2 crates

MIT/Apache

56KB
1K SLoC

cdp-html-shot

github crates.io docs.rs

A Rust library for capturing HTML screenshots using CDP.

  • Auto cleanup
  • Asynchronous API (Tokio)
  • HTML screenshot captures

Usage

[dependencies]
cdp-html-shot = "0.1"

Example

Capture HTML screenshot

use base64::Engine;
use anyhow::Result;
use cdp_html_shot::Browser;

#[tokio::main]
async fn main() -> Result<()> {
    const HTML: &str = r#"
        <html lang="en-US">
        <body>
        <h1>My test page</h1>
        <p>Hello, Rust!</p>
        </body>
        </html>
    "#;
    
    let browser = Browser::new().await?;
    let base64 = browser.capture_html(HTML, "html").await?;

    let img_data = base64::prelude::BASE64_STANDARD.decode(base64)?;
    std::fs::write("test0.jpeg", img_data)?;

    Ok(())
}

Fine control

use base64::Engine;
use anyhow::Result;
use cdp_html_shot::Browser;

#[tokio::main]
async fn main() -> Result<()> {
    let browser = Browser::new().await?;
    let tab = browser.new_tab().await?;

    tab.set_content("<h1>Hello world!</h1>").await?;

    let element = tab.find_element("h1").await?;
    let base64 = element.screenshot().await?;
    tab.close().await?;

    let img_data = base64::prelude::BASE64_STANDARD.decode(base64)?;
    std::fs::write("test0.jpeg", img_data)?;

    Ok(())
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~10–22MB
~339K SLoC