How to Send PDF or a document to printer?

I currently use external binary (.exe) to do this : PDFtoPrinter for Windows
I call it from my app with std::process::Command and supply the file's path into it.

    const PDF_TO_PRINTER: &str = "pdftoprinter";
    let output = Command::new(PDF_TO_PRINTER)
        .args([file_name, printer_target.as_str()])
        .map_err(|_| AppErr::from(AppErrEnum::PrinterError))?;

The problem is I need to make this process faster. Is there any rust native way to send a document to a printer effectively ?

The windows API solution here is confusing for me and if it was the only solution, is there any example available ?

Slow how? Are you calling this command in a loop and the time between prints is too long? Or is it always too long for even a single document?

the PDF I had is several pages long, the time needed from calling the external "pdftoprinter.exe" to actual start of printing is long around 1 second for 2 pages document (50KB in size). I hope there's another solution to call printer natively on Rust

And suppose you do a print from within a PDF reader or MS Office - how long is the delay then?

This sounds about right to me? There's a lot of overhead in talking to a printer, a second is pretty fast. IIRC, most of that time is the printer initializing

Writing windows printer code, especially from a PDF is a very tough job. Like, obscenely tough. You essentially have to write a PDF to XPS conversion, (no XPS format crates, it looks like, so a lot of manual XML nonsense, on top of dealing with translating all the PDF concepts) then do Windows COM programming to send that as a stream (a lot simpler with windows-rs, but still a lot to learn).

Seems like an interesting challenge though. I'm somewhat familiar with PDF from work, maybe I'll take a look.


Follow up, windows changed a bunch dune the last time I looked, but I got it enumerating printers and printing a basic path with a gradient brush in about 300 lines (manually, no pdf yet). Not too horrible, but that's the easy bit. In my experience, embedded fonts are where things get real rough, but from this I think I could get the basics done without too much trouble.

One thing is that it would be nice to be able to write XPS without being on Windows, so they couldn't use the windows XPS object model APIs. If I get something interesting working I'll throw it up, but expect months at least, if ever.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.