使⽤java创建pdf并返回流给前端
使⽤java创建pdf 并返回流给前端
楹联书法业务需求,页⾯点击打印预览⽂件,然后点击打印调⽤系统(浏览器)打印,经⽹上查阅资料,⾃⼰整理后记录下来
⾸先是⾃⼰整理的帮助类
import StringUtils;
import InstanceStatusEnum;
import*;
import*;
import HttpServletRespon;
import*;
/**
* @Author: xp
* @Date: 2021/7/14
*/
public class PdfUtil {
/**
* 返回PDF流
*
* @param respon 相应设置
* @param pathName ⽔印⽂件路径和名称
* @throws Exception 异常
*/
public static void returnPdfStream(HttpServletRespon respon, String pathName)throws Exception {
respon.tContentType("application/pdf");
File file =new File(pathName);
ists()){
FileInputStream in =new FileInputStream(file);
OutputStream out = OutputStream();
byte[] b =new byte[1024*5];
int n;
while((n = in.read(b))!=-1){
out.write(b,0, n);
}
out.flush();
in.clo();
out.clo();
}
}
/**
* 返回⽔印图⽚路径
烤箱蛋糕的做法
*
* @param instanceStatus 流程状态
* @return ⽔印图⽚路径
田七粉
*/
public static String returnWatermarkPath(int instanceStatus){
String watermarkPath ="ioffice-api/src/main/resources/static/watermark";
if(InstanceStatusEnum.Code().equals(instanceStatus)){
watermarkPath = watermarkPath +"/已作废.png";
}el if(InstanceStatusEnum.Code().equals(instanceStatus)){
watermarkPath = watermarkPath +"/审批通过.png";
}el{
watermarkPath = watermarkPath +"/审批中.png";
}
return watermarkPath;
}
/**
* 建⽴⼀个书写器(Writer)与document对象关联,通过书写器(Writer)可以将⽂档写⼊到磁盘中。
*
* @param pdfPath 保存路径
* @param document ⽂档
* @param pdfPathName ⽂件保存路径和名称
* @return 书写器(Writer)
* @throws Exception
*/
public static PdfWriter createPdfWriter(String pdfPath, Document document, String pdfPathName)throws Exception {
//判断⽂件夹是否存在
File file =new File(pdfPath);
if(!ists()){
file.mkdir();
}
file =new File(pdfPathName);
PdfWriter writer = Instance(document,new FileOutputStream(file));
writer.tViewerPreferences(PdfWriter.PageModeUThumbs);
return writer;
}
/**
* 设置第⼀标题内容
*
* @param title 第⼀标题
* @param document ⽂档
* @throws Exception 异常
*/
public static void tPdfFirstTitle(String title, Document document)throws Exception {
Paragraph paragraph =new Paragraph(title,getPdfChineFont(0));
paragraph.tAlignment(Element.ALIGN_CENTER);
paragraph.tSpacingAfter(2);
document.add(paragraph);
}
/**
* 设置第⼆标题内容
*
* @param title 第⼆标题
* @param document ⽂档
* @param alignment 对齐⽅式
* @param firstLineIndent 第⼀⾏缩进
* @param spacingAfter 之后间隔
* @throws Exception
*/
public static void tPdfSecondTitle(String title, Document document,int alignment,int firstLineIndent,int spacingAfter)throws Exception { Paragraph lsh =new Paragraph(title,getPdfChineFont(1));
捉妖记主题曲lsh.tAlignment(alignment);
lsh.tFirstLineIndent(firstLineIndent);
lsh.tSpacingAfter(spacingAfter);
document.add(lsh);
}
/**
* 设置表格内容并将表格加⼊⽂档中
*
* @param dates 数据
* @param document ⽂档
* @param table 表格
* @throws Exception 异常
*/
public static void tPdfTableContent(Object[][] dates, Document document, PdfPTable table)throws Exception {
for(int i =0; i < dates.length; i++){
for(int j =0; j < dates[i].length; j++){
//表格的单元格
PdfPCell pdfCell =new PdfPCell();
//设置表格⾏⾼
if(i >0&& i <3){
pdfCell.tMinimumHeight(50);
}el{
pdfCell.tMinimumHeight(25);
}
Paragraph paragraph =new Paragraph(dates[i][j]+"",getPdfChineFont(1));
pdfCell.tPhra(paragraph);
tCellStyle(pdfCell);
table.addCell(pdfCell);
}
}
document.add(table);
}
/**
* 设置⽔印
*
* @param inputFile 要这设置⽔印的⽂件路径
* @param imageFile ⽔印图⽚路径
* @param tag 标记
* @return ⽣成⽔印⽂件的路径
*/
public static String imageWaterMark(String inputFile, String imageFile, String tag)throws Exception { String[] spe =paratePath(inputFile);
String outputFile = spe[0]+ tag +"."+ spe[1];
PdfReader reader =new PdfReader(inputFile);
PdfStamper stamper =new PdfStamper(reader,new FileOutputStream(outputFile));
int total = NumberOfPages()+1;
Image image = Instance(imageFile);
//坐标
image.tAbsolutePosition(389,730);
//⾃定义⼤⼩
image.scaleAbsolute(80,80);
//旋转弧度
image.tRotation(-55);
//旋转⾓度
image.tRotationDegrees(0);
//依照⽐例缩放
image.scalePercent(50);
PdfGState gs =new PdfGState();
// 设置透明度为0.2
gs.tFillOpacity(0.2f);
PdfContentByte under;
//给每⼀页加⽔印
for(int i =1; i < total; i++){
under = UnderContent(i);
under.beginText();
/
/ 添加⽔印图⽚
under.addImage(image);
under.tGState(gs);
}
stamper.clo();
reader.clo();
return outputFile;
}
/**
* 拆分原⽂件路径
*
* @param path 原⽂件路径
* @return 拆分原路径的数组
*/
public static String[]paratePath(String path){美人鱼存在吗
if(StringUtils.isBlank(path)){
return null;
}
String[] p = path.split("\\.");
return new String[]{p[0], p[1]};
}
/**
* 设置表格单元格样式
*
* @param pdfCell 单元格对象
*/
public static void tCellStyle(PdfPCell pdfCell){
pdfCell.tHorizontalAlignment(Element.ALIGN_CENTER);
pdfCell.tVerticalAlignment(Element.ALIGN_MIDDLE);
pdfCell.tBackgroundColor(new BaColor(0xdd7e6b));
pdfCell.tBorderWidthTop(0.1f);
pdfCell.tBorderWidthBottom(0.1f);
pdfCell.tBorderWidthLeft(0.1f);
abab组词
pdfCell.tBorderWidthRight(0.1f);
}
/**
* 设置pdf字体及⼤⼩
*
* @param type 标题或者内容 0-内容 1-标题
* @return 字体
* @throws Exception 异常
*/
public static Font getPdfChineFont(int type)throws Exception {
// 使⽤系统字体
BaFont bfChine = ateFont("STSong-Light","UniGB-UCS2-H", BaFont.NOT_EMBEDDED); Font font;
if(type ==1){
font =new Font(bfChine);
}el{
font =new Font(bfChine,18, Font.BOLD);
}
return font;
}
/**
* 删除⽂件夹
*
* @param folderPath ⽂件路基
*/
public static void delFolder(String folderPath){
// 删除完⾥⾯所有内容
delAllFile(folderPath);
String filePath = folderPath;
filePath = String();
File myFilePath =new File(filePath);
// 删除空⽂件夹
myFilePath.delete();
}
/
**
* 删除指定⽂件夹下所有⽂件
*
* @param path ⽂件路基
* @return 是否成功
*/
public static boolean delAllFile(String path){
boolean flag =fal;
File file =new File(path);
String[] tempList = file.list();
File temp =null;
for(int i =0; i < tempList.length; i++){
dsWith(File.parator)){
temp =new File(path + tempList[i]);
}el{
temp =new File(path + File.parator + tempList[i]);
}
if(temp.isFile()){
temp.delete();
}
if(temp.isDirectory()){
// 先删除⽂件夹⾥⾯的⽂件
delAllFile(path +"/"+ tempList[i]);
// 再删除空⽂件夹
delFolder(path +"/"+ tempList[i]);
flag =true;
}
}
return flag;
}
}
然后是创建Document pdf⽂档
import BtException;
import ErrorCode;
import StringUtils;
import PrintUrCarPdfDTO;
import PdfUtil;
import DateUnit;
import DateUtil;
import StrUtil;
import*;
import*;
import Date;
/**
* @Author: xp
* @Date: 2021/7/9
娄山关*/
public class PrintUrCarPdf {
/**
* ⽣成Pdf⽂档
* @param entity ⽤车信息对象
第二办公室
* @param pdfPath ⽂件保存路径
* @return ⽂件保存路径
*/
public String printCarPdf(PrintUrCarPdfDTO entity, String pdfPath){
/
/创建⽂档
Document document =new Document();
try{
//创建书写器,写⼊磁盘
String pdfPathName = pdfPath +"\\"+ Lsh()+".pdf";
PdfWriter writer = atePdfWriter(pdfPath, document,pdfPathName);
//设置A4