How do I print an image to printer using rust?
โ Rust ๐ 2026-03-25 ๐ค surdeus ๐๏ธ 2I am using the printer crate: GitHub - talesluna/rust-printers: A rust library to printing APIs implementation for unix (cups) and windows (winspool). ยท GitHub
I have this code:
for printer in all_printers.iter()
{
// Prints qrcode to printer after finding the right printer in list
if printer.name.eq(r"\\DC01\Downstairs printer")
{
println!("printer: {}", printer.name);
println!("Found printer, printing file");
//println!("tmp_filepath: {}", &tmp_filepath.display().to_string());
//printer.print_file(&tmp_filepath.display().to_string(), PrinterJobOptions::none()).unwrap();
printer.print_file(&tmp_filepath.display().to_string(), PrinterJobOptions
{
name: Some("My print job"),
raw_properties: &[("copies", "1"),("document-format", "RAW")],
converter: Converter::Ghostscript(GhostscriptConverterOptions::pngmono()),
}).unwrap();
break;
}
}
However I get this error:
thread 'main' (11600) panicked at src\main.rs:141:16:
called `Result::unwrap()` on an `Err` value: PrintersError { message: "Ghostscript exit with code 1", failure: ConverterFailure, backtrace: <disabled> }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
if I opened the png file from windows photo app and print it to the printer there are no issues, however if I print using this code then it won't print anymore and if I used this code:
for printer in all_printers.iter()
{
if printer.name.eq(r"\\DC01\Downstairs printer")
{
println!("printer: {}", printer.name);
println!("Found printer, printing file");
//println!("tmp_filepath: {}", &tmp_filepath.display().to_string());
printer.print_file(&tmp_filepath.display().to_string(), PrinterJobOptions::none()).unwrap();
break;
}
}
it will print out binary garbage on the paper. Do I need to convert it into xps, pdf etc and then I can print it to the printer? And if so how do I convert it using ghostscript? Are there alternative printer crates out there that can print an image directly?
1 post - 1 participant
๐ท๏ธ Rust_feed