zjh
2024-12-06 c42f62b32bac302a7ac9fcde7489f7c5fc6e11aa
ltkj-admin/src/main/java/com/ltkj/web/config/pdfutils/PdfUtils.java
@@ -1,5 +1,6 @@
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;
@@ -8,8 +9,10 @@
import com.ltkj.hosp.domain.TjPdfVO;
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;
@@ -24,7 +27,8 @@
    /**
     * 字体存放的跟路径,默认为'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 = "/Users/chacca/开发相关/代码/ltkj_peis/ltkj-admin/src/main/resources/Font/";
    /**
     * 纸张大小
@@ -38,7 +42,7 @@
     * @throws DocumentException
     * @throws IOException
     */
    private static BaseFont createBaseFont(String fontName) throws DocumentException, IOException {
    public static BaseFont createBaseFont(String fontName) throws DocumentException, IOException {
        // 默认为宋体
        if (fontName == null) {
            fontName = "simsun.ttc";
@@ -50,6 +54,17 @@
        }
        String font = FONT_PATH + fontName;
        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);
        }
    }
    /**
@@ -151,7 +166,7 @@
     */
    public static PdfPTable setTable(Font headFont, Font textFont, String[] title, List<TjPdfVO> list) {
        //四列
        PdfPTable table = createTable(new float[]{120, 120, 120, 120});
        PdfPTable table = createTable(new float[]{120, 120, 120});
        //画标题
        for (String head : title) {
            table.addCell(createCell(head, headFont));
@@ -161,7 +176,7 @@
            table.addCell(createCell(tjPdfVO.getProName(), textFont));
            table.addCell(createCell(tjPdfVO.getProResult(), textFont));
            table.addCell(createCell(tjPdfVO.getCompany(), textFont));
            table.addCell(createCell(tjPdfVO.getStandardValue(), textFont));
//            table.addCell(createCell(tjPdfVO.getStandardValue(), textFont));
        }
        return table;
    }
@@ -191,6 +206,33 @@
        return table;
    }
    /**
     * 设置
     * 表格内容
     *
     * @param headFont
     * @param textFont
     * @param title
     * @param list
     * @return
     */
    public static PdfPTable setTable2(Font headFont, Font textFont, String[] title, List<TjPdfVO> list) {
        //四列
        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));
        }
        return table;
    }
    private static PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
@@ -203,7 +245,7 @@
            //table.setSpacingAfter(3f);
            // 居中
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.setHorizontalAlignment(Element.ALIGN_LEFT);
            // 边框
            //table.getDefaultCell().setBorder(1);
            table.getDefaultCell().setBorder(0);
@@ -220,7 +262,7 @@
        PdfPCell cell = new PdfPCell();
        // 水平、垂直居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setPhrase(new Phrase(value, font));
        // 设置无边框