java调用本地打印机,绘制打印模板,小票模板

jar资源

网盘链接:https://pan.baidu.com/s/1fFvKpiwwva2gZl-WLfsY9A
密钥:q6wh

绘制打印模板

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.ArrayList;
import java.util.List;

/**
 * 模板
 * @author admin
 * 
 */
public class Prient implements Printable {
	// 菜品集合
	public static List<Test> testList = new ArrayList<Test>();

	// 设置小票打印
	public int print(Graphics g, PageFormat pf, int page)
			throws PrinterException {
		if (page > 0) {
			return NO_SUCH_PAGE;
		}
		Graphics2D g2d = (Graphics2D) g;
		// 设置颜色
		g2d.setColor(Color.BLACK);
		//模式  字体   字体大小
		g2d.setFont(new Font("Default", Font.PLAIN, 16));
		// 参数1:显示内容 参数2:横向偏移 参数3:纵向偏移
		g2d.drawString("点菜清单", 100, 50);
		g2d.drawString("------------------------------------------------", 40, 70);
		g2d.setFont(new Font("Default", Font.PLAIN, 12));
		g2d.drawString("点餐员:自定义", 40, 90);
		g2d.drawString("电话:自定义", 40, 110);
		g2d.drawString("用餐时间:自定义", 40, 130);
		g2d.drawString("用餐地址:打印测试", 40, 150);
		g2d.setFont(new Font("Default", Font.PLAIN, 16));
		g2d.drawString("------------------------------------------------", 40, 170);
		g2d.drawString("菜品             单价             小计", 40, 190);
		g2d.setFont(new Font("Default", Font.PLAIN, 12));
		int H1 = 190;
		double zmoney = 0;
		int count = 0;
		for (Test test : testList) {
			count = count + 1;
			H1 = H1 + 20;
			zmoney = test.getMoney() * test.getNum() + zmoney;
			g2d.drawString(count + "." + test.getName() + "(" + test.getNum()
					+ "份)     ¥" + test.getMoney()+"     ¥"+test.getMoney()*test.getNum(), 40, H1);
		}
		g2d.setFont(new Font("Default", Font.PLAIN, 16));
		g2d.drawString("------------------------------------------------", 40, H1 + 20);
		g2d.setFont(new Font("Default", Font.PLAIN, 12));
		g2d.drawString("合计:¥" + zmoney, 40, H1 + 40);
		g2d.drawString("优惠金额:¥" + 20, 40, H1 + 60);
		g2d.drawString("应收:¥" + (zmoney-20), 40, H1 + 80);
		g2d.drawString("实收:¥" + zmoney, 40, H1 + 100);
		g2d.drawString("找零:¥" + 20, 40, H1 + 120);
		g2d.drawString("收银员:" + "打印测试", 40, H1 + 140);
		g2d.drawString("谢谢惠顾,欢迎下次光临!", 80, H1 + 160);
		return PAGE_EXISTS;
	}
	public static List<Test> getTestList() {
		return testList;
	}
	public static void setTestList(List<Test> testList) {
		Prient.testList = testList;
	}
}

自定义商品对象

/**
 * 菜品对象
 * 
 * @author admin
 * 
 */
public class Test {
	// 菜品名称
	private String name;
	// 价格
	private double money;
	// 数量
	private Integer num;
	//菜品分类
	private String fenlei;

	public Test() {
		super();
	}
	public Test(String name, double money, Integer num) {
		super();
		this.name = name;
		this.money = money;
		this.num = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public Integer getNum() {
		return num;
	}
	public void setNum(Integer num) {
		this.num = num;
	}
	public String getFenlei() {
		return fenlei;
	}
	public void setFenlei(String fenlei) {
		this.fenlei = fenlei;
	}
}

调用打印机

import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.util.List;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;

/**
 * 打印测试类
 * 
 * @author admin
 * 
 */
public class MainTest {

	public static void main(String[] args) {
		//这里是测试用的数据,自己定义就好了
		List<Test> testList = new ArrayList<Test>();
		Test t1 = new Test("麻辣牛肉", 23.00, 2);
		Test t2 = new Test("麻辣牛肉", 23.00, 4);
		Test t3 = new Test("精品千层肚", 24.00, 3);
		Test t4 = new Test("麻辣牛肉", 23.00, 2);
		Test t5 = new Test("极品鲜毛肚", 26.00, 1);
		Test t6 = new Test("极品鲜毛肚", 26.00, 1);
		Test t7 = new Test("极品鲜毛肚", 26.00, 1);
		Test t8 = new Test("极品鲜毛肚", 26.00, 1);
		Test t9 = new Test("极品鲜毛肚", 26.00, 1);
		testList.add(t1);
		testList.add(t2);
		testList.add(t3);
		testList.add(t4);
		testList.add(t5);
		testList.add(t6);
		testList.add(t7);
		testList.add(t8);
		testList.add(t9);
		
		// 设置小票模型菜品集合
		Prient.setTestList(testList);
		// 定义纸张高度
		int height = 200 + (testList.size() * 20) + 160;
		// 通俗理解就是书、文档
		Book book = new Book();
		// 打印格式
		PageFormat pf = new PageFormat();
		// 原点在纸张的左上方,x 指向右方,y 指向下方。
		pf.setOrientation(PageFormat.PORTRAIT);
		// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
		Paper p = new Paper();
		// 页面宽度 页面高度
		p.setSize(260, height);
		// x轴 y轴 宽度 高度
		p.setImageableArea(0, 0, 260, height);
		pf.setPaper(p);
		// 把 PageFormat 和 Printable 添加到书中,组成一个页面
		book.append(new Prient(), pf);
		// 指定打印机打印(printerName打印机名称)
		HashAttributeSet hs = new HashAttributeSet();
		String printerName = "OneNote";
		hs.add(new PrinterName(printerName, null));
		PrintService[] pss = PrintServiceLookup.lookupPrintServices(null, hs);
		if (pss.length == 0) {
			System.out.println("无法找到打印机:" + printerName);
			return;
		}
		// 获取打印服务对象
		PrinterJob job = PrinterJob.getPrinterJob();
		// 写入书
		job.setPageable(book);
		try {
			Integer random6 = (int) ((Math.random() * 9 + 1) * 100000);
			System.out.println("Z"+System.currentTimeMillis()+random6.toString());
			// 添加指定的打印机
			job.setPrintService(pss[0]);
			// 打印执行
			job.print();
		} catch (PrinterException e) {
			System.out.println("================打印出现异常");
		}
	}
}

在这里插入图片描述

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
要使用Java调用打印机打印PDF模板,并向模板填充数据,你可以使用以下步骤: 1. 首先,你需要使用Java的PDF库(比如iText或PDFBox)来读取PDF模板。 2. 接下来,你需要将要填充的数据以某种格式(比如JSON或XML)存储在一个文件或字符串中。 3. 然后,你需要使用Java模板引擎(比如FreeMarker或Velocity)将数据填充到PDF模板中。 4. 最后,你需要使用Java打印API(比如Java Print Service或JavaFX Printing API)将填充后的PDF文档发送到打印机。 以下是一个简单的示例代码,说明如何使用iText和FreeMarker来填充PDF模板打印: ```java // 读取PDF模板 PdfDocument pdfDoc = new PdfDocument(new PdfReader("template.pdf"), new PdfWriter("output.pdf")); // 准备要填充的数据 Map<String, Object> data = new HashMap<>(); data.put("name", "John Smith"); data.put("age", 30); // 使用FreeMarker将数据填充到PDF模板中 Configuration cfg = new Configuration(Configuration.VERSION_2_3_30); cfg.setClassForTemplateLoading(getClass(), "/"); Template template = cfg.getTemplate("template.ftl"); StringWriter writer = new StringWriter(); template.process(data, writer); String filledTemplate = writer.toString(); // 将填充后的PDF文档发送到打印机 PrintService printer = PrintServiceLookup.lookupDefaultPrintService(); PdfDocument filledDoc = new PdfDocument(new PdfReader(new ByteArrayInputStream(filledTemplate.getBytes()))); DocPrintJob printJob = printer.createPrintJob(); printJob.print(new PDFDocumentAdapter(filledDoc)); ``` 注意,以上代码仅供参考,具体实现可能因环境和需求而异。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

22 岁糟老头子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值