【问题标题】:Java : Convert from doc to pdf and ppt to pdf failingJava:从 doc 转换为 pdf 和 ppt 转换为 pdf 失败
【发布时间】:2016-05-17 22:01:57
【问题描述】:

对于我们的 Java 项目,我正在考虑将办公文件转换为 PDF,然后转换为图像。目前,我通过pptxdocxxlsxlsxpdf 对 Image 取得了成功。如果有人需要上述工作代码,请告诉我。

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

谁能给我一些可靠的方法来将DOC && PPT 转换为PDF 并且哪些是免费的?

代码:

  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;

如果没有办法解决这个问题,任何其他转换机制都可以工作。请告诉我。谢谢你。

【问题讨论】:

    标签: java pdf powerpoint doc file-conversion


    【解决方案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
    

    【讨论】:

    • 是的,我现在正在使用 libreoffice.. 它好多了...我也会发布我的代码。
    • 期待。
    【解决方案2】:

    使用 OpenOffice。下载 OpenOffice. 并使用此代码从 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
        }
    
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 2013-08-28
      • 2012-02-05
      • 2014-12-09
      相关资源
      最近更新 更多