How to send a PDF file to an OUTQ in AS400 (IBMi Power System)

To print a PDF document directly thru AS400 you can use the Toolbox for Java/JTOpen. Also please make sure that the printer connected to that Output Queue is PDF capable that can directly take a PDF data stream and print it.

You can download jt400.jar from https://sourceforge.net/projects/jt400/ OR find it here in your system IFS path /QIBM/ProdData/HTTP/Public/jt400/lib

PrintPDFAS400.java Source

package com.mysamplecode;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

import com.ibm.as400.access.AS400;
import com.ibm.as400.access.OutputQueue;
import com.ibm.as400.access.PrintObject;
import com.ibm.as400.access.PrintParameterList;
import com.ibm.as400.access.SpooledFileOutputStream;

public class PrintPDFAS400 {

 static Properties props;

 public static void main(String[] args) {

  String filePath = null;
  String outputQueue = null;

  PrintPDFAS400 printPDF = new PrintPDFAS400();
  if (args.length < 2)
  {
   System.err.println("Usage: java " + printPDF.getClass().getName() +
     " File_Path Output_Queue");
   System.exit(1);
  }

  filePath = args[0].trim();
  outputQueue = args[1].trim();
  printPDF.sendToPrinter(filePath, outputQueue);

 }

 public boolean sendToPrinter(String filePath, String outputQueue){

  boolean printed = false; 

  try {

   props = new Properties();
   props.load(new FileInputStream("properties/as400connection.properties"));

   //Connect to AS400
   AS400 as400 = new AS400(props.getProperty("iSeries").trim(), props.getProperty("userId").trim(), props.getProperty("password").trim());

   //Get Access to Output Queue
   String outputQueuePath = "/QSYS.LIB/QGPL.LIB/" + outputQueue.trim() +  ".OUTQ";
   OutputQueue outQ = new OutputQueue(as400, outputQueuePath);

   //Parms for Spool file
   PrintParameterList parms = new PrintParameterList();
   parms.setParameter(PrintObject.ATTR_COPIES, 1);
   parms.setParameter(PrintObject.ATTR_OUTPUT_QUEUE, outQ.getPath());
   SpooledFileOutputStream osSpool = new SpooledFileOutputStream(as400, parms, null, null);

   InputStream fisPDF = null;

   //PDF needs to be downloaded from the WEB link
   if (filePath.toLowerCase().indexOf("http") == 0) {
    URL url = new URL(filePath);
    URLConnection urlConnect = url.openConnection();
    if (urlConnect.getContentType().equalsIgnoreCase("application/pdf")) {
     fisPDF = url.openStream();
    }
   }

   //PDF is stored locally on the server
   else {
    fisPDF = new FileInputStream(filePath);
   }

   byte[] buf = new byte[2048];
   int bytesRead;
   do {
    bytesRead = fisPDF.read(buf);
    if (bytesRead != -1) {
     osSpool.write(buf, 0, bytesRead);
    }
   }
   while (bytesRead != -1);
   osSpool.flush();
   osSpool.close();
   fisPDF.close();
   printed = true; 

  }
  catch (Exception e)
  {
   System.out.println("Exception occured while creating the spooled file.");
   e.printStackTrace();
  }

  return printed;

 }


}

as400connection.properties Source

iSeries=192.168.100.123
userId=SAMPLE
password=******

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.