-1

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.

jww
  • 97,681
  • 90
  • 411
  • 885
William.P
  • 114
  • 9
  • Could you provide the command lines you've tried for using `lp`/`lpr` ? – Mig Jul 29 '19 at 08:20
  • "lp filename.pdf", "lpr filename.pdf", "lpr -o PDF filename.pdf". Tried to convert pdf into jpg and "lpr filename.jpg" gave similar result. – William.P Jul 29 '19 at 08:36
  • Possible duplicate of [Print PDF from command line in Linux](https://unix.stackexchange.com/q/421341), [CUPS printing of PDF files](https://stackoverflow.com/q/8562292/608639), [Print PDF as image from command line?](https://stackoverflow.com/q/6504036/608639), [Printing PDFs from Windows Command Line](https://stackoverflow.com/q/19124808/608639), etc. (This looks like a [Super User](http://superuser.com/) question to me). – jww Jul 29 '19 at 15:55

1 Answers1

1

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
Mig
  • 662
  • 4
  • 13
  • You are right i was not so clear. I edited the qestion, is not just a simple text file, i tried to convert to txt but it will be not correct. – William.P Jul 29 '19 at 10:38