Coder Social home page Coder Social logo

pdf-to-printer's Introduction

Node.js PDF printing

Build Status codecov npm

A utility to print PDF files from Node.js and Electron.

  • Supports label printers such as Rollo and Zebra.
  • Works on Windows only.

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.

Installation

Install using yarn:

yarn add pdf-to-printer

Or npm:

npm install --save pdf-to-printer

Basic Usage

Print a PDF file to the default printer:

import { print } from "pdf-to-printer";

print("assets/pdf-sample.pdf").then(console.log);

API

A function to print a PDF document.

.print(pdf[, options]) => Promise<void>

Arguments

  1. pdf (string, Required): A path to the PDF file you want to print. Will throw an error if PDF not specified or not found.
  2. 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 is odd. Will print even pages only when even.
    • 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 names noscale, shrink and fit.
    • monochrome (boolean, Optional): Prints the document in black and white. Default is false.
    • side (string, Optional): Supported names duplex, duplexshort, duplexlong and simplex.
    • 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);

.getPrinters() => Promise<Printer[]>

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);

.getDefaultPrinter() => Promise<Printer | null>

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);

Printer Size Notes

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.

Sponsor this project

If you rely on this package, please consider supporting it:

Buy Me A Coffee

License

MIT

pdf-to-printer's People

Contributors

artiebits avatar dependabot-preview[bot] avatar dependabot[bot] avatar ferretwithaberet avatar mdere avatar github-actions[bot] avatar mcdado avatar ccarnivore avatar khannanova avatar dattn avatar gallyamow avatar brunoguschagas avatar codacy-badger avatar ibrahimelaradi avatar joshycube avatar joaodematejr avatar rasgo-cc avatar matyx avatar milburngomes avatar patrick-remy avatar sebastianroming avatar mdere-lightfeatherio avatar prayuj avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.