A utility to print PDF files from Node.js and Electron.
If you are looking for a utility that will work on Unix-like operating systems, then take a look at https://github.com/artiebits/unix-print.
Install using yarn
:
yarn add pdf-to-printer
Or npm
:
npm install --save pdf-to-printer
Print a PDF file to the default printer:
import { print } from "pdf-to-printer";
print("assets/pdf-sample.pdf").then(console.log);
A function to print a PDF document.
Arguments
pdf
(string
, Required): A path to the PDF file you want to print. Will throw an error if PDF not specified or not found.options
(Object
, Optional):printer
(string
, Optional): Send a file to the specified printer.pages
(string
, Optional): Specifies which pages to print in the PDF document.subset
(string
, Optional): Will print odd pages only when value isodd
. Will print even pages only wheneven
.orientation
(string
, Optional): Can provide 90-degree rotation of contents (NOT the rotation of paper which must be pre-set by the choice of printer defaults).scale
(string
, Optional): Supported namesnoscale
,shrink
andfit
.monochrome
(boolean
, Optional): Prints the document in black and white. Default isfalse
.side
(string
, Optional): Supported namesduplex
,duplexshort
,duplexlong
andsimplex
.bin
(string
, Optional): Select tray to print to. Number or name.paperSize
(string
, Optional): Specifies the paper size. On windows you can find this as a dropdown selection in your printer preference. Refer to Printer Size note below.silent
(boolean
, Optional): Silences SumatraPDF's error messages.printDialog
(boolean
, Optional): displays the Print dialog for all the files indicated on this command line.copies
(number
, Optional): Specifies how many copies will be printed.
Returns
Promise<void>
: a Promise that resolves with undefined.
Examples
To print a PDF file to the default printer:
import { print } from "pdf-to-printer";
print("assets/pdf-sample.pdf").then(console.log);
To print to a specific printer:
import { print } from "pdf-to-printer";
const options = {
printer: "Zebra",
};
print("assets/pdf-sample.pdf", options).then(console.log);
Example with a few print settings. It will print pages 1, 3, 5 and scale them so that they fit into the printable area of the paper.
import { print } from "pdf-to-printer";
const options = {
printer: "Zebra",
pages: "1-3,5",
scale: "fit",
};
print("assets/pdf-sample.pdf", options).then(console.log);
A function to get a list of available printers.
Returns
Promise<Printer[]>
: a Promise that resolves with a list of available printers.
Examples
import { getPrinters } from "pdf-to-printer";
getPrinters().then(console.log);
A function to get the default printer info.
Returns
Promise<Printer | null>
: a Promise that resolves with the default printer info, or null
if there is no default printer.
Examples
import { getDefaultPrinter } from "pdf-to-printer";
getDefaultPrinter().then(console.log);
For example, If you need 4"x3" paper size and assuming it is selectable from your printer preference settings, you can set the following:
print("assets/Labeli.pdf", {
paperSize: "4.00\"x3.00\"(102mm x 76mm)"
})
The value in this example of 4.00x3.00(102mm x 76mm)
should exist in your printing preference settings.
If you rely on this package, please consider supporting it: