#printing #winapi #pdf #api-bindings #xps

winprint

A crate for printing to a Windows printer device using Windows API

2 unstable releases

0.2.0 Aug 29, 2024
0.1.0 Mar 14, 2024

#67 in Windows APIs

Download history 1/week @ 2024-07-29 121/week @ 2024-08-26 15/week @ 2024-09-16 19/week @ 2024-09-23 17/week @ 2024-09-30

88 downloads per month

BSD-3-Clause

190KB
4.5K SLoC

Contains (Zip file, 70KB) test_data/test_document.xps

winprint

crates.io Released API docs BSD 3 Clause licensed

A crate for printing to a Windows printer device using Windows API.

About Safety

This crate interfaces with several Windows API functions, necessitating the use of unsafe code blocks. Nevertheless, this crate is designed to be safe and sound to use. If you find any case that will break safety or soundness, please report it as a bug.

License

Licensed under BSD 3 Clause

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, shall be licensed as BSD 3 Clause, without any additional terms or conditions.


lib.rs:

A crate for printing to a Windows printer device using Windows API.

Examples

Print a file

For a simple example presenting how to print a file:

  • Filter for the device you want to use.
  • Wrap the printer device with a printer.
  • Send a file to the printer.

First, get all printer devices via PrinterDevice::all() and filter for the device you want to use.

use winprint::printer::PrinterDevice;

fn get_my_device() -> PrinterDevice {
    let printers = PrinterDevice::all().expect("Failed to get printers");
    printers
        .into_iter()
        .find(|x| x.name() == "My Printer")
        .expect("My Printer not found")
}

Then, create a printer and send a file to it. Currently, there are two kinds of printers available:

Note: The concept Printer here is a warpper of device for printing specific types of data, not meaning the printer device.

use std::path::Path;
use winprint::printer::FilePrinter;
use winprint::printer::PrinterDevice;
use winprint::printer::XpsPrinter;

let my_device = get_my_device();
let xps = XpsPrinter::new(my_device);
let path = Path::new("path/to/test/document.xps");
xps.print(path, Default::default()).unwrap();

Specify the printing preferences

Print ticket is a set of options that can be to specify the printing preferences, It can be used to set options such as the media size, orientation, and so on. If you want to specify the printing preferences, you may use print tickets.

See Print Schema Specification for technical details.

Here is an example presenting how to use print tickets with this crate:

  • Fetch print capabilities from the printer device.
  • Filter the capabilities you want to use.
  • Create a print ticket builder for your printer device.
  • Merge the capabilities into the print ticket you are to build.
  • Build the print ticket.
  • Print the file with the print ticket.
use std::path::Path;
use winprint::printer::FilePrinter;
use winprint::printer::PrinterDevice;
use winprint::printer::XpsPrinter;
use winprint::ticket::FeatureOptionPackWithPredefined;
use winprint::ticket::PredefinedMediaName;
use winprint::ticket::PrintCapabilities;
use winprint::ticket::PrintTicket;
use winprint::ticket::PrintTicketBuilder;

let my_device = get_my_device();
let capabilities = PrintCapabilities::fetch(&my_device).unwrap();
let a4_media = capabilities
    .page_media_sizes()
    .find(|x| x.as_predefined_name() == Some(PredefinedMediaName::ISOA4))
    .unwrap();
let mut builder = PrintTicketBuilder::new(&my_device).unwrap();
builder.merge(a4_media).unwrap();
let ticket = builder.build().unwrap();
let xps = XpsPrinter::new(my_device);
let path = Path::new("path/to/test/document.xps");
xps.print(path, ticket).unwrap();

Features

  • pdfium: Enable PDFium support for printing PDF files.

Dependencies

~131MB
~2M SLoC