生成pdf

336 阅读4分钟
package org.jeecg.modules.demo.st.controller;

import java.io.IOException;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;

public class PdfFontUtils {

    // 字体
    private static BaseFont baseFont = null;

    static{
        try {
            /**
             * 设置字体
             *
             * windows路径字体
             * FONT_TYPE=C:/Windows/fonts/simsun.ttc
             * linux路径字体 宋体 (如果没有这个字体文件,就将windows的字体传上去)
             * FONT_TYPE=/usr/share/fonts/win/simsun.ttc
             */
            //可以用配置文件读取
            //获取配置
            //PropertiesLoader pl = new PropertiesLoader("/config/config.properties");
            //拼接文件web访问路径
            //String FONT_TYPE = pl.getProperty("FONT_TYPE");
            //解决中文问题  幼圆
            baseFont = BaseFont.createFont("C:/Windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文档超级  排版
     * @param type 1-标题 2-标题一  3-标题二 4-标题三  5-正文  6-左对齐
     */
    public static Paragraph getFont(int type, String text){
        Font font = new Font(baseFont);
        if(1 == type){//1-标题
            font.setSize(16f);
            font.setStyle(Font.BOLD);
        } else if(2 == type){//2-标题一
            font.setSize(16f);
            font.setStyle(Font.BOLD);
        } else if(3 == type){//3-标题二
            font.setSize(14f);
            font.setStyle(Font.BOLD);
        } else if(4 == type){//4-标题三
            font.setSize(14f);
        } else if(5 == type){//5-正文
            font.setSize(10.5f);
        } else if(6 == type){//6-左对齐
            font.setSize(10.5f);
        } else {
            font.setSize(10.5f);//默认大小
        }
        //注: 字体必须和 文字一起new
        Paragraph paragraph = new Paragraph(text, font);
        if(1 == type){
            paragraph.setAlignment(Paragraph.ALIGN_CENTER);//居中
            paragraph.setSpacingBefore(10f);//上间距
            paragraph.setSpacingAfter(10f);//下间距
        } else if(2 == type){//2-标题一
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED); //默认
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(2f);//下间距
        } else if(3 == type){
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(4 == type){//4-标题三
            //paragraph.setAlignment(Element.ALIGN_RIGHT);//右对齐
            paragraph.setSpacingBefore(2f);//上间距
            paragraph.setSpacingAfter(2f);//下间距
        } else if(5 == type){
            paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
            paragraph.setFirstLineIndent(24);//首行缩进
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        } else if(6 == type){//左对齐
            paragraph.setAlignment(Element.ALIGN_LEFT);
            paragraph.setSpacingBefore(1f);//上间距
            paragraph.setSpacingAfter(1f);//下间距
        }
        //paragraph.setIndentationLeft(50);//整体缩进左边
        //paragraph.setFirstLineIndent(40);//首行缩进
        return paragraph;
    }
}
package org.jeecg.modules.demo.st.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.jeecg.modules.demo.st.controller.PdfFontUtils;
import org.jeecg.modules.demo.st.entity.*;
import org.jeecg.modules.demo.st.service.IStClassService;
import org.jeecg.modules.demo.st.service.IStStudentService;
import org.jeecg.modules.demo.st.vo.ClassVo;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;



public class CreatePdfText {



    public static void main(String[] args) {
        System.out.println("===========start=============");
        try {
            Document doc = createPdf("F:\\test.pdf");
            //生成  合同文件
           // StStudent student=stStudentService.getById("20172239");
           // createFile(doc,student);
            doc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("===========end=============");
    }

    /**
     * 创建一个pdf并打开
     * @param outpath  pdf路径
     */
    public static Document createPdf(String outpath) throws DocumentException, IOException{
        //页面大小
        //Rectangle rect = new Rectangle(PageSize.A4.rotate());//文档横方向
        Rectangle rect = new Rectangle(PageSize.A4);//文档竖方向
        //如果没有则创建
        File saveDir = new File(outpath);
        File dir = saveDir.getParentFile();
        if (!dir.exists()) {
            dir.mkdirs();
        }
        Document doc = new Document(rect);
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outpath));
        //PDF版本(默认1.4)
        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
        //文档属性
        doc.addTitle("Title@wpixel");
        doc.addAuthor("Author@wpixel");
        doc.addSubject("Subject@wpixel");
        doc.addKeywords("Keywords@wpixel");
        doc.addCreator("Creator@wpixel");
        //页边空白
        doc.setMargins(40, 40, 40, 40);
        //打开文档
        doc.open();
        return doc;
    }

    public static void createFile(Document doc,
                                  StStudent stStudent,
                                  ISysDictService sysDictService,
                                  IStClassService stClassService,
                                  ISysDepartService sysDepartService,
                                  List<StStateJilu> stStateJilu,
                                  List<StHonor> stHonor,
                                  List<StPunish> stPunish ) throws DocumentException{

        doc.add(PdfFontUtils.getFont(1, "学生信息"));
        doc.add(PdfFontUtils.getFont(2, "学生详情信息"));
//        doc.add(PdfFontUtils.getFont(6, "姓名:"+stStudent.getStName()));
//        doc.add(PdfFontUtils.getFont(6, "性别:"+stStudent.getSex()));
//        doc.add(PdfFontUtils.getFont(6, "学院:"+stStudent.getCollege()));
//        doc.add(PdfFontUtils.getFont(6, "班级:"+stStudent.getStClass()));
//        doc.add(PdfFontUtils.getFont(6, "民族:"+stStudent.getNationality()));
//        doc.add(PdfFontUtils.getFont(6, "身份证:"+stStudent.getStCard()));
//        doc.add(PdfFontUtils.getFont(6, "联系方式:"+stStudent.getPhone()));
//        doc.add(PdfFontUtils.getFont(6, "详细地址:"+stStudent.getFamilyAddress()));
//        doc.add(PdfFontUtils.getFont(6, "家长电话:"+stStudent.getPhoneOne()));
//        doc.add(PdfFontUtils.getFont(6, "在校情况:"+stStudent.getStatus()));
//        doc.add(PdfFontUtils.getFont(6, "政治面貌:"+stStudent.getFace()));
//        doc.add(PdfFontUtils.getFont(6, "单亲孤儿:"+stStudent.getSingle()));
//        doc.add(PdfFontUtils.getFont(6, "家庭困难级别:"+stStudent.getDifficult()));
//        doc.add(PdfFontUtils.getFont(6, "单亲孤儿:"+stStudent.getPsychological()));
//        doc.add(PdfFontUtils.getFont(6, "学业预警:"+stStudent.getDelayedGraduation()));

        PdfPTable table = new PdfPTable(2);
        table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        PdfPCell cell;
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "姓名:"+stStudent.getStName())));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);

      String  text = sysDictService.queryDictTextByKey("sex", stStudent.getSex());
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "性别:"+text)));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        String text2 = sysDepartService.queryByIdCollges(stStudent.getCollege());
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "学院:"+text2)));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        String text1 = stClassService.queryByIdClas(stStudent.getStClass());
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "班级:"+text1)));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        String  text3 = sysDictService.queryDictTextByKey("nation", stStudent.getNationality());
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "民族:"+text3)));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "身份证:"+stStudent.getStCard())));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "联系方式:"+stStudent.getPhone())));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "详细地址:"+stStudent.getFamilyAddress())));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "家长电话:"+stStudent.getPhoneOne())));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        String  text4 = sysDictService.queryDictTextByKey("st_status", stStudent.getStatus());
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "在校情况:"+text4)));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        String  text9 = sysDictService.queryDictTextByKey("face", stStudent.getFace());
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "政治面貌:"+text9)));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        String  text5 = sysDictService.queryDictTextByKey("single", stStudent.getSingle());
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "单亲孤儿:"+text5)));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        String  text6 = sysDictService.queryDictTextByKey("difficult", stStudent.getDifficult());
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "家庭困难级别:"+text6)));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        cell.setBorder(Rectangle.NO_BORDER);
        String  text7 = sysDictService.queryDictTextByKey("psychological", stStudent.getPsychological());
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "心理情况:"+text7)));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        String  text11 = sysDictService.queryDictTextByKey("whether", stStudent.getAcademicWarning());
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "学业预警:"+text11)));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        String  text8 = sysDictService.queryDictTextByKey("delayedGraduation", stStudent.getDelayedGraduation());
        cell = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "学籍状态:"+text8)));
        cell.setColspan(1);
        cell.setBorder(0);
        table.addCell(cell);
        doc.add(table);


        //离校记录模块
        PdfPTable table1 = new PdfPTable(2);
        table1.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        PdfPCell cell1;
        doc.add(PdfFontUtils.getFont(2, "离校记录"));
        for (StStateJilu stStateJilu1: stStateJilu) {
            cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "离校时间:"+stStateJilu1.getCreateTime())));
            cell1.setColspan(1);
            cell1.setBorder(0);
            table1.addCell(cell1);
            String  text10 = sysDictService.queryDictTextByKey("st_status", stStateJilu1.getTheReason());
            cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text10)));
            cell1.setColspan(1);
            cell1.setBorder(0);
            table1.addCell(cell1);
            cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "审批人:"+stStateJilu1.getFieldTwo())));
            cell1.setColspan(1);
            cell1.setBorder(0);
            table1.addCell(cell1);
            cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "离校原因:"+stStateJilu1.getFieldOne())));
            cell1.setColspan(1);
            cell1.setBorder(0);
            table1.addCell(cell1);
            cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "归校时间:"+stStateJilu1.getDepartureTime())));
            cell1.setColspan(1);
            cell1.setBorder(0);
            table1.addCell(cell1);
            cell1 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "登记人:"+stStateJilu1.getSort())));
            cell1.setColspan(1);
            cell1.setBorder(0);
            table1.addCell(cell1);

        }
        doc.add(table1);

        //荣誉模块
        PdfPTable table2 = new PdfPTable(2);
        table2.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        PdfPCell cell2;
        doc.add(PdfFontUtils.getFont(2, "荣誉记录"));
        for (StHonor stHonor1: stHonor ) {
            cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "荣誉名称:"+stHonor1.getName())));
            cell2.setColspan(1);
            cell2.setBorder(0);
            table2.addCell(cell2);
            String  text12 = sysDictService.queryDictTextByKey("types", stHonor1.getTypes());
            cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text12)));
            cell2.setColspan(1);
            cell2.setBorder(0);
            table1.addCell(cell2);
            String  text14 = sysDictService.queryDictTextByKey("reward",stHonor1.getReward());
            cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "类别:"+text14)));
            cell2.setColspan(1);
            cell2.setBorder(0);
            table2.addCell(cell2);
            String  text15 = sysDictService.queryDictTextByKey("personal",stHonor1.getPersonal());
            cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text15)));
            cell2.setColspan(1);
            cell2.setBorder(0);
            table2.addCell(cell2);
            String  text16 = sysDictService.queryDictTextByKey("experience",stHonor1.getExperience());
            cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text16)));
            cell2.setColspan(1);
            cell2.setBorder(0);
            table2.addCell(cell2);
            String  text13 = sysDictService.queryDictTextByKey("review", stHonor1.getReview());
            cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text13)));
            cell2.setColspan(1);
            cell2.setBorder(0);
            table1.addCell(cell2);
            cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "审批人:"+stHonor1.getYearTwo())));
            cell2.setColspan(1);
            cell2.setBorder(0);
            table2.addCell(cell2);
            cell2 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "时间:"+stHonor1.getYearOne())));
            cell2.setColspan(1);
            cell2.setBorder(0);
            table2.addCell(cell2);
        }
        doc.add(table2);

        //处分记录
        PdfPTable table3 = new PdfPTable(2);
        table3.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        PdfPCell cell3;
        doc.add(PdfFontUtils.getFont(2, "处分记录"));
        for (StPunish stPunish1: stPunish ) {
            cell3 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "处分时间:"+stPunish1.getPunishmentTime())));
            cell3.setColspan(1);
            cell3.setBorder(0);
            table3.addCell(cell3);
            String  text16 = sysDictService.queryDictTextByKey("punish", stPunish1.getPunish());
            cell3 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, ""+text16)));
            cell3.setColspan(1);
            cell3.setBorder(0);
            table3.addCell(cell3);
//            cell3 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "登记人:"+stPunish1.getFieldOne())));
//            cell3.setColspan(1);
//            cell3.setBorder(0);
//            table3.addCell(cell3);

            cell3 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "审批人:"+stPunish1.getApprover())));
            cell3.setColspan(1);
            cell3.setBorder(0);
            table3.addCell(cell3);
            cell3 = new PdfPCell(new Phrase(PdfFontUtils.getFont(5, "处分原因:"+stPunish1.getTheReason())));
            cell3.setColspan(1);
            cell3.setBorder(0);
            table3.addCell(cell3);

        }
        doc.add(table3);
    }

}
    /**
     * 生成学生的dpf
     * getPDF
     */
    @AutoLog(value = "生成学生的dpf")
    @ApiOperation(value = "生成学生的dpf", notes = "生成学生的dpf")
    @GetMapping(value = "/getPDF")
    public Result<?> getPDF(@RequestParam(required = true) String stId) {
        System.out.println("===========start=============");
        try {
            Document doc = createPdf("F:\\test.pdf");
            //生成  合同文件
            StStudent student=stStudentService.getById(stId);
            List<StStateJilu> stStateJilu =  stStateJiluService.getRecording(stId);
            List<StHonor> stHonor = stHonorService.getStudentHonors(stId);
            List<StPunish> stPunish =  stPunishService.getSchoolPunishs(stId);
                createFile(doc,student,sysDictService,stClassService,sysDepartService,stStateJilu,stHonor,stPunish);
            doc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("===========end=============");
        Result result = new Result();
        return result;
    }
public String queryByIdClas(String id);
    @Override
    @Cacheable(value = CacheConstant.SYS_DICT_CACHE,key = "#id")
    public String queryByIdClas(String id) {
        return stClassMapper.queryByIdClas(id);
    }
public String queryByIdClas(@Param("id") String id);
    <select id="queryByIdClas" parameterType="String"  resultType="String">
        SELECT
                classes
                FROM
                    st_class
                 WHERE
                    id=#{id}
 </select>

本文使用 mdnice 排版