Java:从doc转换为pdf,ppt转换为pdf失败

时间:2016-02-08 09:35:42

标签: java pdf powerpoint doc file-conversion

对于我们的Java项目,我正在研究将office文件转换为PDF,然后转换为图像。目前,我通过pptxdocxxlsxlsxpdf成功获得了Image。如果有人要求上面提到的工作代码,请知道。

不幸的是,docPDFpptPDF无效。我尝试过多种解决方案,但似乎都没有。我尝试过的最新版本是JODConvertor,但也失败了。 JodConvertor库无法连接到我在给定端口运行的libreoffice。

任何人都可以给我一些可靠的方法来转换DOC&& PPTPDF哪些是免费的?

代码:

  private String createDocToPDfAndThenToImage(String path) {

        try {
            File inputFile = new File(path);
            File outputFile = File.createTempFile("/home/akshay/jodtest", ".pdf");

            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
            connection.connect();

            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            converter.convert(inputFile, outputFile);
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

错误日志:

java.net.ConnectException: connection failed: socket,host=127.0.0.1,port=8100,tcpNoDelay=1: java.net.ConnectException: Connection refused
    at com.artofsolving.jodconverter.openoffice.connection.AbstractOpenOfficeConnection.connect(AbstractOpenOfficeConnection.java:79)
    at com.journaldev.spring.service.GroupAttachmentsServiceImpl.createDocToPDfAndThenToImage(GroupAttachmentsServiceImpl.java:406)
    at com.journaldev.spring.service.GroupAttachmentsServiceImpl.addAttachment(GroupAttachmentsServiceImpl.java:338)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

已经使用此命令启动了无头实例:

 /usr/bin/libreoffice --headless --accept=socket,host=localhost,port=8100;

如果无法解决此问题,任何其他转换机制都可以。请告诉我。谢谢。

2 个答案:

答案 0 :(得分:1)

我正在做类似的事情。我使用unoconv,这需要libreoffice。它在PPT / PPTX上非常可靠。它也适用于DOC / DOCX,但有时它会卡住并需要干预。

这是循环文件夹以处理新PPT / PPTX文件的脚本。它用于生产。您也可以添加DOC / DOCX。我有一个java应用程序在文件夹上侦听新文件创建事件,因此它可以在转换完成后处理转换后的pdf文件。

所有这些状态标志文件都用于其他脚本的人为干预和状态检查。您还需要为旧文件清理添加一些代码。

顺便说一句,我对将文件转换为PDF然后转换为图像的方式非常感兴趣。也许它可以给我提示为我的项目找到更好的解决方案。感谢。

    #!/bin/bash

    #endless conversion

    echo "Convert files to pdf..."

    while [ 1 ]
    do
        for file in `ls -tr | grep '.*\.\(ppt\|pptx\)$'`
        do
        if [ -e $file.failed ] || [ -e $file.succeeded ] # already converted
        then
            :
        else
            echo $file > $file.started 
            echo $file > convert.busy # Create busy converting flag file
            output=`./unoconv -f pdf $file`
            result=$?
            echo $result
            if [ $result -ne 0 ]
            then
            echo "conversion to pdf failed"
            echo $file > $file.failed
            else
            echo "conversion to pdf succeeded"
            echo $file > $file.succeeded
            fi
            rm convert.busy # remove busy converting flag file
        fi
        done
        sleep 0.2
    done

答案 1 :(得分:0)

使用OpenOffice。下载OpenOffice.and使用此代码从doc / docx转换为PDF

依赖

编译“org.openoffice:bootstrap-connector:0.1.1”

编译“org.openoffice:unoil:4.1.2”

编译“org.openoffice:ridl:4.1.2”

编译“org.openoffice:jurt:4.1.2”

编译“org.openoffice:juh:4.1.2”

package com.galantis.ecm.converter

import com.galantis.ecm.api.object.model.BaseContent
import org.apache.commons.io.FileUtils

import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.frame.XComponentLoader
import ooo.connector.BootstrapSocketConnector;

class Docx2PdfConverter extends Converter {
  InputStream convert(BaseContent content) {
    try {
      byte[] bytes = content.inputStream.bytes
      def file = new File(FileUtils.getTempDirectory(), "doc.docx")
      FileUtils.writeByteArrayToFile(file, bytes)
      String oooExeFolder = "C:/Program Files (x86)/OpenOffice 4/program";
      XComponentContext xContext = BootstrapSocketConnector.bootstrap(oooExeFolder);
      XMultiComponentFactory xMCF = xContext.getServiceManager();
      Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
      XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
        XDesktop.class, oDesktop);

      XComponentLoader xCompLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, xDesktop);
      String sUrl = "file:///" +  file.getAbsolutePath()
      PropertyValue[] propertyValues = new PropertyValue[0];
      propertyValues = new PropertyValue[1];
      propertyValues[0] = new PropertyValue();
      propertyValues[0].Name = "Hidden";
      propertyValues[0].Value = new Boolean(true);

      XComponent xComp = xCompLoader.loadComponentFromURL(sUrl, "_blank", 0, propertyValues);

      XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, xComp);

      propertyValues = new PropertyValue[2];
      propertyValues[0] = new PropertyValue();
      propertyValues[0].Name = "Overwrite";
      propertyValues[0].Value = new Boolean(true);
      propertyValues[1] = new PropertyValue();
      propertyValues[1].Name = "FilterName";
      propertyValues[1].Value = "writer_pdf_Export";

// Appending the favoured extension to the origin document name
      def outPutPdf = new File(FileUtils.getTempDirectory(), "pdf.pdf")
      String myResult = "D:/4.pdf"
      xStorable.storeToURL("file:///" + myResult, propertyValues);

      def result =  new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(myResult)));
      xDesktop.terminate();
      result
    } catch (Exception e) {
      throw e
    }

  }
}