package com.ltkj.common.utils.pdfutils;
|
|
import com.itextpdf.text.*;
|
import com.itextpdf.text.pdf.BaseFont;
|
import com.itextpdf.text.pdf.PdfPCell;
|
import com.itextpdf.text.pdf.PdfPTable;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.File;
|
import java.io.IOException;
|
import java.net.URLEncoder;
|
|
|
/**
|
* @Author: 西安路泰科技有限公司/lige
|
* @Date: 2022/12/8 9:31
|
*/
|
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 Rectangle RECTANGLE = PageSize.A4;
|
|
/**
|
* 设置字体默认值
|
*
|
* @return
|
* @throws DocumentException
|
* @throws IOException
|
*/
|
private 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;
|
return BaseFont.createFont(font, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
}
|
|
/**
|
* 设置字体
|
*
|
* @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("设置字体失败!");
|
}
|
}
|
|
/**
|
* 创建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;
|
}
|
|
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_CENTER);
|
// 边框
|
//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_CENTER);
|
cell.setPhrase(new Phrase(value, font));
|
|
// 设置无边框
|
cell.setBorder(Rectangle.NO_BORDER);
|
|
// 设置高度
|
//cell.setFixedHeight(20);
|
|
return cell;
|
}
|
}
|