生成通过函件PDF转图片(还有部分排版问题)
This commit is contained in:
@@ -5,12 +5,15 @@ import com.lowagie.text.Font;
|
||||
import com.lowagie.text.Image;
|
||||
import com.lowagie.text.Paragraph;
|
||||
import com.lowagie.text.pdf.*;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
import org.icepdf.core.exceptions.PDFException;
|
||||
import org.icepdf.core.exceptions.PDFSecurityException;
|
||||
import org.icepdf.core.pobjects.Page;
|
||||
import org.icepdf.core.util.GraphicsRenderingHints;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.RenderedImage;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
@@ -128,37 +131,41 @@ public class PDFUtils {
|
||||
/**
|
||||
* PDF 转图片
|
||||
* @param inputStream pdf 输入流
|
||||
* @param dpi
|
||||
* @param scale 缩放比例
|
||||
*/
|
||||
public static ByteArrayOutputStream pdf2Img(InputStream inputStream, float dpi) {
|
||||
PDDocument document = null;
|
||||
ByteArrayOutputStream outputStream = null;
|
||||
public static ByteArrayOutputStream pdf2Img(InputStream inputStream, float scale) {
|
||||
org.icepdf.core.pobjects.Document document = new org.icepdf.core.pobjects.Document();
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
try {
|
||||
// 加载pdf文档
|
||||
FileInputStream fileInputStream = new FileInputStream("passLetter.pdf");
|
||||
document = PDDocument.load(fileInputStream);
|
||||
// PDF文档渲染对象
|
||||
PDFRenderer renderer = new PDFRenderer(document);
|
||||
int pageCount = document.getNumberOfPages();
|
||||
for (int i = 0; i < pageCount; i++) {
|
||||
/*
|
||||
* renderImage(i,1.9f)
|
||||
* i: 指定页对象下标,从0开始,0即第一页
|
||||
* 1.9f:DPI值(Dots Per Inch),官方描述比例因子,其中 1 = 72 DPI
|
||||
*/
|
||||
BufferedImage image = renderer.renderImage(i, dpi);
|
||||
outputStream = new ByteArrayOutputStream();
|
||||
FileOutputStream fileOutputStream = new FileOutputStream("pdf2img.png");
|
||||
ImageIO.write(image, "PNG", outputStream);
|
||||
byte[] byteArray = outputStream.toByteArray();
|
||||
fileOutputStream.write(byteArray);
|
||||
fileOutputStream.close();
|
||||
document.setInputStream(inputStream, "");
|
||||
|
||||
float rotation = 0f;// 旋转角度
|
||||
for (int i = 0; i < document.getNumberOfPages(); i++) {
|
||||
BufferedImage image = (BufferedImage) document.getPageImage(i,
|
||||
GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX,
|
||||
rotation, scale);
|
||||
RenderedImage rendImage = image;
|
||||
try {
|
||||
ImageIO.write(rendImage, "png", outputStream);
|
||||
//例子中是pdf转png格式的,也可以将上面两行改成jpg,转出jpg格式的,
|
||||
//但是从转换效果来看png的清晰度会相对较高。有个小技巧是第一行行改成jpg,
|
||||
//但第二行使用png,也就是转换成jpg格式但有png清晰度的图片。
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
image.flush();
|
||||
document.dispose();
|
||||
return outputStream;
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
} catch (PDFException | PDFSecurityException | IOException e1) {
|
||||
e1.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -42,11 +42,11 @@ public class PassLetterUtils {
|
||||
document.close();
|
||||
|
||||
//3. 填写通过函模板信息
|
||||
addContentToPdf(outputStream, partnerName + " 先生/女士", 122, 640);
|
||||
addContentToPdf(outputStream, passCode, 122, 558);
|
||||
addContentToPdf(outputStream, verifyCity, 155, 494);
|
||||
addContentToPdf(outputStream, "60天", 135, 450);
|
||||
addContentToPdf(outputStream, passTimeStr, 393, 152);
|
||||
addContentToPdf(outputStream, partnerName + " 先生/女士", 122, 638);
|
||||
addContentToPdf(outputStream, passCode, 122, 557);
|
||||
addContentToPdf(outputStream, verifyCity, 155, 492);
|
||||
addContentToPdf(outputStream, "60天", 135, 448);
|
||||
addContentToPdf(outputStream, passTimeStr, 385, 152);
|
||||
return outputStream;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -75,9 +75,9 @@ public class PassLetterUtils {
|
||||
try {
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
|
||||
PdfReader pdfReader = new PdfReader(inputStream);
|
||||
String fontFamillyPath = PassLetterUtils.class.getResource("/static/simsun.ttc").getPath();
|
||||
BaseFont font = BaseFont.createFont(fontFamillyPath + ",0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
||||
PDFUtils.putParagraphAbsolutely(pdfReader, outputStream, x, y, content, font, 20, 1, new Color(255, 82,25));
|
||||
String fontFamilyPath = PassLetterUtils.class.getResource("/static/Alibaba-PuHuiTi-Regular.ttf").getPath();
|
||||
BaseFont font = BaseFont.createFont(fontFamilyPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
||||
PDFUtils.putParagraphAbsolutely(pdfReader, outputStream, x, y, content, font, 19, 1, new Color(255, 82,25));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user