先安裝openoffice用戶端,在測試之後發現有些圖示是無法轉換的,是以在查詢了下後發現LibreOffice,LibreOffice生成的pdf效果要比openoffice還原度高。
maven pom
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--PPT 2 PDF START-->
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jurt</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>4.1.2</version>
</dependency>
<!--jodconverter2.2.1必須依賴slf4j-jdk14必須這個版本,不然源碼中日志會報錯-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.4.3</version>
</dependency>
<!-- PDF2PNG START -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.21</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
JAVA CODE
public class PDF2PNGClient implements Supplier<List<String>> {
private final Logger logger = LoggerFactory.getLogger(PDF2PNGClient.class);
private BlockingQueue<String> queue;
private String targetPath;
private ResourceSuffix suffix;
private List<String> images = null;
public PDF2PNGClient(BlockingQueue<String> queue, String targetPath, ResourceSuffix suffix) {
this.queue = queue;
this.targetPath = targetPath;
this.suffix = suffix;
logger.info("service pdf 2 png new create!!!");
}
public List<String> get() {
try {
logger.info("pdf 2 png start!!!");
String take = queue.take();
logger.info("get task:" + take);
File file = new File(take);
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
images = new LinkedList<String>();
File targetFile = new File(targetPath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI
String imagePath = targetPath + i + "-" + UUID.randomUUID().toString() + "." + suffix.getValue();
ImageIO.write(image, suffix.getValue(), new File(imagePath));
images.add(imagePath);
}
logger.info("new image files:{}", images);
logger.info("thread name [{}] pdf 2 png task success", Thread.currentThread().getName());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return images;
}
}
public class PPT2PDFClient implements Runnable {
private Logger logger = LoggerFactory.getLogger(PPT2PDFClient.class);
private BlockingQueue<String> queue;
private String sourcePath;
private String sourceFileName;
private String targetPath;
private String targetFileName;
public void run() {
logger.info("PPT 2 PDF START!!!");
OpenOfficeConnection connection = null;
try {
File sourceFile = new File(sourcePath + sourceFileName);
if (!sourceFile.exists()) {
logger.info(sourcePath + sourceFileName + " filepath is not found!!!");
throw new NullPointerException("需要轉換的檔案不存在!");
}
// 輸出檔案目錄
File targetFile = new File(targetPath + targetFileName + "." + ResourceSuffix.PDF.getValue());
if (!targetFile.getParentFile().exists()) {
logger.info(targetPath + targetFileName + " targetpath is not found,create field!!!");
targetFile.getParentFile().exists();
}
connection = OpenOfficeClient.getInstance();
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(sourceFile, targetFile);
queue.put(targetPath + targetFile.getName());
logger.info("ppt converter success,date:{}", new Date(System.currentTimeMillis()));
logger.info("ppt converter success,filename:{}", targetPath + File.separator + targetFile.getName());
logger.info("thread name [{}] ppt 2 pdf task success", Thread.currentThread().getName());
} catch (ConnectException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
public PPT2PDFClient(BlockingQueue<String> queue, String sourcePath, String sourceFileName, String targetPath, String targetFileName) {
this.queue = queue;
this.sourcePath = sourcePath;
this.sourceFileName = sourceFileName;
this.targetPath = targetPath;
this.targetFileName = targetFileName;
logger.info("service ppt 2 pdf new create!!!");
}
}
public class PPT2PNGHandleBean {
private static BlockingQueue<String> queue;
private static ExecutorService threadPool;
static {
queue = new LinkedBlockingQueue<>(10);
threadPool = Executors.newCachedThreadPool();
}
public static List<String> ppt2png(String pptSourcePath, String pptSourceName, String pptTargetPath, String pptTargetName, String imageTargetPath) {
PPT2PDFClient pdfClient = new PPT2PDFClient(queue, pptSourcePath, pptSourceName, pptTargetPath, pptTargetName);
PDF2PNGClient pngClient = new PDF2PNGClient(queue, imageTargetPath, ResourceSuffix.PNG);
threadPool.execute(pdfClient);
List<String> aaa = new LinkedList<>();
try {
CompletableFuture<List<String>> future = CompletableFuture.supplyAsync(pngClient);
return future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String separator = File.separator;
String sourcePath = "E:" + separator;
for (int i = 0; i < 50; i++) {
String sourceFileName = "1.ppt";
String targetFileName = String.valueOf(System.currentTimeMillis());
String targetPath = "E:" + separator + i + separator;
List<String> strings = ppt2png(sourcePath, sourceFileName, targetPath, targetFileName, sourcePath + ("image" + i) + separator);
System.out.println(strings);
}
for (int i = 50; i < 100; i++) {
String sourcePath2 = "E:" + separator;
String sourceFileName2 = "2.ppt";
String targetFileName2 = String.valueOf(System.currentTimeMillis());
String targetPath2 = "E:" + separator + i + separator;
List<String> strings1 = ppt2png(sourcePath2, sourceFileName2, targetPath2, targetFileName2, sourcePath2 + ("image" + i) + separator);
System.out.println(strings1);
}
}
}
源碼位址:https://github.com/qichen111/ppt2image