package com.ltkj.web.config.pdfutils; import cn.hutool.core.io.FileUtil; import com.itextpdf.text.*; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.ltkj.hosp.domain.TjPdfVO; import lombok.extern.slf4j.Slf4j; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.net.URLEncoder; import java.nio.file.Files; import java.util.List; import static com.itextpdf.text.Rectangle.NO_BORDER; /** * @Author: 西安路泰科技有限公司/lige * @Date: 2022/12/8 9:31 */ @Slf4j public class PdfUtils { /** * 字体存放的跟路径,默认为'C:\Windows\Fonts\' */ private static final String FONT_PATH = System.getProperty("user.dir") + File.separator + "ltkj-admin"+File.separator+"src"+File.separator+"main"+File.separator+"resources"+File.separator+"Font"+File.separator; // private static final String FONT_PATH = "C:\\Windows\\Fonts\\"; // private static final String FONT_PATH = "/Users/chacca/开发相关/代码/ltkj_peis/ltkj-admin/src/main/resources/Font/"; /** * 纸张大小 */ private static Rectangle RECTANGLE = PageSize.A4; /** * 设置字体默认值 * * @return * @throws DocumentException * @throws IOException */ public static BaseFont createBaseFont(String fontName) throws DocumentException, IOException { // 默认为宋体 if (fontName == null) { fontName = "simsun.ttc"; } String fontPre = fontName.substring(fontName.lastIndexOf(".") + 1); if (fontPre.equals("ttc")) { // ttc格式的字体需要加上后缀 fontName = fontName + ",0"; } String font = FONT_PATH + fontName; // log.info("字体路径 -> {}",font); return BaseFont.createFont(font, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); } public static void main(String[] args) { try { BaseFont simsun = createBaseFont(null); System.out.println("simsun = " + simsun); } catch (DocumentException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } /** * 设置字体 * * @return */ public static Font setFont() { return setFont(null, null, null, null); } public static Font setFont(Integer fontSize) { return setFont(null, fontSize, null, null); } public static Font setFont(Integer fontSize, BaseColor fontColor) { return setFont(null, fontSize, null, fontColor); } /** * @param fontName 字体名称 默认宋体 * @param fontSize 字体大小 默认12号 * @param fontStyle 字体样式 * @param fontColor 字体颜色 默认黑色 * @return */ public static Font setFont(String fontName, Integer fontSize, Integer fontStyle, BaseColor fontColor) { try { BaseFont baseFont = createBaseFont(fontName); Font font = new Font(baseFont); if (fontSize != null) { font.setSize(fontSize); } if (fontStyle != null) { font.setStyle(fontStyle); } if (fontColor != null) { font.setColor(fontColor); } return font; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("设置字体失败!"+FONT_PATH); } } /** * 创建pdf文档 * * @param response * @param fileName * @return */ public static Document createDocument(HttpServletResponse response, String fileName) { try { response.reset(); response.setHeader("Content-Type", "application/pdf-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); } catch (Exception e) { e.printStackTrace(); } // 设置大小 RECTANGLE = PageSize.A4; return new Document(RECTANGLE, 50, 50, 50, 50); } /** * 绘制标题 * * @param font * @param titleName * @return */ public static Paragraph setParagraph(Font font, String titleName) { Paragraph paragraph = new Paragraph(titleName, font); //设置文字居中 paragraph.setAlignment(Element.ALIGN_LEFT); //行间距 paragraph.setLeading(5f); //设置段落上空白 paragraph.setSpacingBefore(10f); //设置段落下空白 paragraph.setSpacingAfter(10f); return paragraph; } /** * 设置 * 表格内容 * * @param headFont * @param textFont * @param title * @param list * @return */ public static PdfPTable setTable(Font headFont, Font textFont, String[] title, List list,boolean flag) { //四列 PdfPTable table = createTable(new float[]{120, 120, 120}); //画标题 for (String head : title) { table.addCell(createCell(head, headFont)); } //画内容 for (TjPdfVO tjPdfVO : list) { table.addCell(createCell(tjPdfVO.getProName(), textFont)); table.addCell(createCell(tjPdfVO.getProResult(), textFont)); table.addCell(createCell(tjPdfVO.getCompany(), textFont)); // table.addCell(createCell(tjPdfVO.getStandardValue(), textFont)); if ((tjPdfVO.getSj() != null || tjPdfVO.getTs() != null) && flag){ PdfPCell cell = new PdfPCell(); // 水平、垂直居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setPhrase(new Phrase("检查所见:", textFont)); // 设置无边框 cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(); // 水平、垂直居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setPhrase(new Phrase(tjPdfVO.getSj(), textFont)); // 设置无边框 cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(); // 水平、垂直居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setPhrase(new Phrase("结论:", textFont)); // 设置无边框 cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(); // 水平、垂直居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setPhrase(new Phrase(tjPdfVO.getTs(), textFont)); // 设置无边框 cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(3); table.addCell(cell); } } return table; } /** * 设置 * 表格内容 * * @param headFont * @param textFont * @param title * @param list * @return */ public static PdfPTable setTable1(Font headFont, Font textFont, String[] title, List list) { //四列 PdfPTable table = createTable(new float[]{240, 240}); //画标题 for (String head : title) { table.addCell(createCell(head, headFont)); } //画内容 for (TjPdfVO tjPdfVO : list) { table.addCell(createCell(tjPdfVO.getProName(), textFont)); table.addCell(createCell(tjPdfVO.getProResult(), textFont)); } return table; } /** * 设置 * 表格内容 * * @param headFont * @param textFont * @param title * @param list * @return */ public static PdfPTable setTable2(Font headFont, Font textFont, String[] title, List list,boolean flag,float[] titleWidth) { //四列 PdfPTable table = createTable(titleWidth); // PdfPTable table = createTable(new float[]{240, 240,240}); //画标题 for (String head : title) { table.addCell(createCell(head, headFont)); } //画内容 for (TjPdfVO tjPdfVO : list) { table.addCell(createCell(tjPdfVO.getProName(), textFont)); table.addCell(createCell(tjPdfVO.getProResult(), textFont)); table.addCell(createCell(tjPdfVO.getCompany(), textFont)); if ((tjPdfVO.getSj() != null || tjPdfVO.getTs() != null) && flag){ PdfPCell cell = new PdfPCell(); // 水平、垂直居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setPhrase(new Phrase("检查所见:", textFont)); // 设置无边框 cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(); // 水平、垂直居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setPhrase(new Phrase(tjPdfVO.getSj(), textFont)); // 设置无边框 cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(); // 水平、垂直居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setPhrase(new Phrase("结论:", textFont)); // 设置无边框 cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(3); table.addCell(cell); cell = new PdfPCell(); // 水平、垂直居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setPhrase(new Phrase(tjPdfVO.getTs(), textFont)); // 设置无边框 cell.setBorder(Rectangle.NO_BORDER); cell.setColspan(3); table.addCell(cell); } } return table; } public static PdfPTable createTable(float[] widths) { PdfPTable table = new PdfPTable(widths); try { // 设置表格大小 table.setTotalWidth(RECTANGLE.getWidth() - 100); //锁住宽度 table.setLockedWidth(true); //table.setSpacingBefore(3f); // 设置表格下面空白宽度 //table.setSpacingAfter(3f); // 居中 table.setHorizontalAlignment(Element.ALIGN_LEFT); // 边框 //table.getDefaultCell().setBorder(1); table.getDefaultCell().setBorder(0); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT); } catch (Exception e) { e.printStackTrace(); } return table; } public static PdfPCell createCell(String value, Font font) { PdfPCell cell = new PdfPCell(); // 水平、垂直居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setPhrase(new Phrase(value, font)); // 设置无边框 cell.setBorder(Rectangle.NO_BORDER); // 设置高度 //cell.setFixedHeight(20); return cell; } /** * * @param value * @param font * @param boder Rectangle类的枚举 * @param txtAlign Element类的枚举 * @return */ public static PdfPCell createCell(String value, Font font,int boder,int txtAlign,Float paddingTop,Float paddingBottom) { PdfPCell cell = new PdfPCell(); // 水平、垂直居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(txtAlign); cell.setPhrase(new Phrase(value, font)); if (paddingTop != null) cell.setPaddingTop(paddingTop); if (paddingBottom != null) cell.setPaddingBottom(paddingBottom); // 设置无边框 cell.setBorder(boder); // 设置高度 //cell.setFixedHeight(20); return cell; } }