Export to pdf mariadb with signatures rust

⚓ Rust    📅 2025-09-02    👤 surdeus    👁️ 3      

surdeus

type or paste code here



```toml
[dependencies]
mysql = "20.0"
printpdf = "0.5"
image = "0.23" # For image handling

use mysql::;
use mysql::prelude::
;

fn establish_connection() -> PooledConn {
let url = "mysql://username:password@localhost:3306/database_name";
let pool = Pool::new(url).expect("Failed to create a database pool");
pool.get_conn().expect("Failed to get a connection")
}

fn fetch_data(conn: &mut PooledConn) -> Vec<(String, String)> {
conn.query_map(
"SELECT name, details FROM your_table",
|(name, details)| (name, details),
).expect("Failed to fetch data")
}


###  

use printpdf::*;
use std::fs::File;
use std::io::BufWriter;
use image::GenericImageView;

fn create_pdf(data: Vec<(String, String)>, logo_path: &str, signature_path: &str) {
    let (document, page1, layer1) = PdfDocument::new("Exported PDF", 210.0, 297.0, "Layer 1");

    let logo_image = image::open(logo_path).expect("Failed to load logo");
    let image = Image::from_image(&logo_image);

    let signature_image = image::open(signature_path).expect("Failed to load signature");
    let signature = Image::from_image(&signature_image);

    let mut current_y = 250.0; // Starting Y position

    // Add logo
    {
        let transform = Transform::translate(20.0, current_y);
        layer1.add_image(image.clone(), transform, Some(50.0), None);
        current_y -= 60.0; // Move down for next content
    }

    // Add text entries from MariaDB
    for (name, details) in data {
        layer1.add_text(
            &format!("Name: {}", name),
            &Font::from_file("path/to/your/font.ttf").unwrap(),
            12.0,
            Pt(12.0),
            Point {
                x: 20.0,
                y: current_y,
            },
            None,
        );
        current_y -= 20.0; // Spacing between entries
        layer1.add_text(
            &format!("Details: {}", details),
            &Font::from_file("path/to/your/font.ttf").unwrap(),
            12.0,
            Pt(12.0),
            Point {
                x: 20.0,
                y: current_y,
            },
            None,
        );
        current_y -= 40.0; // More spacing for next record
    }

    // Add signature
    {
        let transform = Transform::translate(20.0, current_y);
        layer1.add_image(signature.clone(), transform, Some(100.0), None);
    }

    // Save the PDF
    let file = File::create("output.pdf").expect("Failed to create PDF file");
    let mut buf_writer = BufWriter::new(file);
    document.save(&mut buf_writer).expect("Failed to save PDF");
}

fn main() {
let mut conn = establish_connection();
let data = fetch_data(&mut conn);

let logo_path = "path/to/logo.png"; // Path to your logo image
let signature_path = "path/to/signature.png"; // Path to your signature image

create_pdf(data, logo_path, signature_path);

}


### 

1 post - 1 participant

Read full topic

🏷️ Rust_feed