package com.huawei.it.isrp.soms.utils;
import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* pdf文件工具类
*/
@Slf4j
public final class PdfPrintUtil {
/**
* 日志
*/
/**
* private constructor
*/
private PdfPrintUtil() {
super();
}
/***
* 字符串转化成png文件
* @param str 字符串
* @param path 生成的文件路径
* @param fileName 文件名
*/
public static File toPng(String str, String path, String fileName) {
try {
File tmpDirectory = new File(path);
if (!tmpDirectory.isDirectory()) {
tmpDirectory.mkdirs();
}
byte[] bytes = Base64.decodeBase64(str);
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] < 0) {
bytes[i] += 256;
}
}
String filePath = path + "/" + fileName+".png";
OutputStream out = new FileOutputStream(filePath);
out.write(bytes);
out.flush();
out.close();
return new File(filePath);
} catch (Exception e) {
log.error("生成png图片失败:", e);
}
return null;
}
/***
* 字符串转化成png文件
* @param pngPath png文件路径
* @param pdfPath 生成的pdf文件路径
*/
public static File toPdf(String pngPath, String pdfPath) {
com.itextpdf.text.Document document = new Document();
//设置文档页边距
document.setMargins(0, 0, 0, 0);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pdfPath);
PdfWriter.getInstance(document, fos);
//打开文档
document.open();
//获取图片的宽高
Image image = Image.getInstance(pngPath);
float imageHeight = image.getScaledHeight();
float imageWidth = image.getScaledWidth();
//设置页面宽高与图片一致
Rectangle rectangle = new Rectangle(imageWidth, imageHeight);
document.setPageSize(rectangle);
//图片居中
image.setAlignment(Image.ALIGN_LEFT);
//新建一页添加图片
document.newPage();
document.add(image);
return new File(pdfPath);
} catch (Exception e) {
log.error("生成pdf失败:", e);
} finally {
//关闭文档
document.close();
try {
fos.flush();
fos.close();
} catch (IOException e) {
log.error("生成pdf失败:", e);
}
}
return null;
}
/**
* 压缩pdf
*
* @param in 压缩之前的pdf文件
* @param newPdfPath 压缩之后的pdf文件
*/
public static File scaleImage(InputStream in, String newPdfPath) {
try {
//写pdf内容
com.itextpdf.kernel.pdf.PdfWriter writer = new com.itextpdf.kernel.pdf.PdfWriter(newPdfPath);
//读pdf内容
com.itextpdf.kernel.pdf.PdfReader reader =
new com.itextpdf.kernel.pdf.PdfReader(in);
com.itextpdf.kernel.pdf.PdfDocument destpdf = new com.itextpdf.kernel.pdf.PdfDocument(writer);
com.itextpdf.kernel.pdf.PdfDocument srcPdf = new com.itextpdf.kernel.pdf.PdfDocument(reader);
com.itextpdf.kernel.pdf.PdfPage origPage = srcPdf.getPage(1);
com.itextpdf.kernel.geom.Rectangle orig = origPage.getPageSizeWithRotation();
PdfPage page = destpdf.addNewPage();
AffineTransform transformationMatrix =
AffineTransform.getScaleInstance(page.getPageSize().getWidth() / orig.getWidth() / 2.5,
page.getPageSize().getHeight() / orig.getHeight() / 2.5);
com.itextpdf.kernel.pdf.canvas.PdfCanvas canvas = new com.itextpdf.kernel.pdf.canvas.PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
PdfFormXObject pageCopy = origPage.copyAsFormXObject(destpdf);
canvas.addXObject(pageCopy, 0, 1300);
com.itextpdf.layout.Document doc = new com.itextpdf.layout.Document(destpdf);
doc.close();
srcPdf.close();
destpdf.close();
reader.close();
writer.close();
return new File(newPdfPath);
} catch (Exception e) {
log.error("生成压缩的pdf失败:", e);
}
return null;
}
/***
* pdf转化为byte数组
* @param file pdf文件
*/
public static byte[] File2byte(File file) {
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
log.error("文件不存在,转化byte数组失败:", e);
} catch (IOException e) {
log.error("解析失败,转化byte数组失败:", e);
}
return buffer;
}
}
【无标题】
最新推荐文章于 2024-08-18 23:25:39 发布