When is this usefull?
- When you want to print PDF's without a user that needs to push buttons (batchprocessors)
- When you want to print on machines that do not have software installed to print PDF's (for example Acrobat)
How does it work?
The PDFEncoder bean that can be installed as a bean for Servoy allready gives you the ability to view PDF's that are either on disk, in memory or in the database. The same bean can be used to print directly. For this, some java code is required. Servoy has the ability to use java code directly in it's methods. The PDFEncoder is used to send a print command directly through Java's printing services, which can allready handle the printing of PDF.
Requirements
- The latest version of the PDFEncoder bean. This bean can be downloaded from the download section of the the manufacturers website: http://www.jpedal.org/. You need to download jpedal and place the downloaded jar-file in the beans directory of Servoy. Don't forget to remove the old JPedal.jar library!!!
The method:
- Code: Select all
try
{
var docflavor = Packages.javax.print.DocFlavor;
var service = docflavor.SERVICE_FORMATTED
var pageble = service.PAGEABLE;
var defaultService = Packages.javax.print.PrintServiceLookup.lookupDefaultPrintService();
if ( defaultService != null )
{
var job = defaultService.createPrintJob();
//var decodePDF = elements.XXX; //uncomment if you have the PDFEncoder bean on your form
var decodePDF = new Packages.org.jpedal.PdfDecoder(true); //comment out if you have the PDFEncoder bean on your form
decodePDF.openPdfArray( XXX /*blobfield name*/ ); //comment out if you have the PDFEncoder bean on your form
var pageCount = decodePDF.getPageCount();
decodePDF.setPagePrintRange(1,pageCount);
var doc = new Packages.javax.print.SimpleDoc(decodePDF, pageble, null);
job.print( doc, null );
}
}
catch ( e )
{
application.output(e.toString());
}
Limitations
- This is not a feature rich piece of code. All possible layout options are not embedded. If you need to do things with formatting, check out the forum on the earlier mentioned website and extend the code to your liking.
- The method is hardcoded to use the default printer. Use the internet to find sources to rewrate that part of the logic to make it more flexible
- Since this solutions accesses the Java layer, when used in the WebClient, the processing will be done Serverside, so printing will also take place on the Server.
Paul