2017-05-30 75 views
0

我正在使用docx4j從HTML文本創建PDF文檔。 HTML文本中有一些英文和馬拉地文字。英文文本正確在PDF中。但marathi文本不會顯示在生成的pdf中。如何使用docx4j將帶有Marathi文本的HTML文本寫入PDF文檔?

代替文字,它顯示方框。

以下是我正在使用的代碼。

import java.io.FileOutputStream; 

import org.docx4j.Docx4J; 
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl; 
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 

public class ConvertInXHTMLFragment { 

    static String DEST_PDF = "/home/Downloads/Sample.pdf"; 

    public static void main(String[] args) throws Exception { 

     // String content = "<html>Hello</html>"; 
     String content = "<html>पासवर्ड</html>"; 

     WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); 

     XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage); 

     wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(content, null)); 

     Docx4J.toPDF(wordMLPackage, new FileOutputStream(DEST_PDF)); 
    } 

} 

編輯1: -

這是一個樣品從XSLFO

import java.io.OutputStream; 

import org.docx4j.Docx4J; 
import org.docx4j.convert.out.FOSettings; 
import org.docx4j.fonts.IdentityPlusMapper; 
import org.docx4j.fonts.Mapper; 
import org.docx4j.fonts.PhysicalFont; 
import org.docx4j.fonts.PhysicalFonts; 
import org.docx4j.model.fields.FieldUpdater; 
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
import org.docx4j.samples.AbstractSample; 

public class ConvertOutPDFviaXSLFO extends AbstractSample { 

    static { 
     inputfilepath = "/home/Downloads/100.docx";; 
     saveFO = true; 
    } 

    static boolean saveFO; 

    public static void main(String[] args) 
      throws Exception { 

     try { 
      getInputFilePath(args); 
     } catch (IllegalArgumentException e) { 
     } 

     String regex = null; 
     PhysicalFonts.setRegex(regex); 

     WordprocessingMLPackage wordMLPackage; 
     System.out.println("Loading file from " + inputfilepath); 
     wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath)); 

     FieldUpdater updater = null; 

     Mapper fontMapper = new IdentityPlusMapper(); 
     wordMLPackage.setFontMapper(fontMapper); 

     PhysicalFont font = PhysicalFonts.get("Arial Unicode MS"); 
     fontMapper.put("Mangal", font); 

     FOSettings foSettings = Docx4J.createFOSettings(); 
     if (saveFO) { 
      foSettings.setFoDumpFile(new java.io.File(inputfilepath + ".fo")); 
     } 
     foSettings.setWmlPackage(wordMLPackage); 

     String outputfilepath; 
     if (inputfilepath==null) { 
      outputfilepath = System.getProperty("user.dir") + "/OUT_FontContent.pdf";   
     } else { 
      outputfilepath = inputfilepath + ".pdf"; 
     } 
     OutputStream os = new java.io.FileOutputStream(outputfilepath); 

     Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL); 

     System.out.println("Saved: " + outputfilepath); 

     if (wordMLPackage.getMainDocumentPart().getFontTablePart()!=null) { 
      wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles(); 
     } 

     // This would also do it, via finalize() methods 
     updater = null; 
     foSettings = null; 
     wordMLPackage = null; 
    } 
} 

現在,我得到####代替馬拉文本輸出PDF 。

+0

什麼流行/標準字體具有馬拉蒂字形?他們在Arial Unicode MS嗎? – JasonPlutext

+0

@JasonPlutext Mangal字體http://indiatyping.com/index.php/download/mangal-font – ashishjmeshram

+0

@JasonPlutext Arial Unicode MS也應該工作。 – ashishjmeshram

回答

0

Docx4j v3.3通過兩種完全不同的方式支持PDF輸出。

默認使用的是Plutext的PDF Converter。如果要安裝在Conveter你鏈接的字體曼加勒,並在指定的docx工作的事情:

<w:r> 
    <w:rPr> 
     <w:rFonts w:ascii="mangal" w:eastAsia="mangal" w:hAnsi="mangal" w:cs="mangal"/> 
    </w:rPr> 
    <w:t>पासवर्ड</w:t> 
    </w:r> 

同樣適用Arial字體的Unicode MS。

另一種方式是PDF通過XSL FO;看到https://github.com/plutext/docx4j-export-FO

如果您有相關的字體安裝它應該只是工作。如果你不這樣做,那麼你需要告訴它使用哪種字體。

例如,假設docx指定了mangal字體,我沒有。但我有Arial Unicode MS。所以我告訴XSL FO工藝而改用:

fontMapper.put("mangal", PhysicalFonts.get("Arial Unicode MS")); 

注意,你需要知道哪些字體你的docx的規定,以及如何使指定所需的字體。要做到這一點在XHTML導入,從我的答案複製到你剛纔的問題: -

字體由 https://github.com/plutext/docx4j-ImportXHTML/blob/master/src/main/java/org/docx4j/convert/in/xhtml/FontHandler.java#L58

馬拉可能依賴於其他屬性之一,在RFonts 對象處理。您需要查看正在工作的docx才能看到。您可以使用 https://github.com/plutext/docx4j-ImportXHTML/blob/master/src/main/java/org/docx4j/convert/in/xhtml/FontHandler.java#L54 注入適當的字體映射。

+0

我試圖調查XSL FO。現在我用##########代替馬拉地語文本。 – ashishjmeshram

+0

另外,PhysicalFonts.get(「Arial Unicode MS」)總是給我null。 – ashishjmeshram

+0

Arial Unicode MS是否安裝在您的計算機上?如果不是,你會得到空值。 – JasonPlutext

相關問題