npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cmd-printer

v1.3.0

Published

Module for print PDF using node.js and SumatraPDF

Downloads

20

Readme

CMD-Printer

This is a package for print PDF to printers settled in the local machine. Uses SumatraPDF v3.4.6 for print documents, and Powershell commands for lists the available printers. Works with async methods for not block tha main execution thread. For install the package:

npm i --save cmd-printer

Now this package works in Windows and WSL/WSL2 (thanks hugodecasta). Besides is compatible with CommonJS and ESM projects too.

Disclaimer

This library uses an executable to print the documents, so it's not designed to be used in frontend applications. Instead, create a backend application, then add a custom endpoint with an implementation of the library.

Usage

Classes

The library contains 2 classes, that works together:

  • CmdPrinter represents a Printer. Has some static methods to get printer instances.
  • CmdQueue executes the print jobs, and determines the behavior of the printer queue.

Print a PDF file

This is the simplest use of this library. Basically, create a new instance of CmdQueue, and execute the method this.print();, giving the path's array of the pdfs do you want to print as parameter. If the array given is empty, the CmdQueue will throws an EmptyPrintArrayError exception.

import { CmdQueue } from 'cmd-printer';

export async function main(): Promise<void> {
  // Create a new CmdQueue instance
  const cmd = new CmdQueue();
  
  // Print the document using a path array
  await cmd.print([
    './test/100x100.pdf',
    './test/100x150.pdf'
  ]);
}

Print a PDF Buffer

If you have a pdf data in a Buffer, you can pass directly the buffer to the this.print(); method as parameter. The library will be load the buffer as a temporaly file, and destroy the file when the printing process ends.

import { CmdQueue } from 'cmd-printer';

export async function print(byteDctos: Buffer[]): Promise<void> {
  // Create a new CmdQueue instance
  const cmd = new CmdQueue();
  
  // Print the buffer
  await cmd.print(byteDctos);
}

Using a custom printer

By default, a CmdQueue instance prints documents to the default system printer, using the default printer configuration. If do you want to change the printer, you can use CmdPrinter class to get all installed printers, and select one programatically.

import { CmdPrinter, CmdQueue } from 'cmd-printer';

export async function main(): Promise<void> {
  // Get all printers installed in the system as Array
  const printers = await CmdPrinter.getAll();

  // Select one using "this.map();" method
  const printer = printers.find(x => x.shared);

  // Create a new CmdQueue instance with the desired printer
  const cmd = new CmdQueue({ printTo: printer });

  // Print the document
  await cmd.print([ './test/100x100.pdf' ]);
}

Override the default configuration

If you want to set the printing options, you can change the default settings in the constructor, or manipulating the this.options property.

import { CmdQueue } from 'cmd-printer';

export async function main(): Promise<void> {
  // Create a new CmdQueue instance
  const cmd = new CmdQueue({
    orientation: 'landscape',   // The orientation of the page
    silent: true,               // Don't show errors or warnings
    scale: 'fit'                // Size adjustment
  });
  
  // Print the document
  await cmd.print([ './test/100x100.pdf' ]);
}

Printing options

The options available to configure are the following:

  • silent: boolean. Silences any error messages related to command line printing. By default is false.

  • printTo: CmdPrinter. Prints all files indicated to the given printer. After printing, SumatraPDF exits immediately. If this parameter is not specified, the default system setted printer will be used.

  • scale: Scale. The size adjustment of the document into the paper. The values admitted are "noscale", "shrink" and "fit".

  • monochrome: boolean. Prints the document in black and white. By default, this option is false.

  • orientation: Orientation. Can provide 90 degree rotation of contents (NOT the rotation of paper which must be pre-set by the choice of printer defaults). The values accepted are "portrait" and "landscape".

  • printingMode: PrintingMode. Available printing modes:

    • "duplex": Prints the paper in both sides.
    • "duplexshort": Duplex mode using the shortest edge as flip axis.
    • "duplexlong": Duplex mode using the longest edge as flip axis.
    • "simplex": One side printing mode.
  • paperTray: string | number. Select a specific paper tray (by index number or by name) for use.

  • paperSize: PaperSize. Override the paper size of the document, applying this one instead. The values accepted are "A2", "A3", "A4", "A5", "A6", "letter", "legal", "tabloid" or "statement".

  • half: Half. Prints only "odd" or "even" pages of the document.

  • pages: (number | PageInterval)[]. Sets the pages do you want to print. Some examples:

// Prints the pages 1, 4, 5
const cmd = new CmdQueue({
  pages: [ 1, 4, 5 ]
});

// Prints page 1, and pages from 3 to 6
const cmd = new CmdQueue({
  pages: [ 1, { from: 3, to: 6 } ]
});

About CmdPrinter class

This class represents an individual printer, however also has some static methods to get the desired printers:

CmdPrinter.getAll

This function returns a promise of CmdPrinter[]. In other terms, returns a promise with all printers installed in the current machine. For example:

import { CmdPrinter } from 'cmd-printer';

export async function main(): Promise<void> {
  // Get all printers installed in the system as Array
  const printers = await CmdPrinter.getAll();
  console.log(printers);
}

CmdPrinter.find

This function makes a search using a delegate function asynchronously. Returns a promise with the first CmdPrinter instance found, or null if doesn't found. For example:

import { CmdPrinter } from 'cmd-printer';

export async function main(): Promise<void> {
  // Get a printer called "Zebra Printer"
  const printer = await CmdPrinter.find(x => x.name === 'Zebra Printer');
  console.log(printer);
}