Files
Python_Examples/spool2PDF/STM/spool2PDF.py
T
2021-08-26 22:34:38 -04:00

312 lines
11 KiB
Python

'''
Created on Sep 4, 2013
@author: Wes
'''
from STM.TabelModels import SpooledFileTableModel
from com.ibm.as400.access import AS400, SpooledFileList, SpooledFile, \
PrintParameterList, PrintObject, OutputQueue
from com.itextpdf.text import Document, PageSize, Paragraph, FontFactory, Chunk, \
Image
from com.itextpdf.text.pdf import PdfWriter
from java.awt import BorderLayout, GridLayout, Dimension
from java.io import BufferedReader, InputStreamReader, FileOutputStream, \
ByteArrayInputStream, File
from java.lang import Math, String, Runtime
from javax.imageio import ImageIO
from javax.swing import JFrame, UIManager, JTable, JScrollPane, JPanel, JLabel, \
JTextField, JComboBox, JButton, JMenuBar, JMenuItem, JMenu, JOptionPane
import java.lang.Exception as JavaException
import os
outQueues = ['PXP1', 'PXP1P', 'PXP2', 'PXP2P', 'PXP3', 'PXP3P', 'PXP4', 'PXP4P', 'P0', 'P1', 'P2', 'PS', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9', 'CALEB', 'WES', 'VDORMAN']
system = None
qtm = None
userMaskInput = None
outQSelector = None
spooledFiles = []
table = None
def getSpooledFileList(system, userMask, outQMask):
spooledFiles = []
splfList = SpooledFileList(system)
#Set filters, all users, on all queues
splfList.setUserFilter(userMask)
splfList.setQueueFilter(outQMask)
#Open list, openAsynchronously() returns immediately
splfList.openAsynchronously()
#Wait for list to complete
splfList.waitForListToComplete()
enum = splfList.getObjects()
#Add spooled file object to a list
while(enum.hasMoreElements()):
splf = enum.nextElement() #splf is a com.ibm.as400.access.SpooledFile object
if not(splf == None):
spooledFiles.append(splf)
#Clean up after we are done with the list
splfList.close()
return spooledFiles
def copySpooledFile(event):
splf = spooledFiles[table.getSelectedRow()]
tableInput = [splf]
title = 'Make a Copy of a Spooled File'
panel = JPanel(BorderLayout())
tempTable = JTable()
tempTable.setModel(SpooledFileTableModel())
tempPane = JScrollPane(tempTable)
tempTable.setFillsViewportHeight(True)
qtm = tempTable.getModel()
qtm.loadTable(tableInput)
bottomPanel = JPanel()
bottomPanel.add(JLabel('to output queue: '))
outQs = JComboBox(outQueues)
inputPanel.add(outQs)
bottomPanel.add(outQs)
panel.add(JLabel('Copy the following spooled file(s)'), BorderLayout.NORTH)
panel.add(tempPane, BorderLayout.CENTER)
panel.add(bottomPanel, BorderLayout.SOUTH)
panel.setMaximumSize(Dimension(800, 200))
options = ['OK', 'Cancel']
option = JOptionPane.showOptionDialog(frame, panel, title, JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE, None, options, options[0])
if(option == 0):
queueFilter = '/QSYS.LIB/%LIBL%.LIB/'
queueFilter += outQueues[outQs.getSelectedIndex()]
queueFilter += '.OUTQ'
splf.copy(OutputQueue(system, queueFilter))
def reloadTable(event):
global spooledFiles
queueFilter = '/QSYS.LIB/%LIBL%.LIB/'
queueFilter += outQueues[outQSelector.getSelectedIndex()]
queueFilter += '.OUTQ'
spooledFiles = getSpooledFileList(system, userMaskInput.getText(), queueFilter)
qtm.loadTable(spooledFiles)
def findLongestLine(inStream):
x = 0
d = BufferedReader(InputStreamReader(inStream))
data = d.readLine()
while not(data == None):
x = Math.max(x, len(data))
data = d.readLine()
inStream.selectPage(1)
return x
def exportAsPDF(event):
errors = []
for i in table.getSelectedRows():
try:
splf = spooledFiles[i]
doc = Document(PageSize.LETTER, 0.0, 0.0, 0.0, 0.0)
PdfWriter.getInstance(doc, FileOutputStream(checkFileName(splf.getStringAttribute(SpooledFile.ATTR_USERDATA) + '.pdf')))
doc.open()
width = doc.getPageSize().getWidth()
height = doc.getPageSize().getHeight()
printParms = PrintParameterList()
#printParms.setParameter(PrintObject.ATTR_WORKSTATION_CUST_OBJECT, "/QSYS.LIB/QCTXPDF2.WSCST")
printParms.setParameter(PrintObject.ATTR_WORKSTATION_CUST_OBJECT, "/QSYS.LIB/QWPGIF.WSCST")
printParms.setParameter(PrintObject.ATTR_MFGTYPE, "*WSCST")
inStream = splf.getPageInputStream(printParms)
derp = []
j = 1
while not(j > inStream.getNumberOfPages()):
while(inStream.available() > 0):
derp.append(inStream.read())
#image = ImageIO.read(ByteArrayInputStream(derp))
image = Image.getInstance(derp)
image.setAbsolutePosition(0.0, 0.0)
image.scaleToFit(width, height)
doc.add(image)
doc.newPage()
inStream.nextPage()
j += 1
print j
doc.close()
#ImageIO.write(image, 'GIF', File('out.gif'))
except JavaException:
pass
def exportAsPDFWithText(event):
errors = []
for j in table.getSelectedRows():
try:
splf = spooledFiles[j]
printParms = PrintParameterList()
#printParms.setParameter(PrintObject.ATTR_WORKSTATION_CUST_OBJECT, "/QSYS.LIB/QCTXPDF2.WSCST")
printParms.setParameter(PrintObject.ATTR_WORKSTATION_CUST_OBJECT, "/QSYS.LIB/QWPDEFAULT.WSCST")
printParms.setParameter(PrintObject.ATTR_MFGTYPE, "*WSCST")
inStream = splf.getPageInputStream(printParms)
lineLen = findLongestLine(inStream)
print lineLen
d = BufferedReader(InputStreamReader(inStream))
i = 1
adjustLeading = False
if(lineLen > 180):
doc = Document(PageSize.LETTER.rotate(), 4.0, 4.0, 4.0, 0.0)
fontSize = 6.5
scaling = 1.0
leading = 0.8
adjustLeading = True
elif(lineLen > 170):
doc = Document(PageSize.LETTER.rotate(), 4.0, 4.0, 4.0, 0.0)
fontSize = 7.1
scaling = 1
leading = 1.0
adjustLeading = True
elif(lineLen > 150):
doc = Document(PageSize.LETTER.rotate(), 4.0, 4.0, 4.0, 0.0)
fontSize = 8
scaling = 1.0
leading = 1.1625
adjustLeading = True
elif(lineLen > 120):
doc = Document(PageSize.LETTER.rotate(), 4.0, 4.0, 4.0, 0.0)
fontSize = 9.5
scaling = 1.03
leading = 1.0
adjustLeading = True
elif(lineLen > 110):
doc = Document(PageSize.LETTER.rotate(), 4.0, 4.0, 4.0, 0.0)
fontSize = 8.5
scaling = 1.25
leading = .8
adjustLeading = True
elif(lineLen > 92):
doc = Document(PageSize.LETTER, 4.0, 4.0, 4.0, 0.0)
fontSize = 8.0
scaling = 1.2
elif(lineLen > 80):
doc = Document(PageSize.LETTER, 4.0, 4.0, 4.0, 0.0)
fontSize = 7.0
scaling = 1.5
elif(lineLen > 70):
doc = Document(PageSize.LETTER, 4.0, 4.0, 4.0, 0.0)
fontSize = 7.9
scaling = 1.5
else:
doc = Document(PageSize.LETTER, 4.0, 4.0, 4.0, 0.0)
fontSize = 8.45
scaling = 1.475
filename = checkFileName(splf.getStringAttribute(SpooledFile.ATTR_USERDATA) + '.pdf')
PdfWriter.getInstance(doc, FileOutputStream(filename))
doc.open()
courierFont = FontFactory.getFont(FontFactory.COURIER, fontSize)
while not(i > inStream.getNumberOfPages()):
para = Paragraph('', courierFont)
if(adjustLeading):
para.setLeading(0.0, leading)
data = d.readLine()
while not (data == None):
c = Chunk(data)
c.setHorizontalScaling(scaling)
para.add(c)
para.add(Chunk.NEWLINE)
data = d.readLine()
doc.add(para)
doc.newPage()
inStream.nextPage()
i += 1
d.close()
doc.close()
except JavaException:
errors.append(splf.getStringAttribute(SpooledFile.ATTR_USERDATA))
if(len(errors) > 0):
JOptionPane.showMessageDialog(frame, 'An error occurred while processing the following spooled files:\n\r' + str(errors) + '\n\rThey probably have bar codes on them and I havn\'t figured that out yet.', "An Error Has Occurred!", JOptionPane.ERROR_MESSAGE)
if(len(table.getSelectedRows()) == 1):
openPDF(filename)
def checkFileName(name):
if(os.path.exists(name)):
s = name.split('.')[0]
return checkFileName(s + '_' + '.pdf')
else:
return name
def openPDF(inFile):
javaexec = getattr(Runtime.getRuntime(), "exec")
javaexec("cmd.exe /c start " + inFile)
if __name__ == '__main__':
system = AS400('SUNBURY', 'WRAY', 'wesman88')
frame = JFrame('Spoolinator')
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setSize(800, 600)
frame.setLayout(BorderLayout())
#initialize table and scroll pane
table = JTable()
table.setModel(SpooledFileTableModel())
scrollPane = JScrollPane(table)
table.setFillsViewportHeight(True)
qtm = table.getModel()
inputPanel = JPanel()
inputPanel.add(JLabel('User'))
userMaskInput = JTextField('*ALL', 10)
inputPanel.add(userMaskInput)
inputPanel.add(JLabel('Out Queue'))
outQSelector = JComboBox(outQueues)
inputPanel.add(outQSelector)
loadButton = JButton('Load', actionPerformed=reloadTable)
inputPanel.add(loadButton)
menuBar = JMenuBar()
tools = JMenu('Tools')
copy = JMenuItem('Copy Spool File', actionPerformed=copySpooledFile)
tools.add(copy)
menuBar.add(tools)
topPanel = JPanel(GridLayout(3,1))
topPanel.add(menuBar)
topPanel.add(inputPanel)
l = JLabel('Hint: Hold down CTRL to select more than one spooled file.')
l.setHorizontalAlignment(JLabel.CENTER)
topPanel.add(l)
frame.add(topPanel, BorderLayout.NORTH)
frame.add(scrollPane, BorderLayout.CENTER)
#frame.add(JButton('Export', actionPerformed=exportAsPDF), BorderLayout.SOUTH)
frame.add(JButton('Export', actionPerformed=exportAsPDFWithText), BorderLayout.SOUTH)
frame.pack()
frame.setLocationRelativeTo(None)
frame.setVisible(True)