echarts生成的超长图片使用itextPdf转换为pdf
引入依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
注:使用的图片为竖版图片,要将echarts生成的图片顺时针旋转90度
图片旋转方法借鉴于此
package com.snow.imageutils.utils;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
/**
* @title: ImageToPdfUtil
* @Author Qiao
* @Date: 2023/2/22 0022 10:48
* @Version 1.0
*/
public class ImageToPdfUtil {
//itextPd 中pdf页面高度宽度最大值都不能超过14400
private static int PAGE_MAX_HEIGHT=14400;
private static int DECIMAL_POINT_NUM=2;
private static int ONE_HUNDRED=100;
//
public static void imageConvertPDF(Image image,String filePath){
Document document = new Document();
//设置文档页边距
document.setMargins(0, 0, 0, 0);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);
PdfWriter.getInstance(document, fos);
//打开文档
document.open();
//获取图片高度
float scaledHeight = image.getScaledHeight();
//判断图片高度是否大于最高高度14400
if (scaledHeight > PAGE_MAX_HEIGHT){
//图片高度除以页面最大高度14400乘以100等于图片缩小比例
BigDecimal bigDecimal = BigDecimal.valueOf(scaledHeight);
BigDecimal bigDecimal1 = BigDecimal.valueOf(PAGE_MAX_HEIGHT);
BigDecimal divide = bigDecimal1.divide(bigDecimal, DECIMAL_POINT_NUM, BigDecimal.ROUND_DOWN);
BigDecimal multiply = divide.multiply(BigDecimal.valueOf(ONE_HUNDRED));
//multiply 计算的图片缩放比例
image.scalePercent(multiply.intValue());
}
float imageHeight = image.getScaledHeight();
float imageWidth = image.getScaledWidth();
//设置页面宽高与图片一致 最大为14400
Rectangle rectangle = new Rectangle(imageWidth, imageHeight);
document.setPageSize(rectangle);
//图片居中
image.setAlignment(Image.ALIGN_CENTER);
//新建一页添加图片
document.newPage();
document.add(image);
}catch (Exception e){
}finally {
//关闭文档
document.close();
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* @description: 将图片转换为BufferedImage
* @author: qiaohui
* @date: 2023/2/21 0021 14:46
* @param:
* @return:
**/
private static BufferedImage toBufferedImage(java.awt.Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(
image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) {
//........
}
if (bimage == null) {
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
public static void main(String[] args) {
try {
Image image = Image.getInstance("E:/imageServer/6.png");
imageConvertPDF(image,"E:/imageServer/6.pdf");
} catch (BadElementException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
不压缩图片高度直接转换为pdf会出现以下错误,只需要将页面最大尺寸限制再14400以内即可
Exception in thread "main" ExceptionConverter: com.itextpdf.text.DocumentException: The page size must be smaller than 14400 by 14400. It's 694.0 by 17900.0.
at com.itextpdf.text.pdf.PdfPage.<init>(PdfPage.java:96)
at com.itextpdf.text.pdf.PdfDocument.endPage(PdfDocument.java:1021)
at com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:881)
at com.itextpdf.text.Document.close(Document.java:415)
at com.snow.imageutils.utils.ImageToPdfUtil.imageConvertPDF(ImageToPdfUtil.java:67)
at com.snow.imageutils.utils.ImageToPdfUtil.main(ImageToPdfUtil.java:118)