Post by MarkI am asked to print PDF's directly from the AS400 and to do this with
Java. I found an open source project PDFBOX and am trying to implement
this somehow.
This library uses java.awt.PrinterJob.getPrinterJob to setup a printer
java.lang.NullPointerException
at java.lang.Class.forName(Class.java:242)
at java.awt.print.PrinterJob$1.run(PrinterJob.java:60)
at java.awt.print.PrinterJob.getPrinterJob(PrinterJob.java:54)
at org.pdfbox.pdmodel.PDDocument.print(PDDocument.java:708)
at org.pdfbox.PrintPDF.main(PrintPDF.java:97)
Any ideas how to do this on the AS400 platform ?
Hi,
I have done this with iText and it is working wery well.
There is source of printing class..only you need to download iText pdf
library(google it)
and copy Courier.ttf to IFS folder where your class file is.
import java.io.*;
import com.ibm.as400.access.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;
public class spltopdf extends Object{
public static void main(String[] parameters){
if (parameters.length != 7 ){
System.out.println("");
System.out.println("Parameters are not correct.");
System.out.println("");
System.out.println("Command syntax is: spltopdf path name rows
cols notes library file member");
System.out.println("");
System.out.println("Syntax example is: spltopdf /mypdf order 66
198 Demo qgpl tmpspool m000001");
System.out.println("");
System.exit(0);
}
try {
String PDFPATH = parameters[0];
String PDFNAME = parameters[1];
String ROWS = parameters[2];
String COLS = parameters[3];
String LIBRARY = parameters[4];
String FILE = parameters[5];
String MEMBER = parameters[6];
System.out.println(PDFPATH);
System.out.println(PDFNAME);
System.out.println(ROWS);
System.out.println(COLS);
System.out.println(LIBRARY);
System.out.println(FILE);
System.out.println(MEMBER);
int RecordLength = Integer.valueOf(COLS).intValue();
AS400 as400 = new AS400();
QSYSObjectPathName filename = new
QSYSObjectPathName(LIBRARY, FILE, MEMBER, "MBR");
SequentialFile file = new SequentialFile(as400,
filename.getPath());
// Retrieve the record format for the file
AS400FileRecordDescription recordDescription = new
AS400FileRecordDescription(as400, filename.getPath());
RecordFormat[] format =
recordDescription.retrieveRecordFormat();
file.setRecordFormat(format[0]);
file.open(SequentialFile.READ_ONLY, 100,
SequentialFile.COMMIT_LOCK_LEVEL_NONE);
System.out.println("Start Creation of "+ PDFPATH + "/" +
PDFNAME + ".pdf");
try {
// Create the courier.ttf font object
BaseFont courier = BaseFont.createFont("courier.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(courier, 10f); // The fontsize is
10
float f = courier.getWidthPoint("0123456789", 10f); //
length of 10 fixed characters width
float Xpoints = Float.valueOf(COLS).floatValue() * f /
10 + 72 ; // the page width in pixels
// leading = the space between two lines, that is 1.5
times the fontsize
int leading = 15 ;
float Ypoints = (Float.valueOf(ROWS).floatValue() + 5)
* leading + 72 ; //The page height in pixels
// Create the Rectangle object in a certain color and
use this as pageSize
Rectangle pageSize = new Rectangle(Xpoints, Ypoints);
// pageSize.setBackgroundColor(new java.awt.Color(0xFF,
0xFF, 0xDE));
Document document = new Document(pageSize,36,36,36,36);
// The four margins have 36 pixels (0.5 of inch)
IFSFileOutputStream target = new
IFSFileOutputStream(as400,PDFPATH+"/"+PDFNAME+".pdf",IFSFileOutputStream.SHA
RE_NONE, false);
// Create an instance of the com.lowagie.text.Document
object
PdfWriter.getInstance(document, target);
document.addTitle("Spool2pdf converter");
document.addSubject("Spool file converted to pdf
format");
document.addCreator(as400.getUserId());
document.addAuthor(as400.getUserId());
document.addHeader("Expires", "0");
document.open();
boolean FirstTime = true;
String NewPage = "1";
String SpaceBeforeOneLine = "0";
String SpaceBeforeTwoLines = "-";
String fcfc = " ";
String linef = "";
Record data = file.readNext(); // Read first record of
work file
while (data != null) {
if (FirstTime)
FirstTime = false;
else
fcfc = (String) data.getField(0);
linef = (String) data.getField(1);
// According to the value of control char FCFC
create new line, lines or page
if ( fcfc.compareTo(NewPage)==0 ) {
document.newPage(); }
if ( fcfc.compareTo(SpaceBeforeOneLine)==0 ) {
document.add(new Paragraph(leading, " ",
font)); }
if ( fcfc.compareTo(SpaceBeforeTwoLines)==0 ) {
document.add(new Paragraph(leading, " ", font));
document.add(new Paragraph(leading, " ", font));
}
document.add(new Paragraph(leading, linef, font));
// add a spooled file line to PDF
data = file.readNext(); // Read next record of work
file
}
document.close();
as400.disconnectAllServices();
System.out.println("Finnished Creation of "+ PDFPATH +
"/" + PDFNAME + ".pdf");
} catch(DocumentException de) {
System.out.println(de.getMessage());
System.exit(0);
} catch(IOException ioe) {
System.out.println(ioe.getMessage());
System.exit(0);
}
} catch (Exception e) {
System.out.println("Could not read the work file");
System.out.println(e.getMessage());
System.exit(0);
}
System.out.println("Finnished creation of pdf document!");
System.exit(0);}
}
Cheers,
Niko.