|
|
@@ -0,0 +1,114 @@
|
|
|
+package com.test;
|
|
|
+
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
+import org.apache.pdfbox.pdmodel.PDPage;
|
|
|
+import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
|
|
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
|
|
+import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+
|
|
|
+public class ImageToPdfConverter {
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+ String path = "D:\\SYSTEM\\Desktop\\temp\\pdf";
|
|
|
+
|
|
|
+ // 1. 下载你的图片转成字节
|
|
|
+ File dir = new File(path + File.separator + "img1");
|
|
|
+ File[] fileList = dir.listFiles();
|
|
|
+
|
|
|
+ // 2. 生成一页 PDF document
|
|
|
+ PDDocument document = new PDDocument();
|
|
|
+ for (File file : fileList) {
|
|
|
+ if (file.isFile()) {
|
|
|
+ // 如果文件
|
|
|
+ add(document, file.getPath());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 4. 保存PDF
|
|
|
+ File outputFile = File.createTempFile("doc", ".pdf");
|
|
|
+ File parentFolder = outputFile.getParentFile();
|
|
|
+ if (parentFolder != null && !parentFolder.exists()) {
|
|
|
+ parentFolder.mkdirs();
|
|
|
+ }
|
|
|
+ document.save(outputFile);
|
|
|
+ document.close();
|
|
|
+ }
|
|
|
+
|
|
|
+// public static void
|
|
|
+
|
|
|
+ private static void add(PDDocument document, PDImageXObject image) {
|
|
|
+ // 这里是你生成PDF自适应图片大小,不设置会默认为A4
|
|
|
+ PDRectangle pageSize = new PDRectangle(image.getWidth(), image.getHeight());
|
|
|
+ PDPage page = new PDPage(pageSize);
|
|
|
+ document.addPage(page);
|
|
|
+ // 3.将 图片 添加进PDF document
|
|
|
+ try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
|
|
|
+ float pageWidth = pageSize.getWidth();
|
|
|
+ float pageHeight = pageSize.getHeight();
|
|
|
+ float imageWidth = image.getWidth();
|
|
|
+ float imageHeight = image.getHeight();
|
|
|
+ float scale = Math.min(pageWidth / imageWidth, pageHeight / imageHeight);
|
|
|
+ float scaledWidth = imageWidth * scale;
|
|
|
+ float scaledHeight = imageHeight * scale;
|
|
|
+ float x = (pageWidth - scaledWidth) / 2;
|
|
|
+ float y = (pageHeight - scaledHeight) / 2;
|
|
|
+ // 这里是将你的图片填充入pdf页
|
|
|
+ contentStream.drawImage(image, x, y, scaledWidth, scaledHeight);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void add(PDDocument document, String path) throws IOException {
|
|
|
+ // 这里是你生成PDF自适应图片大小,不设置会默认为A4
|
|
|
+ PDPage page = new PDPage(PDRectangle.A4);
|
|
|
+ document.addPage(page);
|
|
|
+
|
|
|
+ // 3.将 图片 添加进PDF document
|
|
|
+ try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
|
|
|
+ PDImageXObject image = PDImageXObject.createFromFile(path, document);
|
|
|
+ float pageWidth = PDRectangle.A4.getWidth();
|
|
|
+ float pageHeight = PDRectangle.A4.getHeight();
|
|
|
+ float imageWidth = image.getWidth();
|
|
|
+ float imageHeight = image.getHeight();
|
|
|
+ float scale = Math.min(pageWidth / imageWidth, pageHeight / imageHeight);
|
|
|
+ float scaledWidth = imageWidth * scale;
|
|
|
+ float scaledHeight = imageHeight * scale;
|
|
|
+ float x = (pageWidth - scaledWidth) / 2;
|
|
|
+ float y = (pageHeight - scaledHeight) / 2;
|
|
|
+ // 这里是将你的图片填充入pdf页
|
|
|
+ contentStream.drawImage(image, x, y, scaledWidth, scaledHeight);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static byte[] downloadImage(String imageUrl) throws IOException {
|
|
|
+ HttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
+ HttpGet httpGet = new HttpGet(imageUrl);
|
|
|
+ HttpResponse response = httpClient.execute(httpGet);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ InputStream inputStream = entity.getContent();
|
|
|
+ byte[] imageBytes = IOUtils.toByteArray(inputStream);
|
|
|
+ inputStream.close();
|
|
|
+ return imageBytes;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static byte[] getByte(String path) throws IOException {
|
|
|
+ File f = new File(path);
|
|
|
+ try (FileInputStream is = new FileInputStream(f);) {
|
|
|
+ byte[] imageBytes = IOUtils.toByteArray(is);
|
|
|
+ return imageBytes;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|