Sunday, January 19, 2020

Print PDF from Oracle APEX to Local or Remote Printer

On Twitter, I got a lot of reactions on my previous blog post Printing to local (Zebra) printer from Oracle APEX in the Cloud.

As more people asked the question of how to print to a local printer from Oracle APEX, I will cover 3 options in this post. All options make use of some features of APEX Office Print (AOP).

When you install the AOP Sample Application, you find the examples under the following sections:

1. Call Print Window of the browser directly from a button

Create a button on the page e.g. PDF and add a Dynamic Action to it. The Dynamic Action calls some JavaScript:

This JavaScript calls following AJAX Process, which uses the AOP PL/SQL API to generate a PDF:

When the PDF is produced, the printJS JavaScript function is being called, which opens the browser's print window. The end-user can change the Printer and hit the Print button.
So this option is not a direct print to a predefined printer, but a two-step process: open print window, hit print.

2. Print directly to an IP Printer

One of APEX Office Print's unique features is that the AOP Server can send a document directly to the IP address of a printer. AOP is really smart and if it sees the printer doesn't support native PDF or the document type we send, it will convert to PDF or Postscript, which all printers support.

The only thing you have to do, in order to print directly to an IP Printer is to add some global variables of AOP. In the below example it will send a PDF to the IP Printer on the same server AOP is running but on port 3000.

3. Use of Post-Process command

You can configure the AOP Server with some post-processing commands, which allow you to run some command-line tools before and after the AOP Server is creating the output. This is a really powerful feature and one use case is to send the document directly to a local (or network or remote) printer.

Just like with the IP Printer, you define the post-process command in some global variables of AOP.

This technique I also described in the previous blog post where we printed to a local Zebra printer. The only special thing in that post was that we used ngroc to make AOP known to the Oracle Cloud.
When you have the AOP Server running on-premises, you don't have that step as your APEX app will call the on-premises AOP Server. The only thing you need to make sure is that AOP Server knows the printer you configured in the post-process command.

There you go, three options to print from your Oracle APEX app directly to a local or remote printer using APEX Office Print.

Tuesday, January 14, 2020

Printing to local (Zebra) printer from Oracle APEX in the Cloud

These days Oracle Application Express (APEX) is being used more and more in "local" applications like a bar, a restaurant, a power station, etc. On many occasions, they also have the requirement to print something to a local printer.

As the Always FREE Oracle Cloud is becoming more and more popular and stable, people use that service to host their APEX apps, instead of installing Oracle XE with APEX locally.

In this blog post, I will demonstrate how to print from the Oracle Cloud to a local Zebra printer by using APEX Office Print (AOP).

In my test case, I have a Zebra Printer GK420t connected to my local computer.

It's a 3 step process:

  1. Run APEX Office Print (AOP) on a local pc
  2. Make AOP available to the Oracle Cloud
  3. Use the AOP APEX Plug-in to do the print

1. Run APEX Office Print (AOP) on a local pc

The first thing we do is download the on-premises version of APEX Office Print (AOP) for your Operating System. Unzip the file and go to the server directory and run APEX Office Print.


The first time you run AOP, AOP will create an aop_config.json file, which holds the configuration of AOP. We want to make a small modification to the file to include the local printer name.
So exit AOP again (CTRL-C) and do:

vi aop_config.json

On the last lines, you see post_process_commands, here you define your command to print to your local printer. Note that you can have as many post process commands by comma separating them.

If you want to know which printers are available on your machine, you can do:

lpstat -p | awk '{print $2}'

It will not only show you the local (connected) printers, but also the network printers. You can use any of those with AOP.

That's it! Just run AOP again and you are all set 😀

2. Make AOP available to the Oracle Cloud

Next, we need to make this local AOP server known to the Oracle Cloud. There are different ways of doing this, you could create a domain and point to the IP address of the local machine, but as the Oracle Cloud only accepts HTTPS, you would also need to set up an SSL certificate (e.g. with Letsencrypt). As this is a local machine, there's a high chance that the IP changes, so I looked for another solution which makes it easy to expose a local webserver to the internet.

This is exactly what a tool called ngrok is doing. Ngrok creates a public URL for a local webserver, in our case AOP.

Installation of ngrok, is as simple as installing AOP: download, unzip, run!

Download ngrok, unpack and run the executable:

./ngrok http 8010

It will show you the URL you can use to access your local AOP server, both HTTP and HTTPS.
In the Oracle Cloud, you always want to use the HTTPS connection.

3. Use the AOP APEX Plug-in to do the print

Import the AOP plug-in in your Oracle APEX app and specify the HTTPS URL as the AOP URL in the Component Settings of the Plug-in. Next, create a Dynamic Action and specify the Post Processing Command you want to run:

That's it! Don't think it can be any simpler than that ;)