Today I am going to present you a plugin that I have just created. It is a plugin to print a PDF file silently, that is, without asking the user for confirmation.
The best thing about this is that you can use the virtual printers that Windows offers, in this way you can do your tests in an ecological way.
Throughout this post I will show you the documentation of the plugin and all the details of it, including how you can get it.
Using the plugin
The plugin should be running in the background whenever you want to print. You can run it manually or put it in the Windows startup folder to start automatically.
By the way, this plugin to print a PDF silently does not run in a window, it will be hidden. To stop it, go to the task manager.
However, sometimes a pop-up or temporary window is likely to open depending on the actions (for example, if the indicated PDF does not exist).
In the same location of the plugin, the file called
PDFtoPrinter.exe must exist, for this there is no alternative.
This file is not mine, so I can’t distribute it. But it seems to me that you can download it totally free, as I indicate in my post on Printing PDF from the command line.
Any printer that appears in the Windows control panel is capable of being used by this plugin, just make sure you type the name of them correctly.
In fact, this plugin can print to ink, toner, thermal printers (POS) or even virtual printers.
For the examples that I am going to put, I will assume that the PDF file is called
ticket.pdf. Let’s look at the first case.
If the PDF is in the same location as the plugin
In case the executable plugin and the PDF are in the same directory (folder), you must send only the name of the document to the API. For example, for this case it would be
In case the PDF is in another location
If the plugin is in a different directory than the plugin or if you like absolute paths, you can indicate the path without problems. For example, I have
ticket.pdf on my desktop so the full path is:
C:\Users\Luis Cabrera Benito\Desktop\ticket.pdf
In this way you can print the PDF regardless of its location, as long as the plugin can access it. By this I mean that the PDF must be on the same computer as the plugin.
This plugin creates a web server at
localhost:8080 and exposes two routes; one to print a local PDF and one to print a remote PDF.
The root path is where the printing of a PDF document is requested indicating the following parameters in the URL:
impresora: the name of the printer as it appears on the control panel
nombrePdf: the absolute path or name of the PDF document. If it does not exist, a pop-up window will be displayed.
For example, to print a PDF called
ticket.pdf on the
ZJ58 printer, I would make a GET request to the following URL:
And as I indicated earlier, if I wanted to print a PDF that is in another location, I would do it with a GET request like this:
Notice that the location of the PDF has spaces but I have replaced them with the entity
Also take into account that the path must have the antidiagonal
\ so if you want to indicate the path in the language, you must escape it with itself, that is, like this:
\\. You will understand it better in the examples.
By the way, the API will return a 200 response code in case everything goes well. And a 500 code in case there is an error (if there is an error, it will be in the response body).
I want to clarify that even if the API returns a 200 response, if the printer does not exist or there is some internal error, this will not be reported. The most common case is when the printer does not exist; the other cases are under control.
Printing remote PDF from internet by URL
As you have noticed, up to this point I have taught you how to print a local PDF. But you can print a PDF that is on your hosting or on an internet site.
For example, to print a PDF from the internet that is at
https://parzibyte.github.io/cv/assets/cv_LuisCabreraBenito.pdf on the
Microsoft Print to PDF printer, I would make a GET request to the following URL:
Notice that now the request is made to
localhost:8080/url since when it is made to
localhost:8080/ it is to print a local PDF. The parameters are
impresora indicating the name of the printer and
urlPdf indicating the URL of the PDF that is going to be printed.
To give another example, if I wanted to print a PDF that is in
localhost/archivos/mi_pdf.pdf on the
POS58 printer, I would make a request to the following URL:
Important note: it does not matter the site or domain where the PDF is to be printed silently from the cloud.
The only thing that is important is that it is publicly available (that anyone who visits that URL has access to it) since internally the plugin will download it and then print it; all this in a silent way.
Getting the plugin – Download
The plugin is not open source or free; It has a cost of 25 USD. If you want to get it, send me a message.
At the moment it is only compatible with Windows. Obviously the following examples won’t work if you don’t have the plugin, but once you have it running in the background, you can test them.
Let’s look at the first example. In this case we assume that the PDF file (
ticketx.pdf) is in the same directory as the plugin. The code looks like this:
We are making the request on line 50, and handling the response on line 51.
Example 2: user supplied values
If you want the user to be able to indicate the values such as the printer and the name or path of the PDF you can do it, here is an example:
Example 3: absolute path of PDF
If you want to indicate the name of the PDF as the absolute path (in case it is not in the same directory as the plugin) then remember to escape the backslash. Here the example:
The sequence escape I mention is on line 47, please note that since Windows paths are indicated with
\, I must escape this character with
\\. Also, I am also using the function that replaces spaces, as my path has spaces.
Example 4: PDF from URL
Here I show you an example to print a PDF through its URL:
In short, just download the
PDFtoPrinter.exe file, contact me to get the plugin, put both files in the same directory and run the plugin, either when starting Windows or manually.
With the examples and the API it is more than enough to understand how it works, but I also leave you a video on YouTube:
By the way, I also have another plugin to print to POS thermal printers.