简体   繁体   English

Jodconverter:我可以用来将HTML转换为PDF吗?

[英]Jodconverter : Can i use to convert HTML into PDF?

I have a word file. 我有一个word文件。 May i use jodconverter to convert into pdf. 我可以使用jodconverter转换成pdf吗? I have used jodconverter from doc to pdf and it gives very good results. 我使用了从doc到pdf的jodconverter,它给出了很好的结果。 But i don't know weather it support from HTML to pdf. 但是我不知道天气支持从HTML到pdf。

Thanks! 谢谢!

From what I can see about Jodconverter I understand that it uses LibreOffice or OpenOffice to export doc files to PDF. 从我对Jodconverter的了解中,我了解到它使用LibreOffice或OpenOffice将文档文件导出为PDF。 Now that software can't fully support Microsoft Office. 现在,该软件不能完全支持Microsoft Office。 Only Microsoft Office can fully support their format and thus use one of the above software to convert your docx files may be possible but in some cases (or in all of them) with unpleasant results. 仅Microsoft Office可以完全支持其格式,因此可以使用上述软件之一来转换您的docx文件,但在某些情况下(或在所有情况下)均会产生不愉快的结果。 From what I know you have two options : 据我了解,您有两种选择:

  1. You could turn all your docx documents to LibreOffice and then export them to PDF without compatibility issues using the LibreOffice software ( or your external java library ) 您可以使用LibreOffice软件(或外部Java库)将所有docx文档转换为LibreOffice,然后将它们导出为PDF,而不会出现兼容性问题。

  2. You could use the Microsoft Office Word to do the conversion from docx to pdf. 您可以使用Microsoft Office Word进行从docx到pdf的转换。 In order to achieve that you will need a script telling the MS Word to make the conversion and call that Script from Java using Runtime.getRuntime().exec(command); 为了实现此目的,您将需要一个脚本来告诉MS Word进行转换,并使用Runtime.getRuntime().exec(command);从Java调用该脚本Runtime.getRuntime().exec(command); . At least that's the way I know, and doing it. 至少那是我知道的方式,并且做到了。

Here is the script I found out years ago and still use in some cases. 这是我几年前发现的脚本,在某些情况下仍然可以使用。 The script is not mine so I can't take any credits. 该脚本不是我的,所以我不能功劳。 All you need to do is to make a new file with extension .vbs and add the code below. 您要做的就是创建一个扩展名为.vbs的新文件,并添加以下代码。

Option Explicit
Doc2PDF Wscript.Arguments.Item(0)
Sub Doc2PDF( myFile )
    Dim objDoc, objFile, objFSO, objWord, strFile, strPDF
   Const wdFormatDocument                    =  0
   Const wdFormatDocument97                  =  0
   Const wdFormatDocumentDefault             = 16
   Const wdFormatDOSText                     =  4
   Const wdFormatDOSTextLineBreaks           =  5
   Const wdFormatEncodedText                 =  7
   Const wdFormatFilteredHTML                = 10
   Const wdFormatFlatXML                     = 19
   Const wdFormatFlatXMLMacroEnabled         = 20
   Const wdFormatFlatXMLTemplate             = 21
   Const wdFormatFlatXMLTemplateMacroEnabled = 22
   Const wdFormatHTML                        =  8
   Const wdFormatPDF                         = 17
   Const wdFormatRTF                         =  6
   Const wdFormatTemplate                    =  1
   Const wdFormatTemplate97                  =  1
   Const wdFormatText                        =  2
   Const wdFormatTextLineBreaks              =  3
   Const wdFormatUnicodeText                 =  7
   Const wdFormatWebArchive                  =  9
   Const wdFormatXML                         = 11
   Const wdFormatXMLDocument                 = 12
   Const wdFormatXMLDocumentMacroEnabled     = 13
   Const wdFormatXMLTemplate                 = 14
   Const wdFormatXMLTemplateMacroEnabled     = 15
   Const wdFormatXPS                         = 18
   Const wdFormatOfficeDocumentTemplate      = 23
   Const wdFormatMediaWiki                   = 24 
   Set objFSO = CreateObject( "Scripting.FileSystemObject" )
   Set objWord = CreateObject( "Word.Application" )
   With objWord
       .Visible = false
       If objFSO.FileExists( myFile ) Then
           Set objFile = objFSO.GetFile( myFile )
           strFile = objFile.Path
       Else
           WScript.Echo "FILE OPEN ERROR: The file does not exist" & vbCrLf
           .Quit
           Exit Sub
       End If
       strPDF = objFSO.BuildPath( objFile.ParentFolder, _
                objFSO.GetBaseName( objFile ) & ".pdf" )
       .Documents.Open strFile
       Set objDoc = .ActiveDocument
     objDoc.SaveAs strPDF, wdFormatPDF
      objDoc.Close
       .Quit
   End With
End Sub

Here is a small example with the method you could use to export docx files to PDF : 这是一个可用于将docx文件导出为PDF的方法的小示例:

private void convertFile(String text) {
    File f = new File(text);

    if (f.exists() && f.getName().endsWith(".docx")) {
        // The full path of your script, in my case is the Desktop.
        String converterLocation = System.getProperty("user.home") + "\\Desktop\\wordToPdf.vbs";

        // wscript <converterLocation> <"fullFilePath">
        String command = "wscript " + converterLocation + " \"" + f.getAbsolutePath();

        try {
            Process p = Runtime.getRuntime().exec(command);
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I know that this answer is not what you expect but I just want to point out a solution of your problem that i am aware of. 我知道这个答案不是您期望的,但我只想指出我所知道的您的问题的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM