java导出pdf模板(模板是pdf的)

package org.jeecg.modules.system.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.demo.st.entity.StAwards;
import org.jeecg.modules.demo.st.entity.StScholarships;
import org.jeecg.modules.demo.st.entity.StStudent;
import org.jeecg.modules.demo.st.service.IStClassService;
import org.jeecg.modules.demo.st.utils.PdfUtils;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysDictService;
import reactor.core.publisher.Flux;

import java.util.*;

public class GetStuPdf {
    public static void getPdf(StStudent stStudent,
                              ISysDictService sysDictService,
                              IStClassService stClassService,
                              List<StAwards> stAwards,
                              StScholarships stScholarships,
                              ISysDepartService sysDepartService) {

        Map<String, String> map = new HashMap();
        map.put("college", sysDepartService.queryByIdCollges(stStudent.getCollege()));
        map.put("student_id", stStudent.getStudentId());
        map.put("st_name", stStudent.getStName());
        map.put("sex", sysDictService.queryDictTextByKey("sex", stStudent.getSex()));
        map.put("face", sysDictService.queryDictTextByKey("face", stStudent.getFace()));
        map.put("nationality", sysDictService.queryDictTextByKey("nation", stStudent.getNationality()));
        map.put("st_class", stClassService.queryByIdClas(stStudent.getStClass()));
        map.put("phone", stStudent.getPhone());
        map.put("st_card", stStudent.getStCard());
        map.put("grades_ranking", stScholarships.getGradesRanking());
        map.put("evaluation", stScholarships.getEvaluation());
        map.put("compulsory", stScholarships.getCompulsory());
        map.put("pass", stScholarships.getPass());
        map.put("rank", stScholarships.getRank());
        map.put("application", stScholarships.getApplication());
        map.put("recommend", stScholarships.getRecommend());
        map.put("hospital", stScholarships.getHospital());
        map.put("school", stScholarships.getSchool());
        map.put("xuezhi", stStudent.getXuezhi());
        map.put("chushengriqi", stStudent.getChushengriqi());
        map.put("ruxueshijian", stStudent.getRuxueshijian());

        //一个人可以有多个stAwards表数据
        List<Map<String, String>> mapList = new ArrayList<>();
        for (int i = 0; i < stAwards.size(); i++) {
            Map<String, String> map2 = new HashMap();
            map2.put("time", stAwards.get(i).getTime());
            map2.put("award_name", stAwards.get(i).getAwardName());
            map2.put("awarding_unit", stAwards.get(i).getAwardingUnit());
            mapList.add(map2);
        }

        /* mapList.add(map2);*/
     /*   Map map2 = new HashMap();
        map2.put("st_photo","C:/Users/fisherman/Desktop/20185033.jpg");*/

        Map<String, Object> o = new HashMap();
        o.put("datemap", map);
        /*o.put("imgmap",map2);*/
        o.put("planList", mapList);  //获奖情况
        PdfUtils.pdfout(o);
    }
}
package org.jeecg.modules.demo.st.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.jeecg.modules.demo.st.entity.StStudent;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangpeng on 2018/02/01.
 */
public class PdfUtils {
    // 利用模板生成pdf
    public static void pdfout(Map<String, Object> o) {
        // 模板路径
        String templatePath = "C:/Users/fisherman/Desktop/xzmb1.pdf";
        // 生成的新文件路径
        String newPDFPath = "C:/Users/fisherman/Desktop/xz1.pdf";

        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font FontChinese = new Font(bf, 5, Font.NORMAL);
            out = new FileOutputStream(newPDFPath);// 输出流
            reader = new PdfReader(templatePath);// 读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            //文字类的内容处理
            Map<String, String> datemap = (Map<String, String>) o.get("datemap");
            form.addSubstitutionFont(bf);
            for (String key : datemap.keySet()) {
                String value = datemap.get(key);
                form.setField(key, value);
            }

            //图片类的内容处理
/*            Map imgmap = (Map)o.get("imgmap");
            for(String key : imgmap.keySet()) {
                String value = imgmap.get(key);
                String imgpath = value;
                int pageNo = form.getFieldPositions(key).get(0).page;
                Rectangle signRect = form.getFieldPositions(key).get(0).position;
                float x = signRect.getLeft();
                float y = signRect.getBottom();
                //根据路径读取图片
                Image image = Image.getInstance(imgpath);
                //获取图片页面
                PdfContentByte under = stamper.getOverContent(pageNo);
                //图片大小自适应
                image.scaleToFit(signRect.getWidth(), signRect.getHeight());
                //添加图片
                image.setAbsolutePosition(x, y);
                under.addImage(image);
            }*/

            stamper.setFormFlattening(true);// 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑
            stamper.close();
            Document doc = new Document();
            Font font = new Font(bf, 32);
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            PdfImportedPage importPage1 = copy.getImportedPage(new PdfReader(bos.toByteArray()), 2);
            PdfImportedPage importPage2 = copy.getImportedPage(new PdfReader(bos.toByteArray()), 3);
            copy.addPage(importPage);
            copy.addPage(importPage1);
            copy.addPage(importPage2);
            doc.close();
        } catch (IOException e) {
            System.out.println(e);
        } catch (DocumentException e) {
            System.out.println(e);
        }
    }
}
    /**
     * 生成学生的dpf
     * getPDF
     */
    @AutoLog(value = "生成学生的校长特别奖学金pdf")
    @ApiOperation(value = "生成学生的校长特别奖学金pdf", notes = "生成学生的校长特别奖学金pdf")
    @GetMapping(value = "/getXZPDF")
    public Result<?> getXZPDF(@RequestParam(required = true) String stId) {
        System.out.println("===========start=============");
        StStudent student = stStudentService.getStudentId(stId);   //通过学号查询信息
        List<StAwards> stAwards = stAwardsService.getStStAwardsId(stId); //通过学号查询信息
        StScholarships stScholarships = stScholarshipsService.getStScholarshipsIds(stId); //通过学号查询信息
        GetStuPdf.getPdf(student, sysDictService, stClassService, stAwards, stScholarships, sysDepartService);
        System.out.println("===========end=============");
        Result result = new Result();
        return result;
    }

你可能感兴趣的:(项目碎片——java)