Java:从文档转换为pdf和ppt转换为pdf失败
对于我们的Java项目,我希望将office文件转换为PDF,然后再转换为图像。目前,我成功地使用了Java:从文档转换为pdf和ppt转换为pdf失败,java,pdf,powerpoint,doc,file-conversion,Java,Pdf,Powerpoint,Doc,File Conversion,对于我们的Java项目,我希望将office文件转换为PDF,然后再转换为图像。目前,我成功地使用了pptx,docx,xls,xlsx,pdf到图像。如果有人需要上面提到的工作代码,让我知道 不幸的是,doctoPDF和ppttoPDF不起作用。我尝试过多种解决方案,但似乎都不管用。我最近尝试的是JODConvertor,但也失败了。JodConvertor库无法连接到libreoffice,我正在给定端口上运行它 有谁能给我一些可靠的方法把DOC&&PPT转换成PDF,而且是免费的 代码:
pptx
,docx
,xls
,xlsx
,pdf
到图像。如果有人需要上面提到的工作代码,让我知道
不幸的是,doc
toPDF
和ppt
toPDF
不起作用。我尝试过多种解决方案,但似乎都不管用。我最近尝试的是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;
如果没有办法解决这个问题,任何其他转换机制都可以工作。请让我知道。多谢各位 我正在做类似的事情。我使用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
使用OpenOffice。下载OpenOffice.com并使用此代码将doc/docx转换为PDF 依赖关系 编译“org.openoffice:bootstrap连接器: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”
是的,我现在用的是libreoffice.它好多了。。。我也会发布我的代码。期待它。
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
}
}
}