HeavenTang

导航

java 在服务器生成pdf文件再上传

点击查看代码

    public void printNlqPdf(AdmissionSubmitEntity admissionSubmitEntity, HttpServletResponse response, SystemUserInfo user) {
        String yxdm = admissionSubmitEntity.getCommitCollege();
        String nd = admissionSubmitEntity.getCommitYear();
        YzyNlqDto nlqDto = new YzyNlqDto();
        nlqDto.setYxdm(yxdm);
        nlqDto.setNd(nd);
        List<YzyNlqVo> yzyNlqVoList = yzyNlqMapper.queryYzyNlqPdf(nlqDto);
        if (CollectionUtils.isEmpty(yzyNlqVoList)) {
            throw new ServiceException(400, "该学院没有数据");
        }

        YzyNlqDto yzyNlqDto = new YzyNlqDto();
        yzyNlqDto.setNd(nd);
        yzyNlqDto.setYxdm(yxdm);
        yzyNlqDto.setNlq("1");
        if (StringUtils.isBlank(nd)) {
            throw new ServiceException(500, "年度数据不能为空!");
        }
        // 查询复试小项成绩信息
        Map<String, List<YzyNlqInfoVo>> nlqInfoVoMap = yzyNlqService.getNlqInfoVoMap(yzyNlqDto);
        // 查询计算规则
        Map<String, Cjjsgz> cjjsgzMap = yzyNlqService.getCjjsgzMap(nd);
        // 查询复试科目信息
        Map<String, List<Fsxxkm>> fsxxkmMap = yzyNlqService.getFsxxkmMap(nd);
        // 查询当前参数筛选出考生相关信息
        List<YzyNlqVo> yzyNlqVoLists = yzyNlqMapper.queryYzyNlq(yzyNlqDto);
        // 通过复试小项成绩信息、复试科目信息、总成绩计算规则、给考生信息组装成绩相关数据
        yzyNlqVoLists = yzyNlqService.handleNlqVoData(yzyNlqVoLists, nlqInfoVoMap, cjjsgzMap, fsxxkmMap);

        List<YzyNlqInfoVo> lists = new ArrayList<>();
        for (YzyNlqVo l : yzyNlqVoLists) {
            List<YzyNlqInfoVo> list = l.getYzyNlqInfoVoList();
            if (CollectionUtils.isNotEmpty(list)) {
                lists.addAll(list);
            }
        }
        File file = null;
        FileOutputStream fileOutputStream = null;
        OutputStream out = null;
        FileInputStream fileInputStream = null;
        BufferedInputStream bufferedInputStream = null;
        try {
            List<Map<String, String>> mapList = new ArrayList<>();

            for (YzyNlqVo maps : yzyNlqVoList) {
                Map<String, String> map = new HashMap<>();
                map = org.apache.commons.beanutils.BeanUtils.describe(maps);
                if (maps.getZydm() != null && maps.getZymc() != null) {
                    map.put("zymc", maps.getZydm() + maps.getZymc());
                } else {
                    map.put("zymc", maps.getZymc());
                }
                if (maps.getBkzydm() != null && maps.getBkzy() != null) {
                    map.put("bkzy", maps.getBkzydm() + maps.getBkzy());
                } else {
                    map.put("bkzy", maps.getBkzy());
                }
                mapList.add(map);
            }
            KgszDto kgszDto = new KgszDto();
//            String nd = kgszMapperExt.queryNd(kgszDto);
            String headerName = admissionSubmitEntity.getCommitYear() + "_" + admissionSubmitEntity.getCommitCollege() + "一志愿拟录取名单";

            String realPath = request.getSession().getServletContext().getRealPath("");
            String filePath = realPath + "/" + headerName + ".pdf";
            fileOutputStream = new FileOutputStream(filePath);


            createNlqPdf(mapList, lists, yzyNlqDto, headerName, response, fileOutputStream);

            // 删除临时存储磁盘的的文件
            file = new File(filePath);
            fileInputStream = new FileInputStream(file);
//            MultipartFile multipartFile = this.fileConvertToMultipartFile(file);
            MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                    ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
            // 上传到fastdf服务器,返回group信息

//            String fileId = null;
            try {
                long fileSize = multipartFile.getSize();
                if (fileSize > 209715200) { // 1024*1024*200M
                    throw new EducationException("文件大小不能超过200M");
                }
                StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getBytes(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()));
                if (org.apache.commons.lang.StringUtils.isBlank(storePath.getFullPath())) {
                    throw new EducationException("上传文件失败");
                }

                AttachmentDTO dto = new AttachmentDTO();
                dto.setBelongsTable("T_ZSGL_SSNLQTJ");
                dto.setDataId(admissionSubmitEntity.getCommitYear() + "_" + admissionSubmitEntity.getCommitCollege());
                dto.setFileId(storePath.getFullPath());
                dto.setFileName(headerName + ".pdf");
                dto.setFileSize(fileSize + "");
                dto.setFileType(multipartFile.getContentType());
                dto.setCreator(user.getUserId());
                dto.setAttachmentId(uuIDStringGenerator.nextUUID());
                admissionSubmitMapper.insertFileInfo(dto);
            } catch (Exception e) {
                log.error(e.getMessage());
                e.printStackTrace();
            }

        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        } finally {
            if (file != null) {
                if (file.exists()) {
                    boolean delete = file.delete();
                    if (!delete) {
                        log.error("context", file.getName() + "删除失败");
                    }
                }
            }
            try {
                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public void createNlqPdf(List<Map<String, String>> mapList, List<YzyNlqInfoVo> lists, YzyNlqDto yzyNlqDto, String headerName, HttpServletResponse response, FileOutputStream out) throws IOException, DocumentException {
        try {
            //设置中文
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
            // 创建pdf
            Document document = new Document(PageSize.A4.rotate());
//            PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());

            PdfWriter writer = PdfWriter.getInstance(document, out);
            document.open();

            BaseFont headerBase = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font headerFont = new Font(headerBase, 14, Font.BOLD);
            // 查询复试小项信息
            FskmszListDto fskmszListDto = new FskmszListDto();
            fskmszListDto.setYxdm(yzyNlqDto.getYxdm());
            fskmszListDto.setZydm(yzyNlqDto.getZydm());
            List<FsxxkmVo> fsxxList = fsxxkmMapper.queryMinterm(fskmszListDto)
                    .stream().collect(Collectors
                            .collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(FsxxkmVo::getKmmc))), ArrayList::new));
            // 头部表格文字
            PdfPCell header = new PdfPCell(new Paragraph(headerName, headerFont));
            if (fsxxList.size() == MagicNumber.ZERO) {
                header.setColspan(17);
            } else {
                header.setColspan(17 + fsxxList.size());
            }
            header.setPaddingTop(20f);
            header.setPaddingBottom(10f);
            // 水平、垂直居中
            header.setHorizontalAlignment(Element.ALIGN_CENTER);
            header.setVerticalAlignment(Element.ALIGN_MIDDLE);
            // 无边框
            header.setBorderWidthTop(0);
            header.setBorderWidthRight(0);
            header.setBorderWidthBottom(0);
            header.setBorderWidthLeft(0);

            // 头部表格
            PdfPTable table = new PdfPTable(19 + fsxxList.size());
            // 宽度100%
            table.setWidthPercentage(100);
            table.setSpacingBefore(20f);
            table.setSpacingAfter(3f);

            // 添加头部表格
            table.addCell(header);
            List<String> keyList = new ArrayList<String>() {{
                this.add("zymc");
                this.add("ksbh");
                this.add("xm");
                this.add("xb");
                this.add("csny");
                this.add("xs");
                this.add("bkzy");
                this.add("xxfs");
                this.add("cszf");
                this.add("jffz");
                if (fsxxList.size() != MagicNumber.ZERO) {
                    for (FsxxkmVo list : fsxxList) {
                        this.add(list.getId());
                    }
                }
                this.add("fscj");
                this.add("zcj");
                this.add("yydj");
                this.add("zzmm");
                this.add("byxx");
                this.add("byzy");
                this.add("byny");
                this.add("yddh");
                this.add("dzxx");
            }};
            List<String> valueList = new ArrayList<String>() {{
                this.add("复试专业");
                this.add("准考证号");
                this.add("姓名");
                this.add("性别");
                this.add("出生年月");
                this.add("学院");
                this.add("报考专业");
                this.add("学习方式");
                this.add("初试总分");
                this.add("照顾政策加分");
                if (fsxxList.size() != MagicNumber.ZERO) {
                    for (FsxxkmVo list : fsxxList) {
                        this.add(list.getKmmc());
                    }
                }
                this.add("复试成绩");
                this.add("总成绩");
                this.add("英语等级");
                this.add("政治面貌");
                this.add("毕业学校");
                this.add("毕业专业");
                this.add("毕业年月");
                this.add("移动电话");
                this.add("电子信箱");
            }};

            List<PdfPRow> list = table.getRows();
            String[] keyArrayList = keyList.toArray(new String[keyList.size()]);
            String[] valueArrayList = valueList.toArray(new String[valueList.size()]);
            PdfUtils.setRowHead(list, FontChinese, valueArrayList);
            Map<String, List<YzyNlqInfoVo>> cjxxMap = lists.stream().collect(Collectors.groupingBy(YzyNlqInfoVo::getKsbh));
            for (Map<String, String> map : mapList) {
                String ksbh = map.get("ksbh");
                if (cjxxMap.containsKey(ksbh)) {
                    cjxxMap.get(ksbh).forEach(e -> {
                        map.put(e.getFsxxkmId(), e.getFs());
                    });
                }
                PdfUtils.setRowKeyData(list, FontChinese, map, keyArrayList);
            }
            document.add(table);
            document.close();
            writer.close();
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
    }

posted on 2026-03-31 17:59  HeavenTang  阅读(9)  评论(0)    收藏  举报