我正在使用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 。
什麼流行/標準字體具有馬拉蒂字形?他們在Arial Unicode MS嗎? – JasonPlutext
@JasonPlutext Mangal字體http://indiatyping.com/index.php/download/mangal-font – ashishjmeshram
@JasonPlutext Arial Unicode MS也應該工作。 – ashishjmeshram