生成通过函件PDF转图片(还有部分排版问题)
This commit is contained in:
@@ -98,11 +98,11 @@
|
|||||||
<artifactId>openpdf</artifactId>
|
<artifactId>openpdf</artifactId>
|
||||||
<version>1.3.30</version>
|
<version>1.3.30</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- PDFBox -->
|
<!-- https://mvnrepository.com/artifact/org.icepdf.os/icepdf-core -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.pdfbox</groupId>
|
<groupId>org.icepdf.os</groupId>
|
||||||
<artifactId>pdfbox</artifactId>
|
<artifactId>icepdf-core</artifactId>
|
||||||
<version>2.0.27</version>
|
<version>6.1.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -5,12 +5,15 @@ import com.lowagie.text.Font;
|
|||||||
import com.lowagie.text.Image;
|
import com.lowagie.text.Image;
|
||||||
import com.lowagie.text.Paragraph;
|
import com.lowagie.text.Paragraph;
|
||||||
import com.lowagie.text.pdf.*;
|
import com.lowagie.text.pdf.*;
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.icepdf.core.exceptions.PDFException;
|
||||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
import org.icepdf.core.exceptions.PDFSecurityException;
|
||||||
|
import org.icepdf.core.pobjects.Page;
|
||||||
|
import org.icepdf.core.util.GraphicsRenderingHints;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.awt.image.RenderedImage;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -128,37 +131,41 @@ public class PDFUtils {
|
|||||||
/**
|
/**
|
||||||
* PDF 转图片
|
* PDF 转图片
|
||||||
* @param inputStream pdf 输入流
|
* @param inputStream pdf 输入流
|
||||||
* @param dpi
|
* @param scale 缩放比例
|
||||||
*/
|
*/
|
||||||
public static ByteArrayOutputStream pdf2Img(InputStream inputStream, float dpi) {
|
public static ByteArrayOutputStream pdf2Img(InputStream inputStream, float scale) {
|
||||||
PDDocument document = null;
|
org.icepdf.core.pobjects.Document document = new org.icepdf.core.pobjects.Document();
|
||||||
ByteArrayOutputStream outputStream = null;
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
try {
|
try {
|
||||||
// 加载pdf文档
|
document.setInputStream(inputStream, "");
|
||||||
FileInputStream fileInputStream = new FileInputStream("passLetter.pdf");
|
|
||||||
document = PDDocument.load(fileInputStream);
|
float rotation = 0f;// 旋转角度
|
||||||
// PDF文档渲染对象
|
for (int i = 0; i < document.getNumberOfPages(); i++) {
|
||||||
PDFRenderer renderer = new PDFRenderer(document);
|
BufferedImage image = (BufferedImage) document.getPageImage(i,
|
||||||
int pageCount = document.getNumberOfPages();
|
GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX,
|
||||||
for (int i = 0; i < pageCount; i++) {
|
rotation, scale);
|
||||||
/*
|
RenderedImage rendImage = image;
|
||||||
* renderImage(i,1.9f)
|
try {
|
||||||
* i: 指定页对象下标,从0开始,0即第一页
|
ImageIO.write(rendImage, "png", outputStream);
|
||||||
* 1.9f:DPI值(Dots Per Inch),官方描述比例因子,其中 1 = 72 DPI
|
//例子中是pdf转png格式的,也可以将上面两行改成jpg,转出jpg格式的,
|
||||||
*/
|
//但是从转换效果来看png的清晰度会相对较高。有个小技巧是第一行行改成jpg,
|
||||||
BufferedImage image = renderer.renderImage(i, dpi);
|
//但第二行使用png,也就是转换成jpg格式但有png清晰度的图片。
|
||||||
outputStream = new ByteArrayOutputStream();
|
} catch (IOException e) {
|
||||||
FileOutputStream fileOutputStream = new FileOutputStream("pdf2img.png");
|
e.printStackTrace();
|
||||||
ImageIO.write(image, "PNG", outputStream);
|
}
|
||||||
byte[] byteArray = outputStream.toByteArray();
|
image.flush();
|
||||||
fileOutputStream.write(byteArray);
|
document.dispose();
|
||||||
fileOutputStream.close();
|
|
||||||
return outputStream;
|
return outputStream;
|
||||||
}
|
}
|
||||||
}catch (Exception e){
|
} catch (PDFException | PDFSecurityException | IOException e1) {
|
||||||
e.printStackTrace();
|
e1.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
outputStream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return outputStream;
|
return outputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,11 +42,11 @@ public class PassLetterUtils {
|
|||||||
document.close();
|
document.close();
|
||||||
|
|
||||||
//3. 填写通过函模板信息
|
//3. 填写通过函模板信息
|
||||||
addContentToPdf(outputStream, partnerName + " 先生/女士", 122, 640);
|
addContentToPdf(outputStream, partnerName + " 先生/女士", 122, 638);
|
||||||
addContentToPdf(outputStream, passCode, 122, 558);
|
addContentToPdf(outputStream, passCode, 122, 557);
|
||||||
addContentToPdf(outputStream, verifyCity, 155, 494);
|
addContentToPdf(outputStream, verifyCity, 155, 492);
|
||||||
addContentToPdf(outputStream, "60天", 135, 450);
|
addContentToPdf(outputStream, "60天", 135, 448);
|
||||||
addContentToPdf(outputStream, passTimeStr, 393, 152);
|
addContentToPdf(outputStream, passTimeStr, 385, 152);
|
||||||
return outputStream;
|
return outputStream;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
@@ -75,9 +75,9 @@ public class PassLetterUtils {
|
|||||||
try {
|
try {
|
||||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
|
||||||
PdfReader pdfReader = new PdfReader(inputStream);
|
PdfReader pdfReader = new PdfReader(inputStream);
|
||||||
String fontFamillyPath = PassLetterUtils.class.getResource("/static/simsun.ttc").getPath();
|
String fontFamilyPath = PassLetterUtils.class.getResource("/static/Alibaba-PuHuiTi-Regular.ttf").getPath();
|
||||||
BaseFont font = BaseFont.createFont(fontFamillyPath + ",0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
BaseFont font = BaseFont.createFont(fontFamilyPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
||||||
PDFUtils.putParagraphAbsolutely(pdfReader, outputStream, x, y, content, font, 20, 1, new Color(255, 82,25));
|
PDFUtils.putParagraphAbsolutely(pdfReader, outputStream, x, y, content, font, 19, 1, new Color(255, 82,25));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
|
import static com.cool.store.utils.PDFUtils.pdf2Img;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class PartnerInterviewServiceImpl implements PartnerInterviewService {
|
public class PartnerInterviewServiceImpl implements PartnerInterviewService {
|
||||||
|
|
||||||
@@ -122,19 +124,15 @@ public class PartnerInterviewServiceImpl implements PartnerInterviewService {
|
|||||||
String code = passLetterDetail.getPassCode() == null ? PassLetterUtils.genPassCode(createTime) : passLetterDetail.getPassCode();
|
String code = passLetterDetail.getPassCode() == null ? PassLetterUtils.genPassCode(createTime) : passLetterDetail.getPassCode();
|
||||||
//生成的 pdf 通过函内存输出流
|
//生成的 pdf 通过函内存输出流
|
||||||
ByteArrayOutputStream pdfOut = PassLetterUtils.genPassLetter(passLetterDetail.getPartnerName(), code, passLetterDetail.getVerifyCity(), createTime);
|
ByteArrayOutputStream pdfOut = PassLetterUtils.genPassLetter(passLetterDetail.getPartnerName(), code, passLetterDetail.getVerifyCity(), createTime);
|
||||||
FileOutputStream fileOutputStream = new FileOutputStream("passLetter.pdf");
|
|
||||||
fileOutputStream.write(pdfOut.toByteArray());
|
|
||||||
fileOutputStream.close();
|
|
||||||
//生成的 pdf 通过函内存输入流
|
//生成的 pdf 通过函内存输入流
|
||||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(pdfOut.toByteArray());
|
ByteArrayInputStream inputStream = new ByteArrayInputStream(pdfOut.toByteArray());
|
||||||
// String passPdfUrl = ossServer.uploadFileServer(inputStream, "partner/passLetter/" + code + ".pdf");
|
String passPdfUrl = ossServer.uploadFileServer(inputStream, "partner/passLetter/" + code + ".pdf");
|
||||||
String passPdfUrl = "";
|
|
||||||
//转换为图片
|
//转换为图片
|
||||||
inputStream.reset();
|
inputStream.reset();
|
||||||
ByteArrayOutputStream imageOut = PDFUtils.pdf2Img(inputStream, 2.0f);
|
ByteArrayOutputStream imageOut = PDFUtils.pdf2Img(inputStream, 2.0f);
|
||||||
|
inputStream = new ByteArrayInputStream(imageOut.toByteArray());
|
||||||
//上传 OSS
|
//上传 OSS
|
||||||
// String passImageUrl = ossServer.uploadFileServer(inputStream, "partner/passLetter/" + code + ".jpg");
|
String passImageUrl = ossServer.uploadFileServer(inputStream, "partner/passLetter/" + code + ".png");
|
||||||
String passImageUrl = "";
|
|
||||||
//计算有效期截止日期
|
//计算有效期截止日期
|
||||||
DateTime expiryDate = DateUtil.offsetDay(createTime, 60);
|
DateTime expiryDate = DateUtil.offsetDay(createTime, 60);
|
||||||
String expiryDateStr = DateUtil.format(expiryDate, "yyyy-MM-dd") + " 23:59:59";
|
String expiryDateStr = DateUtil.format(expiryDate, "yyyy-MM-dd") + " 23:59:59";
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user