需要的JAR :
Jar包链接: https://download.csdn.net/download/weixin_38948287/11253648
case ".pdf":
case ".PDF":
PdfToHtml.PdfToImage(uploadPath + newFileName, transPath+time,time);
package com.framework.common.utils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
public class PdfToHtml {
public static void PdfToImage(String pdfurl,String htmlurl,String time){
StringBuffer buffer = new StringBuffer();
FileOutputStream fos;
PDDocument document;
File pdfFile;
int size;
BufferedImage image;
FileOutputStream out;
//PDF转换成HTML保存的文件夹
String path =htmlurl;
File htmlsDir = new File(path);
if(!htmlsDir.exists()){
htmlsDir.mkdirs();
}
File htmlDir = new File(path+"/");
if(!htmlDir.exists()){
htmlDir.mkdirs();
}
try{
//遍历处理pdf附件
buffer.append("<!doctype html>\r\n");
buffer.append("<head>\r\n");
buffer.append("<meta charset=\"UTF-8\">\r\n");
buffer.append("</head>\r\n");
buffer.append("<body style=\"background-color:gray;\">\r\n");
buffer.append("<style>\r\n");
buffer.append("img {background-color:#fff; text-align:center; width:100%; max-width:100%;margin-top:6px;}\r\n");
buffer.append("</style>\r\n");
document = new PDDocument();
//pdf附件
pdfFile = new File(pdfurl);
document = PDDocument.load(pdfFile, (String) null);
size = document.getNumberOfPages();
PDFRenderer reader = new PDFRenderer(document);
for(int i=0 ; i < size; i++){
//image = new PDFRenderer(document).renderImageWithDPI(i,130,ImageType.RGB);
image = reader.renderImage(i, 1.5f);
//生成图片,保存位置
out = new FileOutputStream(path + "/"+ "image" + "_" + i + ".png");
ImageIO.write(image, "png", out); //使用pang的清晰度
//将图片路径追加到网页文件里
buffer.append("<img src=\"" + time +"/"+ "image" + "_" + i + ".png\"/>\r\n");
image = null; out.flush();
out.close();
}
reader = null;
document.close();
buffer.append("</body>\r\n");
buffer.append("</html>