批量打印二维码

后端
/**
     * 批量下载学生普查二维码
     */
    @Override
    public void batchDownQrCode(List<SchoolSurveyStudentInfoExp> list, HttpServletResponse response) throws IOException {
        byte[] pdfBytes = generatePdf(list);
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "inline; filename=student_qr.pdf");
        response.getOutputStream().write(pdfBytes);
        response.getOutputStream().flush();
    }
    /**
     * 批量生成学生二维码
     * @param studentList
     * @return
     * @throws IOException
     */
    public static byte[] generatePdf(List<SchoolSurveyStudentInfoExp> studentList) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer = new PdfWriter(baos);
        PdfDocument pdfDoc = new PdfDocument(writer);
        Document document = new Document(pdfDoc);

        // 加载中文字体
        PdfFont font = null;
        try{
            //window系统获取的路径
            font = PdfFontFactory.createFont("D:\\IdeaProjects\\dev_product\\03_Dev\\01_GlassesManagementSystemSpringBoot\\GlassesManagementSystem\\src\\main\\resources\\fonts\\msyh.ttc,1", PdfEncodings.IDENTITY_H, false);
            //linux获取字体路径
            //font = PdfFontFactory.createFont("classpath:fonts/msyh.ttc,1", PdfEncodings.IDENTITY_H, false);
            //这个字体可以使用,缺点是有的字体识别不了例如  凉   𫍽
            //font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", true);
        } catch (Exception e) {
            log.error("Failed to load font", e);
            // 使用默认字体
            font = PdfFontFactory.createFont();
        }

        int count = 0;
        Table table = new Table(4); // 每行4个学生信息
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);
        table.setBorder(NO_BORDER); // 去掉表格边框
        table.setKeepTogether(false); // 允许表格分页
        table.setBorderCollapse(BorderCollapsePropertyValue.SEPARATE); // 确保表格边框不会合并

        for (SchoolSurveyStudentInfoExp student : studentList) {
            // Create QR Code
            String qrText = student.getStudentName() + "," + student.getStudentId() + "#" + student.getBatchId() + "," + student.getStudentGender() + "," + student.getStudentAge() + "," + student.getStudentMobile() ;
            // 或尝试强制指定编码(如果库支持)
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BarcodeQRCode qrCode = new BarcodeQRCode(qrText, hints);
            PdfFormXObject qrFormXObject = qrCode.createFormXObject(ColorConstants.BLACK, pdfDoc);
            Image qrImage = new Image(qrFormXObject);
            qrImage.scaleToFit(120, 120); // 放大二维码图片

            // Create Student Information
            Paragraph studentName = new Paragraph((student.getStudentName())+" "+(student.getStudentGender().equals("1") ? "男" : "女")).setFont(font).setTextAlignment(TextAlignment.CENTER);
            Paragraph className = new Paragraph( student.getGradeName() + "/" + student.getClassName()).setFont(font).setTextAlignment(TextAlignment.CENTER);
            Paragraph createTime = new Paragraph(String.valueOf(java.time.LocalDate.now())).setTextAlignment(TextAlignment.CENTER);

            // Combine QR Code and Student Information
            Cell cell = new Cell().add(qrImage).add(studentName).add(className).add(createTime);
            cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
            cell.setTextAlignment(TextAlignment.CENTER);
            cell.setBorder(NO_BORDER); // 去掉单元格边框
            table.addCell(cell);

            count++;
            if (count % 16 == 0) {
                // 每16个学生信息完成一页
                document.add(table);
                table = new Table(4); // 重新创建一个新的表格
                table.setHorizontalAlignment(HorizontalAlignment.CENTER);
                table.setBorder(NO_BORDER); // 去掉表格边框
                table.setKeepTogether(false); // 允许表格分页
                table.setBorderCollapse(BorderCollapsePropertyValue.SEPARATE); // 确保表格边框不会合并
            }
        }
        // 添加剩余的学生信息
        if (count % 16 != 0) {
            document.add(table);
        }
        document.close();
        byte[] pdfBytes = baos.toByteArray();
        log.info("Generated PDF size: {}", pdfBytes.length); // 添加日志记录 PDF 数据大小
        return pdfBytes;
    }

前端请求

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class='layui-form' lay-filter='print_form' id="print_form">
                <script type="text/html" template lay-done="layui.data.done(d)">
               </script>
                <embed id="pdfEmbed" type="application/pdf" width="100%" height="730">
            </form>
        </div>
    </div>
    <div style="text-align: center;margin-top: 2%;">
        <button class="layui-btn layui-btn-normal printHide"  lay-submit lay-filter="cancel">关闭</button>
    </div>
</div>
<script>
    layui.data.done = function(d) {
        layui.use(['view', 'element', 'form', 'layarea','laydate','qrcode'], async function () {
            console.log(d)
            var $ = layui.$,
                form = layui.form,
                element = layui.element,
                qrcode = layui.qrcode,
                layer = layui.layer,
                admin = layui.admin,
                view = layui.view;
            let  access_token = JSON.parse(localStorage.getItem('layuiAdmin')).access_token
            // 使用 fetch 发送请求
            fetch(layui.setter.requestHost + '/surveyStudentInfo/batchDownQrCode', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                     access_token
                },
                body: JSON.stringify(d.params)
            })
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok ' + response.statusText);
                }
                return response.blob();
            })
            .then(blob => {
                const blobUrl = URL.createObjectURL(blob);
                console.log('Blob URL:', blobUrl);
                document.getElementById('pdfEmbed').src = blobUrl;
            })
            .catch(error => {
                console.error('There was a problem with the fetch operation:', error);
                layer.msg('请求失败', {icon: 2, offset: "auto"});
            });
            // 画面初期化
            form.render(null, 'print_form');
            //打印
            form.on('submit(salePrint)', function () {
                printPageUtil.printPage('print_form');
                return false;
            });
            form.on('submit(cancel)', function (result) {
                //关闭当前页面
                layer.closeAll();
            });
        });
    }
</script>
<style>
    .qr-container {
        display: flex;
        flex-wrap: wrap;
        page-break-inside: avoid;
    }
    .qr-parent {
        display: flex;
        flex-direction: column;
        align-items: center;
    }
    .qr-code {
        width: 174px;
        height: 174px;
    }
    .qr-info {
        font-size: 12px;
        text-align: center;
    }
    @media print {
        body * {
            visibility: hidden;
        }
        .qr-container, .qr-container * {
            visibility: visible;
        }
        .qr-parent {
            /*display: flex;
            flex-direction: column;*/
            text-align: center;
        }
        .qr-info {
            font-size: 12px;
            text-align: center;
        }
    }
</style>

 

posted on 2025-04-09 10:15  IT-QI  阅读(15)  评论(0)    收藏  举报