1、maven依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.9</version>
</dependency>
2、工具类
package com.office.demo;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class PdfConvertImageUtils {
public static BufferedImage pdfStreamToPng(InputStream pdfFileInputStream) {
PDDocument doc = null;
PDFRenderer renderer = null;
try
{
doc = PDDocument.load(pdfFileInputStream);
renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
BufferedImage image = null;
for (int i = 0; i < pageCount; i++)
{
if (image != null)
{
image = combineBufferedImages(image, renderer.renderImageWithDPI(i, 144));
}
if (i == 0)
{
image = renderer.renderImageWithDPI(i, 144);
}
}
return combineBufferedImages(image);
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (doc != null)
{
doc.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
return null;
}
public static BufferedImage combineBufferedImages(BufferedImage... images) {
int height = 0;
int width = 0;
for (BufferedImage image : images)
{
height += image.getHeight();
width = image.getWidth();
}
BufferedImage combo = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = combo.createGraphics();
int x = 0;
int y = 0;
for (BufferedImage image : images)
{
g2.setStroke(new BasicStroke(2.0f));
g2.setColor(new Color(193, 193, 193));
g2.drawLine(x, y, width, y);
g2.drawImage(image, x, y, null);
y += image.getHeight();
}
return combo;
}
public static void createHtmlByBase64(String base64, String htmlPath) {
StringBuilder stringHtml = new StringBuilder();
PrintStream printStream = null;
try
{
printStream = new PrintStream(new FileOutputStream(htmlPath));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
stringHtml.append("<html><head>");
stringHtml.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
stringHtml.append("<title></title>");
stringHtml.append("</head>");
stringHtml.append("<body style=\"\r\n" + " text-align: center;\r\n" + " background-color: #C1C1C1;\r\n" + "\">");
stringHtml.append("<img src=\"data:image/png;base64," + base64 + "\" />");
stringHtml.append("<a name=\"head\" style=\"position:absolute;top:0px;\"></a>");
stringHtml.append("<a style=\"position:fixed;bottom:10px;right:10px\" href=\"#head\">回到首页</a>");
stringHtml.append("</body></html>");
try
{
String s = stringHtml.toString();
System.out.println(s);
printStream.println(stringHtml.toString());
} catch (Exception e)
{
e.printStackTrace();
} finally
{
if (printStream != null)
{
printStream.close();
}
}
}
public static String bufferedImageToBase64(BufferedImage bufferedImage) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String png_base64 = "";
try
{
ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
png_base64 = encoder.encodeBuffer(bytes).trim().replaceAll("\n", "").replaceAll("\r", "");
} catch (IOException e)
{
e.printStackTrace();
}
return png_base64;
}
public static void main(String[] args) {
InputStream inputStream = UrlUtils.getInputStreamByUrl("https://xxxxx/0ba8f215-e13c-4dbc-a693-c300f5690bfc.pdf");
String htmlPath = "F:\\123.html";
BufferedImage bufferedImage = null;
try
{
bufferedImage = pdfStreamToPng(inputStream);
String base64_png = bufferedImageToBase64(bufferedImage);
createHtmlByBase64(base64_png, htmlPath);
} catch (Exception e)
{
e.printStackTrace();
} finally
{
try
{
if (inputStream != null)
{
inputStream.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
url中获取文件流工具
package com.office.demo;
import org.apache.commons.io.IOUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlUtils {
public static InputStream getInputStreamByUrl(String strUrl) {
HttpURLConnection conn = null;
try
{
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(20 * 1000);
final ByteArrayOutputStream output = new ByteArrayOutputStream();
IOUtils.copy(conn.getInputStream(), output);
return new ByteArrayInputStream(output.toByteArray());
} catch (Exception e)
{
e.printStackTrace();
} finally
{
try
{
if (conn != null)
{
conn.disconnect();
}
} catch (Exception e)
{
}
}
return null;
}
}