PDF修改尺寸
需要注意:第一个方法返回的是转换后PDF的base64。第二个方法返回的是文件流,这个方法才是转的核心。文章来源:https://www.toymoban.com/news/detail-615906.html
/**
* 修改PDF尺寸
*
* @param pdfUrl PDF链接
* @param pdfWidthInMillimeters 指定宽 mm
* @param pdfHeightInMillimeters 指定高 mm
* @return PDF转换尺寸后的base64
*/
public static String updatePdfSize(String pdfUrl, float pdfWidthInMillimeters, float pdfHeightInMillimeters) {
try {
URL url = new URL(pdfUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
InputStream is = conn.getInputStream();
ByteArrayOutputStream out = updatePdfSize(is, pdfWidthInMillimeters, pdfHeightInMillimeters);
BASE64Encoder encoder = new BASE64Encoder();
String pdfBase64 = encoder.encode(out.toByteArray());
is.close();
return pdfBase64;
} catch (IOException e) {
log.error("updatePdfSize error:{}", e.getMessage(), e);
}
return "";
}
/**
* 转换PDF尺寸
*
* @param inputStream PDF源文件流
* @param pdfWidthInMillimeters 指定宽 mm
* @param pdfHeightInMillimeters 指定高 mm
* @return 转换后的PDF的文件流
*/
public static ByteArrayOutputStream updatePdfSize(InputStream inputStream, float pdfWidthInMillimeters, float pdfHeightInMillimeters) {
try {
// 毫米转换为磅(1毫米≈2.83465磅)
float pdfWidthInPoints = pdfWidthInMillimeters * 2.83465f;
float pdfHeightInPoints = pdfHeightInMillimeters * 2.83465f;
// 容器初始化
ByteArrayOutputStream out = new ByteArrayOutputStream();
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc, out);
doc.open();
PdfReader pdfReader = new PdfReader(inputStream);
PdfContentByte cb = writer.getDirectContent();
// 循环修改尺寸
int total = pdfReader.getNumberOfPages();
for (int i = 1; i <= total; i++) {
PdfImportedPage page = writer.getImportedPage(pdfReader, i);
Rectangle rectangle = pdfReader.getPageSize(i);
float originalWidth = rectangle.getWidth();
float originalHeight = rectangle.getHeight();
// 计算缩放比例
float scaleWidth = pdfWidthInPoints / originalWidth;
float scaleHeight = pdfHeightInPoints / originalHeight;
float scale = Math.min(scaleWidth, scaleHeight);
doc.setPageSize(new RectangleReadOnly(originalWidth * scale, originalHeight * scale));
doc.newPage();
cb.addTemplate(page, scale, 0, 0, scale, 0, 0);
}
doc.close();
writer.close();
return out;
} catch (Exception e) {
log.error("updatePdfSize error:{}", e.getMessage(), e);
return null;
}
}
图片转成指大小PDF
返回的是base64。需要返回流的,可能简单改写下文章来源地址https://www.toymoban.com/news/detail-615906.html
/**
* 图片转成指定大小的PDF的base64
*
* @param pngImagePath 图片地址
* @param pdfWidthInMillimeters 指定的PDF宽(mm)
* @param pdfHeightInMillimeters 指定的PDF高(mm)
* @return PDF的Base64
*/
public static String convertPngToPdfBase64(String pngImagePath, float pdfWidthInMillimeters, float pdfHeightInMillimeters) {
try {
// 毫米转换为磅(1毫米≈2.83465磅)
float pdfWidthInPoints = pdfWidthInMillimeters * 2.83465f;
float pdfHeightInPoints = pdfHeightInMillimeters * 2.83465f;
// 读取PNG图像的原始宽度和高度(单位:点)
Image pngImage = Image.getInstance(pngImagePath);
float originalWidth = pngImage.getWidth();
float originalHeight = pngImage.getHeight();
// 计算图像在PDF中的缩放比例
float scaleWidth = pdfWidthInPoints / originalWidth;
float scaleHeight = pdfHeightInPoints / originalHeight;
float scale = Math.min(scaleWidth, scaleHeight);
// 计算图像在PDF中的位置居中显示
float xPosition = (pdfWidthInPoints - originalWidth * scale) / 2;
float yPosition = (pdfHeightInPoints - originalHeight * scale) / 2;
// 创建Document对象,并设置PDF文档的大小为所需尺寸
Document document = new Document(new Rectangle(pdfWidthInPoints, pdfHeightInPoints));
// 创建PdfWriter对象,将输出流与Document对象关联
ByteArrayOutputStream os = new ByteArrayOutputStream();
PdfWriter.getInstance(document, os);
// 打开Document
document.open();
// 设置PNG图像在PDF中的缩放比例和位置
pngImage.scaleAbsolute(originalWidth * scale, originalHeight * scale);
pngImage.setAbsolutePosition(xPosition, yPosition);
// 将PNG图像添加到PDF中
document.add(pngImage);
// 关闭Document
document.close();
// 返回PDF的base64
return new BASE64Encoder().encode(os.toByteArray()).trim().replaceAll("\\r", "").replaceAll("\\n", "");
} catch (DocumentException | IOException e) {
log.error("image 转 pdf 流失败 {}", e.getMessage(), e);
return "";
}
}
到了这里,关于PDF尺寸修改:等比绽放(标准面单100*150mm)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!