简体   繁体   中英

How to print a PDF file from command line or by npm library?

I need to print a PDF file and i have two choices:

  • printing from nodejs app
  • executing from command line.

From node i have tried "printer", "node-native-printer, "node-printer" libraries but when i try to print my PDF i get a lot of pages with the file's source code printed in every page. From command line i tried lp/lpr and and also in this case I get the same result, lot of pages with source code (as if i were opening the PDF with a text editor and print what you can see there). I also tried to make a .txt file like this:

AAAAAAAAAAAA
qwerty
zxcvbnm

but lp/lpr prints only the first line of AAAAAAAAAAAA

I have also tried in windows, from command line (PDFtoPrinter command) prints the PDF correctly.

Actual printer in use: " Xerox WorkCentre 3225", with drivers installed both on windows and linux.

EDIT 1

My .pdf file is not just a text file, it contains text, barcodes etc. so i can't convert to txt.

EDIT 2

I found a solution:

  • pdf2ps filename.pdf filename.ps
  • lpr filename.ps

This works, but i would like to know if there is a solution to print it directly.

If you have or can install enscript , you can try:

enscript file.txt -o - | ps2pdf - text.pdf | lp -d <PRINTERNAME>

You may need ghostscript installed as well because I think ps2pdf is part of ghostscript .

I am only putting the conversion to PDF part because you give an example with text file and also mention a page printed with codes, so maybe there is a conversion step between txt and pdf which outputs something malformed.

In order to know the printer name, you can run lpstat -p -d . It will show you the list of available printers.

EDIT:

Alright I saw your edit, in this case you still can write a shell function to do all this in one go. It does not exactly answer your question, but it is equally seemless.

In your .bashrc or equivalent:

function pdfprint() {
  pdf2ps "$1" | lpr
}

And use it that way:

pdfprint filename.pdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM