将pdf的base64编码格式转为img的base64编码格式并合成一张图片:public static String base64PdfToJpgBase64(String base64Pdf) {if (StrUtil.isEmpty(base64Pdf)){return null;}String jpg_base64 = null;Base64Decoder decoder = new Base64Decoder();try {// Base64解码byte[] pdf_bytes = decoder.decode(base64Pdf);PDDocument doc = PDDocument.load(pdf_bytes);int size = doc.getNumberOfPages();/*图像合并使用的参数*///定义宽度int width = 0;// 保存一张图片中的RGB数据int[] singleImgRGB;// 定义高度,后面用于叠加int shiftHeight = 0;//保存每张图片的像素值BufferedImage imageResult = null;// 利用PdfBox生成图像PDDocument pdDocument = doc;PDFRenderer renderer = new PDFRenderer(pdDocument);/*根据总页数, 按照50页生成一张长图片的逻辑, 进行拆分*/// 每50页转成1张图片int pageLength = size; //有多少转多少// 总计循环的次数int totalCount = pdDocument.getNumberOfPages() / pageLength + 1;for (int m = 0; m < totalCount; m++) {for (int i = 0; i < pageLength; i++) {int pageIndex = i + (m * pageLength);if (pageIndex == pdDocument.getNumberOfPages()) {System.out.println("循环次数 m = " + m);break;}// 96为图片的dpi,dpi越大,则图片越清晰,图片越大,转换耗费的时间也越多BufferedImage image = renderer.renderImageWithDPI(pageIndex, 106, ImageType.RGB);int imageHeight = image.getHeight();int imageWidth = image.getWidth();if (i == 0) {//计算高度和偏移量//使用第一张图片宽度;width = imageWidth;// 保存每页图片的像素值// 加个判断:如果m次循环后所剩的图片总数小于pageLength,则图片高度按剩余的张数绘制,否则会出现长图片下面全是黑色的情况if ((pdDocument.getNumberOfPages() - m * pageLength) < pageLength) {imageResult = new BufferedImage(width, imageHeight * (pdDocument.getNumberOfPages() - m * pageLength), BufferedImage.TYPE_INT_RGB);} else {imageResult = new BufferedImage(width, imageHeight * pageLength, BufferedImage.TYPE_INT_RGB);}} else {// 将高度不断累加shiftHeight += imageHeight;}singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);}// 这个很重要,下面会有说明shiftHeight = 0;}pdDocument.close();ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流ImageIO.write(imageResult, "jpg", baos);//写入流中byte[] jpg_Bytes = baos.toByteArray();//转换成字节BASE64Encoder encoder = new BASE64Encoder();jpg_base64 = encoder.encodeBuffer(jpg_Bytes).trim();//转换成base64串jpg_base64 = jpg_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\nbaos.close();doc.close();} catch (IOException e) {e.printStackTrace();}return jpg_base64;}