I've been trying for a while to print PDF files natively. I tried using FTP, but it seems none of our printers know how to handle them. I tried the Ghostprint utility on the YiP's site, but it chokes on the graphic PDF's I need to print. Most recently, I stumbled on to pdfbox, which is a Java utility for handling PDF files. I wrote a little Java app using it and from my PC it works perfectly. Under IBMi, not so much. The PrinterJob.lookupPrintServices() method that's supposed to return a list of available printers returns an empty set running under IBMi. I tried using ISeriesPrintServiceLookup. That returned the printer, but the margins are off while printing.
Any suggestions?
Any suggestions?
Code:
import java.awt.print.PrinterJob;
import java.io.File;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageable;
import com.ibm.as400.access.AS400;
import com.ibm.as400.javax.print.ISeriesPrintServiceLookup;
public class PrintPDF {
public static void main( String[] args ) throws Exception
{
String nameOS = System.getProperty("os.name");
String pdfFilePath = null;
if (nameOS.equals("OS/400")) {
System.out.println("IBMi");
pdfFilePath = "/tmp/Test.PDF";
System.out.println(printPdfFile(pdfFilePath, getIbmPrinter("PRT_NAME")));
}
else {
System.out.println("WinTel");
pdfFilePath = "C:\\temp\\Test.PDF";
System.out.println(printPdfFile(pdfFilePath, getMsPrinter("PrinterName")));
}
}
public static String printPdfFile(String pdfFile, PrintService printer) {
PDDocument document = null;
try {
// get PDF
document = PDDocument.load( pdfFile );
// setup for printing
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setJobName(new File(pdfFile).getName());
printJob.setPrintService(printer);
// print PDF
printJob.setPageable(new PDPageable(document, printJob));
printJob.print();
// success
return "0";
}
catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
finally {
if (document != null) { document = null; }
}
}
private static PrintService getIbmPrinter(String printerName) {
AS400 ibmiSystem = null;
// find printer
ibmiSystem = new AS400();
ISeriesPrintServiceLookup ips = new ISeriesPrintServiceLookup(ibmiSystem);
PrintService[] services = ips.getPrintServices();
ibmiSystem = null;
ips = null;
for(int i = 0; i < services.length; i++) {
if(services[i].getName().indexOf(printerName) != -1) {
return services[i];
}
}
return null;
}
private static PrintService getMsPrinter(String printerName) {
javax.print.PrintService[] service = PrinterJob.lookupPrintServices();
DocPrintJob docPrintJob=null;
int count = service.length;
for (int i = 0; i < count; i++) {
if (service[i].getName().equalsIgnoreCase(printerName)) {
docPrintJob = service[i].createPrintJob();
return docPrintJob.getPrintService();
}
}
return null;
}
}





Comment