zjh
2025-01-20 73a816fd2b1e29c25d615c85ce34b12b55c16ccf
ltkj-admin/src/main/java/com/ltkj/web/config/pdfutils/PdfUtils.java
@@ -1,15 +1,19 @@
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;
@@ -19,12 +23,14 @@
 * @Author: 西安路泰科技有限公司/lige
 * @Date: 2022/12/8 9:31
 */
@Slf4j
public class PdfUtils {
    /**
     * 字体存放的跟路径,默认为'C:\Windows\Fonts\'
     */
    private static final String FONT_PATH = "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/";
    /**
@@ -50,7 +56,19 @@
            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);
        }
    }
    /**
@@ -93,7 +111,7 @@
            return font;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("设置字体失败!");
            throw new RuntimeException("设置字体失败!"+FONT_PATH);
        }
    }
@@ -150,7 +168,7 @@
     * @param list
     * @return
     */
    public static PdfPTable setTable(Font headFont, Font textFont, String[] title, List<TjPdfVO> list) {
    public static PdfPTable setTable(Font headFont, Font textFont, String[] title, List<TjPdfVO> list,boolean flag) {
        //四列
        PdfPTable table = createTable(new float[]{120, 120, 120});
        //画标题
@@ -163,6 +181,47 @@
            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;
    }
@@ -202,9 +261,10 @@
     * @param list
     * @return
     */
    public static PdfPTable setTable2(Font headFont, Font textFont, String[] title, List<TjPdfVO> list) {
    public static PdfPTable setTable2(Font headFont, Font textFont, String[] title, List<TjPdfVO> list,boolean flag,float[] titleWidth) {
        //四列
        PdfPTable table = createTable(new float[]{240, 240,240});
        PdfPTable table = createTable(titleWidth);
//        PdfPTable table = createTable(new float[]{240, 240,240});
        //画标题
        for (String head : title) {
            table.addCell(createCell(head, headFont));
@@ -214,12 +274,53 @@
            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;
    }
    private static PdfPTable createTable(float[] widths) {
    public static PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            // 设置表格大小
@@ -244,7 +345,7 @@
        return table;
    }
    private static PdfPCell createCell(String value, Font font) {
    public static PdfPCell createCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        // 水平、垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
@@ -259,4 +360,30 @@
        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;
    }
}