使用Java实现一个面单打印插件

为什么写这个面单打印插件

做电商的都知道,每次收到订单需要打印面单的时候,通过发起一个后端请求,拿到面单地址,在前端页面进行展示,然后在点击打印操作按钮,选择打印机 点击打印,这一套流程下来,比较繁琐。因为面单压根就不需要预览,打印出来再预览再配货不是跟方便么?所以就开始往这方面思考了,甚至还可以做到自动打印,而不用手工操作,这样只要一出面单就知道有客户下单了,是不是很方便快捷,就像现在美团点外卖,饭店出单一样。基于这样,我就开始写代码。
我主要开发使用的语言是PHP,用PHP来实现这个,还是比较麻烦,然后现在也接触了Java一段时间,索性就用Java实现吧

框架用的是Springboot

话不多说,直接上代码

下面展示一些 内联代码片

java 复制代码
@GetMapping("/printpdf")
    public @ResponseBody ApiResponse  printPDF(String file_name)
    {
        String currentPath = System.getProperty("user.dir");
        String directoryPath = currentPath+"/static/"; // 目录路径
        String pdf = directoryPath+file_name;
        // 选择要打印的文档文件
        File documentFile = new File(pdf);
        // 获取默认的打印机
        PrintService defaultPrinter = PrintServiceLookup.lookupDefaultPrintService();
        // 创建打印任务
        DocPrintJob printJob = defaultPrinter.createPrintJob();


        // 打印文档
        FileInputStream inputStream;
        try {
            // 加载PDF文件
            PDDocument document = PDDocument.load(new File(pdf));
            // 创建打印任务
            PrinterJob job = PrinterJob.getPrinterJob();
            // 设置打印内容
            job.setPageable(new PDFPageable(document));
            // 可选:设置打印属性,如纸张大小、打印方向等
            PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
            attributes.add(OrientationRequested.PORTRAIT);
            // 执行打印任务
            job.print(attributes);
            // 关闭文档
            document.close();
            System.out.println("PDF文件已成功打印。");

           /* inputStream = new FileInputStream(documentFile);
            Doc document = new SimpleDoc(inputStream, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
            printJob.print(document, attributes);
            System.out.println(document.getPrintData());
            inputStream.close();*/
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        ApiResponse response = new ApiResponse();
        response.setCode("S000");
        response.setMessage("打印成功");
        response.setData(new ArrayList<>());
        return response;
    }
这里要用到的依赖
xml 复制代码
 		<dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.30</version>
        </dependency>
最后

通过get请求printpdf这个方法,就可以自动打印了,不需要预览

相关推荐
海绵宝宝贾克斯儿41 分钟前
C++中如何实现一个单例模式?
开发语言·c++·单例模式
史迪仔011242 分钟前
[python] Python单例模式:__new__与线程安全解析
开发语言·python·单例模式
苍煜1 小时前
Maven构建流程详解:如何正确管理微服务间的依赖关系-当依赖的模块更新后,我应该如何重新构建主项目
java·微服务·maven
冼紫菜1 小时前
[特殊字符]CentOS 7.6 安装 JDK 11(适配国内服务器环境)
java·linux·服务器·后端·centos
isyangli_blog1 小时前
(1-4)Java Object类、Final、注解、设计模式、抽象类、接口、内部类
java·开发语言
三块钱07941 小时前
【原创】基于视觉大模型gemma-3-4b实现短视频自动识别内容并生成解说文案
开发语言·python·音视频
易只轻松熊1 小时前
C++(20): 文件输入输出库 —— <fstream>
开发语言·c++·算法
芯眼1 小时前
ALIENTEK精英STM32F103开发板 实验0测试程序详解
开发语言·c++·stm32·单片机·嵌入式硬件·社交电子
青出于兰2 小时前
C语言| 指针变量的定义
c语言·开发语言
玉笥寻珍2 小时前
筑牢信息安全防线:涉密计算机与互联网隔离的理论实践与风险防控
开发语言·计算机网络·安全·计算机外设·php·安全架构·安全性测试