Module actix

Module actix 

Source
Expand description

Actix-web framework integration.

This module provides helpers for using BrowserPool with Actix-web.

§Setup

Add to your Cargo.toml:

[dependencies]
html2pdf-api = { version = "0.1", features = ["actix-integration"] }
actix-web = "4"

§Basic Usage

use actix_web::{web, App, HttpServer, HttpResponse, Responder};
use html2pdf_api::prelude::*;
use std::sync::Arc;

async fn generate_pdf(
    pool: web::Data<SharedBrowserPool>,
) -> impl Responder {
    let pool_guard = pool.lock().unwrap();
    let browser = match pool_guard.get() {
        Ok(b) => b,
        Err(e) => return HttpResponse::InternalServerError().body(e.to_string()),
    };

    let tab = browser.new_tab().unwrap();
    tab.navigate_to("https://example.com").unwrap();
     
    // Generate PDF...
    let pdf_data = tab.print_to_pdf(None).unwrap();

    HttpResponse::Ok()
        .content_type("application/pdf")
        .body(pdf_data)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // Create and warmup pool
    let pool = BrowserPool::builder()
        .factory(Box::new(ChromeBrowserFactory::with_defaults()))
        .build()
        .expect("Failed to create pool");

    pool.warmup().await.expect("Failed to warmup");

    // Convert to shared state
    let shared_pool = pool.into_shared();

    HttpServer::new(move || {
        App::new()
            .app_data(web::Data::new(Arc::clone(&shared_pool)))
            .route("/pdf", web::get().to(generate_pdf))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

§Using with init_browser_pool

If you have the env-config feature enabled:

use actix_web::{web, App, HttpServer};
use html2pdf_api::init_browser_pool;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let pool = init_browser_pool().await
        .expect("Failed to initialize browser pool");

    HttpServer::new(move || {
        App::new()
            .app_data(web::Data::new(Arc::clone(&pool)))
            .configure(configure_routes)
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

§Graceful Shutdown

For proper cleanup, shutdown the pool when the server stops:

use actix_web::{web, App, HttpServer};
use html2pdf_api::prelude::*;
use std::sync::Arc;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let pool = BrowserPool::builder()
        .factory(Box::new(ChromeBrowserFactory::with_defaults()))
        .build()
        .expect("Failed to create pool");

    pool.warmup().await.expect("Failed to warmup");

    let shared_pool = Arc::new(std::sync::Mutex::new(pool));
    let shutdown_pool = Arc::clone(&shared_pool);

    let server = HttpServer::new(move || {
        App::new()
            .app_data(web::Data::new(Arc::clone(&shared_pool)))
    })
    .bind("127.0.0.1:8080")?
    .run();

    let server_handle = server.handle();

    // Run server
    let result = server.await;

    // Cleanup pool after server stops
    if let Ok(mut pool) = shutdown_pool.lock() {
        pool.shutdown_async().await;
    }

    result
}

Traits§

BrowserPoolActixExt
Extension trait for BrowserPool with Actix-web helpers.

Functions§

create_pool_data
Create Actix-web Data from an existing shared pool.
create_pool_data_from_arc
Create Actix-web Data from an Arc reference.

Type Aliases§

BrowserPoolData
Type alias for Actix-web Data wrapper around the shared pool.