BCL easyPDF SDK
easyPDF SDK Usermanual
PDF Creator Programming API  |  Download Free Trial  |  Contact Us to Purchase

Native Python Printer API

Usage

The native Python API, which is written in Python 3, is a thin interface around a binary executable. It is very similar to the native .NET API, with the exception of some language-specific differences.

Note: there is no native Python 2 API. That was a very different language than Python 3.

In order to use easyPDF, just import the library by adding import easyPDFPrinter. The Printer SDK is inside the easyPDFPrinter. namespace. easyPDFPrinter.py can be found under C:\Program Files\Common Files\BCL Technologies\easyPDF 8.

When an error occurs, an exception of type PrinterException is thrown.

The Printer class has a destructor, which means it is automatically disposed when the object goes out of scope. That makes memory management trivial, just make sure you are not holding on to the variable when you are no longer using the SDK.

If the customer's application crashes, the system automatically closes all pipes belonging to the process, which means all related worker processes automatically quit as well.

The minimal Python sample code looks like this:

import easyPDFPrinter
printer = easyPDFPrinter.Printer()
try:
   printJob = printer.getPrintJob()
   printJob.PrintOut("c:\\test\\input.doc", "c:\\test\\output.pdf")
except easyPDFPrinter.PrinterException as ex:
   print(ex)

Printer's constructor is designed to never throw exceptions. That's because it does not launch a worker process and does not create a named pipe, it merely initializes a few variables to their default values. In other words, creating a Printer object is extremely lightweight, like creating a Color object.

However, as soon as you do anything else, it instantly launches a worker process.

Notification Events

Notification events in the native Python API are slightly different than in the native .NET API. Here is an example that catches OnPageStart, which is called before printing each page:

def PageStart(uid, pageNumber):
   print("Page %d..." % (pageNumber + 1))
   return easyPDFPrinter.prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION

printer = easyPDFPrinter.Printer()
try:
   monitor = printer.getPrintJobMonitor()
   monitor.OnPageStart = PageStart
   printJob = printer.getPrintJob()
   printJob.PrintOut("c:\\test\\input.doc", "c:\\test\\output.pdf")
except easyPDFPrinter.PrinterException as ex:
   print(ex)

Alternatively, the event handler can be a class method:

class Handler:
   def PageStart(self, uid, pageNumber):
      print("Page %d..." % (pageNumber + 1))
      return easyPDFPrinter.prnMonitorResponse.PRN_MON_CONTINUE_CONVERSION

printer = easyPDFPrinter.Printer()
handler = Handler()
try:
   monitor = printer.getPrintJobMonitor()
   monitor.OnPageStart = handler.PageStart
   printJob = printer.getPrintJob()
   printJob.PrintOut("c:\\test\\input.doc", "c:\\test\\output.pdf")
except easyPDFPrinter.PrinterException as ex:
   print(ex)

Launch Timeout

Launching the worker process and connecting the named pipe should happen very fast, in milliseconds. However, under an abnormally heavy load, the computer may not have enough cycles to perform this task in a timely fashion.

The Native Python API has an internal timeout built-in. The default value is 1 minute. This is because a server may momentarily slow down so much that it is not responding for seconds, but it usually recovers after a while.

The default timeout value can be changed via Printer's launchTimeout member (in milliseconds). This must be performed immediately after the creation of the Printer object.

printer = easyPDFPrinter.Printer()
try:
   printer.launchTimeout = 60000
   printJob = printer.getPrintJob()
   printJob.PrintOut("c:\\test\\input.doc", "c:\\test\\output.pdf")
except easyPDFPrinter.PrinterException as ex:
   print(ex)

Upon timeout an exception is thrown, and the SDK cannot be used. Another attempt may be made later.

Server-Side Operation

Printing from the server side has always required easyPDF Loader, a special service that helps execute functions under a different user account.

The Native Python SDK offers two possible solutions for server-side printing. One is called the Loader Service, and the other one is called Impersonation.

Customers who are printing from a web server or a system service should study both alternatives, and decide which one they prefer.

Both the Loader Service and Impersonation are under Printer's loaderSettings member. This must be set up immediately after the creation of the Printer object.

For example, here is how to use Impersonation:

printer = easyPDFPrinter.Printer()
try:
   printer.loaderSettings.useLoader = True
   printer.loaderSettings.userName = "easyPDF8User"
   printer.loaderSettings.password = "your password comes here"
   printJob = printer.getPrintJob()
   printJob.PrintOut("c:\\test\\input.doc", "c:\\test\\output.pdf")
except easyPDFPrinter.PrinterException as ex:
   print(ex)

PrinterMonitor

PrinterMonitor does not exist in the native Python API at this moment.

Note that most customers do not need PrinterMonitor. Its sole purpose is to enable File > Print in a desktop environment. It was not designed for server-side operation anyway.

If you just want to get notification events while printing, please use PrintJobMonitor, which is part of the native Python API.

If you really need PrinterMonitor, it is still available in the COM API.