Crate winprint

source ·
Expand description

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

§Examples

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::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_size()
    .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.

Modules§

  • Provides a way to print various types of data to a printer device.
  • Utilities for testing
  • Provides a way to specify the printing preferences.