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

Native PHP Printer API

Usage

The native PHP API, which is written in PHP 5, 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.

In order to use easyPDF, just import the library by adding require("easyPDFPrinter.php");. The Printer SDK is inside the BCL\easyPDF\Printer\ namespace. easyPDFPrinter.php 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 PHP sample code looks like this:

<?php
require("easyPDFPrinter.php");
$printer = new BCL\easyPDF\Printer\Printer();
try
{
   $printer->getPrintJob()->PrintOut("c:\\test\input.doc", "c:\\test\\output.pdf");
}
catch(BCL\easyPDF\Printer\PrinterException $ex)
{
   echo $ex->getMessage(), "\n";
}
?>

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 PHP API are slightly different than in the native .NET API. Here is an example that catches OnPageStart, which is called before printing each page:

function handlePageStart($uid, $pageNumber) { echo "Page ", $pageNumber + 1, "...\n"; return BCL\easyPDF\Printer\prnMonitorResponse::PRN_MON_CONTINUE_CONVERSION; }

$printer = new BCL\easyPDF\Printer\Printer();
try
{
   $monitor = $printer->getPrintJobMonitor();
   $monitor->OnPageStart = handlePageStart;
   $printer->getPrintJob()->PrintOut("c:\\test\abc.doc", "c:\\temp\\out.pdf");
}
catch(BCL\easyPDF\Printer\PrinterException $ex)
{
   echo $ex->getMessage(), "\n";
}

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 PHP 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 = new BCL\easyPDF\Printer\Printer();
try
{
   $printer->launchTimeout = 60000;
   $printer->getPrintJob()->PrintOut("c:\\test\input.doc", "c:\\test\\output.pdf");
}
catch(BCL\easyPDF\Printer\PrinterException $ex)
{
   echo $ex->getMessage(), "\n";
}

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 PHP 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 = new BCL\easyPDF\Printer\Printer();
try
{
   $printer->loaderSettings->useLoader = true;
   $printer->loaderSettings->userName = "easyPDF8User";
   $printer->loaderSettings->password = "your password comes here";
   $printer->getPrintJob()->PrintOut("c:\\test\input.doc", "c:\\test\\output.pdf");
}
catch(BCL\easyPDF\Printer\PrinterException $ex)
{
   echo $ex->getMessage(), "\n";
}

PrinterMonitor

PrinterMonitor is a desktop-only feature designed to enable traditional File > Print functionality in a desktop environment.

Use PrintJobMonitor, instead of PrinterMonitor, to monitor a PrintJob.PrintOut function. PrinterMonitor is only for enabling interactive prints.

PrinterMonitor should never be used on the server side, or from a service. It is not compatible with Impersonation or the Loader Service.

You can start the printer monitor by calling printer.getPrinterMonitor(). The printer monitor will only stop when the printer object is deleted (garbage collected). PrinterMonitor is not blocking, because it is running in another thread in an external process.

It is a good idea to specify the output file path before you begin printing:

$printer = new BCL\easyPDF\Printer\Printer();
try
{
   $monitor = $printer->getPrinterMonitor();
   $monitor->setOutputFileName("c:\\test\\output.pdf");

   echo "You can begin printing now. When finished, press Enter to stop the printer monitor."
        $handle = fopen("php://stdin","r");
        fgets($handle);
        fclose($handle);
}
catch(BCL\easyPDF\Printer\PrinterException $ex)
{
   echo $ex->getMessage(), "\n";
}

The same file name is used for all subsequent operations. If you don't explicitly set the file name, it is automatically chosen, usually based on the input file name. Be careful — even if you normally have access to a directory, the printing application may not.

You may also print programmatically after the printer monitor is started. However, it is preferred to use GenericPrintJob.PrintOut for all non-interactive printing.

In PHP, we do not support print progress notification events. We feel like PHP is mainly used on the server side, and PrinterMonitor is a desktop feature. Listening on callback events requires a background thread, which doesn't exist in standard PHP. It may be possible to use the pthreads extension, but most users won't have that installed, so using it would break easyPDF on most machines.

If you really need PrinterMonitor callbacks, it is still available in the COM/.NET API, as well as in C/C++, C#, VB.NET, Java and Python.