DEV Community

Christos Panagiotakopoulos
Christos Panagiotakopoulos

Posted on

Why would you want an API to print a PDF?

Why would somebody need an API to just print a PDF file? Well... Hear my short story:

At the company where I work, we developed a small "kiosk" React app that is running on a full-screen browser and uses speech recognition to gather some details and then automatically print a receipt.

The issue is when you issue the command at the browser to print the page, it opens the system's print dialog. As a result, the experience becomes mediocre for the user. The app is running on localhost (and will never run on the web) so this allowed me to create a simple solution:

Run a small nodejs backend on localhost that directly interfaces with the printer and issues the command to print without opening any system dialog.

Because I some of you might benefit from it, I published it on Github here, to check it out. It's fairly basic but it does its job (and why develop something when it is already working?)

It could also be used for IoT applications, I have some usecases in mind for my small RasPi.

You can check it out here:

GitHub logo chrispanag / printer-api

Upload a PDF and automatically print it 🖨️

Printer API

Ever wanted to upload a PDF from your web app and just print it? Here's the solution ;)

Getting Started

  1. Clone the repo
  2. yarn
  3. Edit the .env file. (See below for details)
  4. yarn run build
  5. yarn start

How to edit the .env file

PRINTER_NAME=<THE_NAME_OF_YOUR_PRINTER> (or delete the key completely to use your 'default' printer)
PORT=<PORT> (Default is 3000, change it as you wish)
Enter fullscreen mode Exit fullscreen mode

How to perform the request

curl --location --request POST 'http://localhost:3000/print' \
--form 'pdf=@<path_to_file>'

Troubleshooting

I've only tested it on a Linux system (Ubuntu), and most probably it won't work on Windows. Also, it won't run (at least out of the box) on Docker.

Acknowledgments

Thanks to @thiagoelg for maintaining the npm module to interface with the printer.




Top comments (0)