IDEMPIERE-1083 Print shipping label applet is not working
This commit is contained in:
parent
cee1db4c65
commit
08b1b1b2cf
|
@ -0,0 +1,120 @@
|
|||
import java.awt.print.PrinterJob;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.print.Doc;
|
||||
import javax.print.DocFlavor;
|
||||
import javax.print.DocPrintJob;
|
||||
import javax.print.PrintService;
|
||||
import javax.print.SimpleDoc;
|
||||
import javax.print.attribute.HashDocAttributeSet;
|
||||
import javax.print.attribute.standard.DocumentName;
|
||||
import javax.swing.JApplet;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Low Heng Sin
|
||||
*
|
||||
*/
|
||||
public class PrintLabelApplet extends JApplet {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -3107445661561669767L;
|
||||
private ArrayList<String> filepaths = new ArrayList<String>();
|
||||
private int listSize = 0;
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
String s = getParameter("size");
|
||||
try {
|
||||
listSize = Integer.parseInt(s);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Invalid listSize param=" + s);
|
||||
listSize = 0;
|
||||
}
|
||||
|
||||
for (int i = 0; i < listSize; i++)
|
||||
filepaths.add(getParameter("file_" + i));
|
||||
|
||||
super.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println(this.getClass().getName() + " start()");
|
||||
PrinterJob pjob = null;
|
||||
PrintService service = null;
|
||||
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
|
||||
|
||||
try {
|
||||
int count = 0;
|
||||
|
||||
while (true) {
|
||||
URL url = new URL(getCodeBase(), "labeldata.jsp?filepath=" + filepaths.get(count) + "&count=" + (count + 1));
|
||||
System.out.println("Protocol="+url.getProtocol());
|
||||
System.out.println("Host="+url.getHost());
|
||||
System.out.println("Port="+url.getPort());
|
||||
System.out.println("Default Port="+url.getDefaultPort());
|
||||
System.out.println("Path"+url.getPath());
|
||||
System.out.println("URL="+url.toString());
|
||||
URLConnection conn = url.openConnection();
|
||||
int contentLength = conn.getContentLength();
|
||||
System.out.println("ContentLength="+contentLength);
|
||||
InputStream is = conn.getInputStream();
|
||||
System.out.println("InputStream="+is.available());
|
||||
byte[] data = null;
|
||||
BufferedInputStream bis = new BufferedInputStream(is);
|
||||
System.out.println("BufferedInputStream="+bis.available());
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
while(bis.available() > 0) {
|
||||
baos.write(bis.read());
|
||||
}
|
||||
is.close();
|
||||
bis.close();
|
||||
data = baos.toByteArray();
|
||||
baos.close();
|
||||
System.out.println("ByteArrayOutputStream="+data.length);
|
||||
if (data.length > 0) {
|
||||
count ++;
|
||||
if (pjob == null) {
|
||||
// Create Print Job
|
||||
pjob = PrinterJob.getPrinterJob();
|
||||
if (pjob.printDialog())
|
||||
service = pjob.getPrintService();
|
||||
else
|
||||
break;
|
||||
}
|
||||
DocPrintJob job = service.createPrintJob();
|
||||
HashDocAttributeSet as = new HashDocAttributeSet();
|
||||
as.add(new DocumentName("shipping label", null));
|
||||
Doc doc = new SimpleDoc(data, flavor, as);
|
||||
job.print(doc, null);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
if (count >= listSize)
|
||||
break;
|
||||
}
|
||||
this.showStatus(count + " label printed.");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
this.showStatus("Failed to print label - " + e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
System.out.println(this.getClass().getName() + " destroy()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
System.out.println(this.getClass().getName() + " stop()");
|
||||
}
|
||||
}
|
|
@ -13,20 +13,16 @@
|
|||
*****************************************************************************/
|
||||
package org.adempiere.webui;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.adempiere.webui.component.ToolBarButton;
|
||||
import org.adempiere.webui.component.Window;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.zkoss.zk.au.out.AuScript;
|
||||
import org.zkoss.zk.ui.Executions;
|
||||
import org.zkoss.zk.ui.event.Event;
|
||||
import org.zkoss.zk.ui.event.EventListener;
|
||||
import org.zkoss.zk.ui.event.Events;
|
||||
import org.zkoss.zk.ui.util.Clients;
|
||||
import org.zkoss.zul.Applet;
|
||||
import org.zkoss.zul.Div;
|
||||
|
||||
/**
|
||||
|
@ -40,37 +36,47 @@ public class LabelAppletWindow extends Window implements EventListener<Event>
|
|||
*
|
||||
*/
|
||||
private static final long serialVersionUID = -8980224912104404397L;
|
||||
|
||||
|
||||
public LabelAppletWindow(List<byte[]> list)
|
||||
{
|
||||
super();
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
HttpSession session = (HttpSession) Executions.getCurrent().getDesktop().getSession().getNativeSession();
|
||||
session.setAttribute(uuid, list);
|
||||
|
||||
Div div = new Div();
|
||||
appendChild(div);
|
||||
|
||||
Applet applet = new Applet();
|
||||
applet.setCode("PrintLabelApplet.class");
|
||||
applet.setArchive("labelapplet.jar");
|
||||
applet.setWidth("0");
|
||||
applet.setHeight("0");
|
||||
applet.setParam("size", list.size() + "");
|
||||
|
||||
File tempFile = null;
|
||||
FileOutputStream fos = null;
|
||||
for(int i = 0; i < list.size(); i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
tempFile = File.createTempFile("lblapp", Long.toString(System.nanoTime()));
|
||||
fos = new FileOutputStream(tempFile);
|
||||
applet.setParam("file_" + i, tempFile.getAbsolutePath());
|
||||
fos.write(list.get(i));
|
||||
fos.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
div.appendChild(applet);
|
||||
|
||||
ToolBarButton link = new ToolBarButton();
|
||||
link.setLabel("Click here to close this popup after printing is completed.");
|
||||
link.addEventListener(Events.ON_CLICK, this);
|
||||
appendChild(link);
|
||||
|
||||
this.setBorder("normal");
|
||||
|
||||
StringBuffer appletTag = new StringBuffer();
|
||||
appletTag.append("<applet code='PrintLabelApplet.class' archive='labelapplet.jar' width=0 height=0>");
|
||||
appletTag.append("<param name='key' value='" + uuid + "'>");
|
||||
appletTag.append("<param name='size' value='" + list.size() + "'>");
|
||||
|
||||
for(int i = 0; i < list.size(); i++)
|
||||
appletTag.append("<param name='data_" + i + "' value='" + Base64.encodeBase64(list.get(i)) + "'>");
|
||||
|
||||
appletTag.append("</applet>");
|
||||
|
||||
String script = "document.getElementById('" + div.getUuid() + "').innerHTML=\"" + appletTag.toString() + "\";";
|
||||
Clients.response(new AuScript(div, script));
|
||||
this.setBorder("normal");
|
||||
}
|
||||
|
||||
|
||||
public void onEvent(Event event) throws Exception
|
||||
{
|
||||
if (Events.ON_CLICK.equals(event.getName()))
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
<project name="labelapplet" basedir="." default="sign">
|
||||
<target name="build">
|
||||
<mkdir dir="bin"/>
|
||||
<javac srcdir="WEB-INF/src"
|
||||
destdir="bin"
|
||||
debug="on"
|
||||
listfiles="yes"
|
||||
excludes="**/fi/**, **/metainfo/**, **/org/**, **/web/**"/>
|
||||
</target>
|
||||
|
||||
<target name="jar" depends="build">
|
||||
<jar destfile="labelapplet.jar" basedir="bin">
|
||||
<manifest>
|
||||
<attribute name="Built-By" value="Trek Global"/>
|
||||
</manifest>
|
||||
</jar>
|
||||
</target>
|
||||
|
||||
<target name="sign" depends="jar">
|
||||
<signjar alias="labelapplet"
|
||||
storepass="labelapplet"
|
||||
jar="labelapplet.jar"
|
||||
keystore="labelappletkeystore"/>
|
||||
</target>
|
||||
</project>
|
Binary file not shown.
|
@ -1,29 +1,52 @@
|
|||
<%@ page trimDirectiveWhitespaces="true" %>
|
||||
<%@page import="java.io.ByteArrayOutputStream"%>
|
||||
<%@page import="java.io.FileInputStream"%>
|
||||
<%@page import="java.io.File"%>
|
||||
<%@page import="java.io.BufferedOutputStream"%>
|
||||
<%@page import="java.io.OutputStream"%>
|
||||
<%@ page language="java" contentType="application/octet-stream"%>
|
||||
<%
|
||||
String key = request.getParameter("key");
|
||||
if (key == null || key.trim().length() == 0 )
|
||||
String filepath = request.getParameter("filepath");
|
||||
if (filepath == null || filepath.trim().length() == 0 )
|
||||
return;
|
||||
|
||||
Object object = session.getAttribute(key);
|
||||
if (object != null && object instanceof java.util.List) {
|
||||
java.util.List list = (java.util.List)object;
|
||||
System.out.println("size=" + list.size());
|
||||
if (!list.isEmpty()) {
|
||||
byte[] data = (byte[])list.remove(0);
|
||||
System.out.println("length=" + data.length);
|
||||
File file = new File(filepath);
|
||||
if (file.exists())
|
||||
{
|
||||
FileInputStream fis = null;
|
||||
ByteArrayOutputStream baos = null;
|
||||
byte[] data = null;
|
||||
try
|
||||
{
|
||||
fis = new FileInputStream (file);
|
||||
baos = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024*8]; // 8kB
|
||||
int length = -1;
|
||||
while ((length = fis.read(buffer)) != -1)
|
||||
baos.write(buffer, 0, length);
|
||||
data = baos.toByteArray();
|
||||
fis.close();
|
||||
baos.close();
|
||||
|
||||
response.setContentLength(data.length);
|
||||
|
||||
OutputStream os = response.getOutputStream();
|
||||
BufferedOutputStream bos = new BufferedOutputStream(os);
|
||||
bos.write(data);
|
||||
bos.flush();
|
||||
} else {
|
||||
bos.close();
|
||||
|
||||
file.delete();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
response.setContentLength(0);
|
||||
}
|
||||
} else {
|
||||
System.out.println("key not found=" + key);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("file not found=" + filepath);
|
||||
response.setContentLength(0);
|
||||
}
|
||||
%>
|
||||
|
|
Loading…
Reference in New Issue