2016-05-17 115 views
2

我正在尋找一個很好的轉換器來將office文件和圖像轉換爲pdf。我想嘗試免費的來源Documents4J。而有這樣的代碼:將office文件轉換爲pdf與documents4J

package documentsForJ; 
import java.io.File; 
import java.util.concurrent.Future; 
import java.util.concurrent.TimeUnit; 
import com.documents4j.api.DocumentType; 
import com.documents4j.api.IConverter; 
import com.documents4j.job.LocalConverter; 

public class App { 
    public static void main(String[] args) { 

     File wordFile = new File("test.doc"), target = new File("test.pdf"); 

     IConverter converter = LocalConverter.builder().baseFolder(new File("G:\\documentsForJ\\target")) 
       .workerPool(20, 25, 2, TimeUnit.SECONDS) 
       .processTimeout(5, TimeUnit.SECONDS).build(); 

     Future<Boolean> conversion = converter.convert(wordFile).as(DocumentType.DOCX).to(target).as(DocumentType.PDF) 
       // .prioritizeWith(1000) // optional 
       .schedule(); 

    } 
} 

我有以下罐子綁定:

  • documents4j-API-1.0.2.jar documents4j-客戶1.0.2.jar
  • documents4j客戶端-standalone-1.0.2-shaded.jar
  • documents4j本地-1.0.2.jar
  • documents4j服務器 - 獨立-1.0.2-shaded.jar
  • documents4j試驗1.0.2.jar DOC uments4j變壓器-API-1.0.2.jar
  • documents4j-變壓器MSOFFICE鹼基1.0.2.jar
  • documents4j-變壓器MSOFFICE-Excel的1.0.2.jar
  • documents4j-變壓器 - MSOFFICE - 測試 - 1.0.2.jar
  • documents4j-變壓器MSOFFICE字-1.0.2.jar
  • documents4j-util的-獨立-1.0.2.jar
  • documents4j-util的變壓器-工序 - 1.0.2.jar
  • documents4j-util-ws-1.0.2.jar documents4j-server-1.0.2.jar
  • documents4j變壓器-1.0.2.jar documents4j-UTIL-全1.0.2.jar
  • documents4j-util的轉換-1.0.2.jar

德恩我跑我得到的應用以下錯誤:

SLF4J: Class path contains multiple SLF4J bindings. 
SLF4J: Found binding in [jar:file:/G:/AdminPrgs/Code/Java/eclipse/workspaceTest/documentsForJ/lib/documents4j-client-standalone-1.0.2-shaded.jar!/org/slf4j/impl/StaticLoggerBinder.class] 
SLF4J: Found binding in [jar:file:/G:/AdminPrgs/Code/Java/eclipse/workspaceTest/documentsForJ/lib/documents4j-server-standalone-1.0.2-shaded.jar!/org/slf4j/impl/StaticLoggerBinder.class] 
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. 
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] 
16:28:49.030 [main] DEBUG o.z.exec.ProcessExecutor - Executing [cmd, /C, "C:\temp\excel_start1565707660.vbs"] in C:\temp. 
16:28:49.038 [main] DEBUG o.z.exec.ProcessExecutor - Started [email protected] 
16:28:49.074 [[email protected]] DEBUG o.zeroturnaround.exec.WaitForProcess - [email protected] stopped with exit code 3 
16:28:49.081 [main] INFO c.d.c.msoffice.MicrosoftExcelBridge - From-Microsoft-Excel-Converter was started successfully 
16:28:49.085 [main] DEBUG o.z.exec.ProcessExecutor - Executing [cmd, /C, "C:\temp\word_start2143216009.vbs"] in C:\temp. 
16:28:49.086 [main] DEBUG o.z.exec.ProcessExecutor - Started [email protected] 
16:28:49.121 [[email protected]] DEBUG o.zeroturnaround.exec.WaitForProcess - [email protected] stopped with exit code 3 
16:28:49.121 [main] INFO c.d.c.msoffice.MicrosoftWordBridge - From-Microsoft-Word-Converter was started successfully 
16:28:49.132 [main] INFO com.documents4j.job.LocalConverter - The documents4j local converter has started successfully 
fertig! 
16:28:49.145 [pool-1-thread-1] INFO c.d.c.msoffice.MicrosoftExcelBridge - Requested conversion from AbrechnungKFZ-Kosten.xls (application/vnd.ms-excel) to AbrechnungKFZ-Kosten.pdf (application/pdf) 
16:28:49.148 [pool-1-thread-1] DEBUG o.z.exec.ProcessExecutor - Executing [cmd, /C, "C:\temp\excel_convert961528339.vbs G:\AdminPrgs\Code\Java\eclipse\workspaceTest\documentsForJ\AbrechnungKFZ-Kosten.xls G:\AdminPrgs\Code\Java\eclipse\workspaceTest\documentsForJ\AbrechnungKFZ-Kosten.pdf 999"] in C:\temp. 
16:28:49.148 [pool-1-thread-1] DEBUG o.z.exec.ProcessExecutor - Started [email protected] 
16:28:49.188 [[email protected]] DEBUG o.zeroturnaround.exec.WaitForProcess - [email protected] stopped with exit code -4 

所生成的VBS C:\ TEMP \ excel_start1565707660.vbs提供了以下錯誤:enter image description here

而這是所生成的VBS的代碼:

' See http://msdn.microsoft.com/en-us/library/bb243311%28v=office.12%29.aspx 
Const WdExportFormatPDF = 17 
Const MagicFormatPDF = 999 

Dim arguments 
Set arguments = WScript.Arguments 

' Transforms a file using MS Excel into the given format. 
Function ConvertFile(inputFile, outputFile, formatEnumeration) 

    Dim fileSystemObject 
    Dim excelApplication 
    Dim excelDocument 

    ' Get the running instance of MS Excel. If Excel is not running, exit the conversion. 
    On Error Resume Next 
    Set excelApplication = GetObject(, "Excel.Application") 
    If Err <> 0 Then 
    WScript.Quit -6 
    End If 
    On Error GoTo 0 

    ' Find the source file on the file system. 
    Set fileSystemObject = CreateObject("Scripting.FileSystemObject") 
    inputFile = fileSystemObject.GetAbsolutePathName(inputFile) 


    ' Convert the source file only if it exists. 
    If fileSystemObject.FileExists(inputFile) Then 

    ' Attempt to open the source document. 
    On Error Resume Next 
    Set excelDocument = excelApplication.Workbooks.Open(inputFile, , True, , , , , , , , , , , , 2) 
    If Err <> 0 Then 
     WScript.Quit -2 
    End If 
    On Error GoTo 0 

    ' Convert: See http://msdn2.microsoft.com/en-us/library/bb221597.aspx 
    On Error Resume Next 
    If formatEnumeration = MagicFormatPDF Then 
     excelDocument.ExportAsFixedFormat xlTypePDF, outputFile 
    Else 
     excelDocument.SaveAs outputFile, formatEnumeration 
    End If 

    ' Close the source document. 
    excelDocument.Close False 
    If Err <> 0 Then 
     WScript.Quit -3 
    End If 
    On Error GoTo 0 

    ' Signal that the conversion was successful. 
    WScript.Quit 2 

    Else 

    ' Files does not exist, could not convert 
    WScript.Quit -4 

    End If 

End Function 

' Execute the script. 
Call ConvertFile(arguments.Unnamed.Item(0), arguments.Unnamed.Item(1), CInt(arguments.Unnamed.Item(2))) 

我會非常好,如果你能給我任何建議,如何解決這個錯誤。提前感謝!

+0

看起來你可能會在文件的某個地方使用特殊字符。 VBS抱怨說你的腳本中提供的參數少於三個。 Currentlty,有一個錯誤,任何文件名中的空格都不能正確轉義並且轉換失敗。你的情況似乎有類似的問題。 –

+0

給出test.doc的正確路徑,好像腳本無法找到它們。嘗試把它放在一個沒有空格的文件夾中,並將其引用爲'File wordFile = new File(「C:\\ test \\ test.doc」);'帶空格的文件夾也可以工作,但先試試這個。 –

回答

0

這個問題很有可能在新發布的documents4j版本1.0.3中解決,該版本1.0.3會正確轉義轉換腳本參數。以前,沒有任何參數被轉義,當VBScript處理您觀察到的腳本參數時會引起混淆。