使用openoffice、LibreOffice转pdf,在通过ppt生成图片

先安装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 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值