如何在本机 Rust 中将 PDF 或文档发送到打印机?

How to Send PDF or a document to printer in native Rust?

我目前使用外部二进制文件 (.exe) 执行此操作:http://www.columbia.edu/~em36/pdftoprinter.html 我使用 std::process::Command 从我的应用程序中调用它,并将文件的路径提供给它。

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

问题是我需要加快这个过程。有什么生锈的本机方法可以有效地将文档发送到打印机吗?

The windows API solution here 让我感到困惑,如果这是唯一的解决方案,是否有可用的示例?

编写 windows 打印机代码,尤其是 PDF 打印机代码,结果证明是一项非常艰巨的工作。就像,非常强硬。

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 package, but still a lot to learn).

我在 Rust-forum 上问过这个问题,截至今天,还没有处理这个问题的原生 Rust 包。所以我就此打住。

我目前使用的解决方案是调用其他可执行文件来执行此操作。 到目前为止,我尝试过的最快的是 SumatraPDF。下面是我的实现:

   use std::process::Command;


   let file_name = "pdf_file_name.pdf";
   let printer_target = "your_printer_name";
   let output = Command::new("/path/to/sumatra.exe")
        .args([
            "-print-to",
            printer_target,
            "-print-settings",
            "noscale",
            file_name,
        ])
        .output()
        .map_err(|_| // handle your error here // ))?;

来源:

  1. https://www.sumatrapdfreader.org/docs/Command-line-arguments
  2. https://users.rust-lang.org/t/how-to-send-pdf-or-a-document-to-printer/74963