java applet 打印_关于web页面JApplet打印小票

importnet.sourceforge.jbarcodebean.JBarcodeBean;importnet.sourceforge.jbarcodebean.model.Code39;importnet.sourceforge.jbarcodebean.model.Ean13;importorg.json.JSONArray;importorg.json.JSONObject;importjavax.print.PrintService;importjavax.print.PrintServiceLookup;import javax.swing.*;import java.awt.*;importjava.awt.image.BufferedImage;import java.awt.print.*;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.net.URL;importjava.net.URLConnection;importjava.text.SimpleDateFormat;importjava.util.Date;/*** Created by Administrator on 2018/4/29.*/

public class JRPrinterApplet extends JApplet implementsPrintable {

JSONObject jsonObject=newJSONObject();private int imageWidth = 300; //图片的宽度

@Overridepublic voidinit() {

String req_path=this.getParameter("req_path");

String print_type=this.getParameter("print_type");

String print_name=this.getParameter("print_name");

String jsonStr=this.getParameter("json");//链接测速

if(print_type.equals("login_test")){

System.out.println("****连接测试");

String get=urlGet(req_path+"/print/print_error?type=login_test");

System.out.println(get);return;

}else if(print_type.equals("print")){

System.out.println("****连接打印");

jsonObject=newJSONObject(jsonStr);

String id=String.valueOf(jsonObject.get("id"));

System.out.println(print_name+" "+req_path+" "+id+" "+print_type+"\n"+jsonStr);

Book book= newBook();//设置成竖打

PageFormat pf = newPageFormat();

pf.setOrientation(PageFormat.PORTRAIT);//LANDSCAPE表示竖打;PORTRAIT表示横打;REVERSE_LANDSCAPE表示打印空白//通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。

Paper p = newPaper();

p.setSize(imageWidth,300); //纸张大小(590, 840)表示A4纸

p.setImageableArea(0, 0, imageWidth, 300); //A4(595 X842)设置打印区域,其实0,0应该是72,72//,因为A4纸的默认X,Y边距是72

pf.setPaper(p);//把 PageFormat 和 Printable 添加到书中,组成一个页面

book.append(this, pf);//获取打印服务对象

PrinterJob.lookupPrintServices();

PrinterJob job=PrinterJob.getPrinterJob();//设置打印类

job.setPageable(book);

PrintService[] pss= PrintServiceLookup.lookupPrintServices(null,null);//PrintService ps=null;

boolean have_ps=false;for(PrintService printService:pss){

System.out.println("****找到打印机:"+printService.toString());if(printService.toString().contains(print_name)){try{

System.out.println("----找到对应打印机:"+printService.toString());

job.setPrintService(printService);

have_ps=true;

}catch(PrinterException e) {

String services= urlGet(req_path + "/print/print_error?type=Doc_error&id=" + id + "&msg=" +e);

System.out.println(services);

e.printStackTrace();return;

}

}

}if(!have_ps){

String var19= urlGet(req_path + "/print/print_error?type=print_no_find&id=" +id);

System.out.println(var19);

}else{try{//可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印

/*boolean a=job.printDialog();

if(a)

{

job.print();

}*/String get=urlGet(req_path + "/print/print_error?type=print_ok&id=" +id);

System.out.println(get);

job.print();

}catch(PrinterException e) {

String get= urlGet(req_path + "/print/print_error?type=print_is_error&id=" + id + "&msg=" +e);

System.out.println(get);

e.printStackTrace();

}

}

}

}public int print(Graphics gra, PageFormat pf, int pageIndex) throwsPrinterException {

Component c= null;

Graphics2D g2=(Graphics2D) gra;double x =pf.getImageableX();double y =pf.getImageableY();switch(pageIndex) {case 0:

g2.drawImage(graphicsGeneration(jsonObject), (int) x, (int) y, c);returnPAGE_EXISTS;default:returnNO_SUCH_PAGE;

}

}publicBufferedImage graphicsGeneration(JSONObject jsonObject) {

String id=String.valueOf(jsonObject.get("id"));

String name=String.valueOf(jsonObject.get("name"));

JSONArray array=jsonObject.getJSONArray("list");int title_h=20;int left_long=10;int row=1;inti;int imageHeight=title_h*(row+7)+80;

BufferedImage image= newBufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);//设置图片的背景色

Graphics2D main =image.createGraphics();

main.setColor(Color.white);

main.fillRect(0, 0, imageWidth, imageHeight);//***********************页面头部

Graphics g =image.createGraphics();

g.fillRect(0, 0, imageWidth, title_h);

g.setColor(Color.white);

Font font= new Font("宋体", Font.BOLD, 14);

g.setFont(font);

g.setColor(Color.BLACK);

g.drawString("***道口义兴张祥记烧鸡老铺***", 50, title_h*row+++3);//其他内容

font = new Font("宋体", Font.PLAIN, 12);

g.setFont(font);

g.drawString("单号:"+id,left_long,title_h*row);

g.drawString("时间:"+new SimpleDateFormat("MM月dd日 HH:mm:ss").format(new Date()),imageWidth/2,title_h*row++);

g.drawString("收货人:"+name,left_long,title_h*row++);

g.drawLine(5,65,295,65);for(i=0;i

JSONObject o=(JSONObject) array.get(i);

g.drawString((i+1)+".",left_long,title_h*row);

g.drawString(o.get("name")+" x"+o.get("num"),left_long+15,title_h*row);

String unit="";if(o.get("unit")!=null&&!o.get("unit").equals("")){

unit="/"+o.get("unit");

}

g.drawString(o.get("money")+unit,240,title_h*row);

row++;

}//生成条维码

JBarcodeBean jBarcodeBean = newJBarcodeBean();//条形码类型//jBarcodeBean.setCodeType(new Ean13());

jBarcodeBean.setCodeType(newCode39());//在条形码下面显示文字

jBarcodeBean.setLabelPosition(JBarcodeBean.LABEL_BOTTOM);

jBarcodeBean.setCode(id);

jBarcodeBean.setBarcodeHeight(50);//条维码

BufferedImage tiao_wei_ma = new BufferedImage(300, 80, BufferedImage.TYPE_INT_RGB);

tiao_wei_ma=jBarcodeBean.draw(tiao_wei_ma);//***********************插入图片

Graphics mainPic =image.getGraphics();if(tiao_wei_ma!=null){

mainPic.drawImage(tiao_wei_ma,0,title_h*(row-1),null);

mainPic.dispose();

}//createImage("F:\\temp/hehe.jpg",image);

returnimage;

}/*** 向指定URL发送GET方法的请求

**/

public staticString urlGet(String url) {

BufferedReader in= null;try{

URL realUrl= newURL(url);//打开和URL之间的连接

URLConnection connection =realUrl.openConnection();//设置通用的请求属性

connection.setRequestProperty("accept", "*/*");

connection.setRequestProperty("connection", "Keep-Alive");

connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

connection.setConnectTimeout(5000);

connection.setReadTimeout(5000);//建立实际的连接

connection.connect();//定义 BufferedReader输入流来读取URL的响应

in = new BufferedReader(newInputStreamReader(connection.getInputStream()));

StringBuffer sb= newStringBuffer();

String line;while ((line = in.readLine()) != null) {

sb.append(line);

}returnsb.toString();

}catch(Exception e) {

System.out.println(e);

}//使用finally块来关闭输入流

finally{try{if (in != null) {

in.close();

}

}catch(Exception e2) {

e2.printStackTrace();

}

}return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值