照旧是总结前人的技术
show code!
一 依赖
<!--PDF转换为图片-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
二 代码
public void pdfToImg(HttpServletResponse response ,String pdfUrl) {
String imageType = "PNG";//可改成JPG
OutputStream sos = null;
try {
sos = response.getOutputStream();
PDFToImg(sos, pdfUrl, getPDFNum(pdfUrl), imageType);
sos.flush();
} catch (Exception e) {
log.error("PDF转换图片异常: " + e.getMessage());
throw new RuntimeException("PDF转换异常");
} finally {
try {
sos.close();
} catch (IOException e) {
log.error("关闭输出流异常: " + e.getMessage());
}
}
}
/**
* PDF转图片 根据页码一页一页转
* @throws IOException
* imgType:转换后的图片类型 jpg,png
*/
private void PDFToImg(OutputStream sos,String fileUrl,int page,String imgType) throws Exception {
PDDocument pdDocument = null;
/* dpi越大转换后越清晰,相对转换速度越慢 */
int dpi = 100;
try {
pdDocument = getPDDocument(fileUrl);
PDFRenderer renderer = new PDFRenderer(pdDocument);
int pages = pdDocument.getNumberOfPages();
if (page <= pages && page > 0) {
BufferedImage image = renderer.renderImageWithDPI(page-1,dpi);
ImageIO.write(image, imgType, sos);
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(),e);
throw new RuntimeException("PDF转换异常");
} finally {
if (pdDocument != null) {
pdDocument.close();
}
}
}
private PDDocument getPDDocument(String fileUrl) throws IOException {
InputStream inputStream = getInputStreamByUrl(fileUrl);
return PDDocument.load(inputStream);
}
/**
* 根据url地址 获取输入流
* @param url
* @return
* @throws IOException
*/
private InputStream getInputStreamByUrl(String url) throws IOException {
URL httpurl=new URL(URLDecoder.decode(url, "UTF-8"));
InputStream is;
HttpURLConnection httpConn=(HttpURLConnection)httpurl.openConnection();
httpConn.setDoOutput(true);// 使用 URL 连接进行输出
httpConn.setDoInput(true);// 使用 URL 连接进行输入
httpConn.setUseCaches(false);// 忽略缓存
httpConn.setRequestMethod("GET");// 设置URL请求方法
//可设置请求头
httpConn.setRequestProperty("Content-Type", "application/octet-stream");
httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
httpConn.setRequestProperty("Charset", "UTF-8");
httpConn.connect();
if (httpConn.getResponseCode() >= 400 ) {
is = httpConn.getErrorStream();
}
else{
is = httpConn.getInputStream();
}
return is;
}