Hi again, thanks for the quick reply. I have converted one document with (some) success, however, I'm getting a lot of errors and warnings which I assume are the reason for the document not being converted in an acceptable way.
I have tried to google, and search this forum, but I'm not finding any help, so I'm hoping someone here is able to explain the errors to me and how I go about them.
I get 1
- Code: Select all
9250 [main] WARN org.docx4j.fonts.PhysicalFonts - Aborting: file:/C:/WINDOWS/FONTS/ALGER.TTF (can't get EmbedFontInfo[] .. try deleting fop-fonts.cache?)
and about 100 of
- Code: Select all
9266 [main] WARN org.docx4j.fonts.PhysicalFonts - Aborting: file:/C:/WINDOWS/FONTS/BAUHS93.TTF (can't get EmbedFontInfo[] .. try deleting fop-fonts.cache?)
and because of those I get
- Code: Select all
9500 [main] WARN org.docx4j.fonts.IdentityPlusMapper - - - No physical font for: Cambria
And this one
- Code: Select all
9594 [main] ERROR org.apache.fop.fo.extensions.svg.SVGElementMapping - Error while initializing the Batik SVG extensions
java.lang.NoClassDefFoundError: org/apache/batik/util/XMLResourceDescriptor
at org.apache.fop.fo.extensions.svg.SVGElementMapping.initialize(SVGElementMapping.java:80)
at org.apache.fop.fo.ElementMapping.getTable(ElementMapping.java:54)
at org.apache.fop.fo.ElementMappingRegistry.addElementMapping(ElementMappingRegistry.java:118)
at org.apache.fop.fo.ElementMappingRegistry.addElementMapping(ElementMappingRegistry.java:97)
at org.apache.fop.fo.ElementMappingRegistry.setupDefaultMappings(ElementMappingRegistry.java:78)
at org.apache.fop.fo.ElementMappingRegistry.<init>(ElementMappingRegistry.java:65)
at org.apache.fop.apps.FopFactory.<init>(FopFactory.java:154)
at org.apache.fop.apps.FopFactory.newInstance(FopFactory.java:177)
at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:231)
at jd2p.Jd2p.main(Jd2p.java:84)
- Code: Select all
9985 [main] ERROR org.docx4j.convert.out.pdf.viaXSLFO.Conversion - Document font Cambria is not mapped to a physical font!
And finally the complete output and sourcecode
- Code: Select all
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jd2p;
/**
*
* @author kk
*/
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.bind.JAXBException;
import org.docx4j.XmlUtils;
import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFont;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
public class Jd2p extends AbstractSample {
public static void main(String[] args)
throws Exception {
boolean save = true;
/*
try {
getInputFilePath(args);
} catch (IllegalArgumentException e) {
// inputfilepath = System.getProperty("user.dir") + "/sample-docs/sample-docx.xml";
// inputfilepath = System.getProperty("user.dir") + "/docs/Docx4j_GettingStarted.xml";
}
*/
inputfilepath = "skabelon_entk_1.docx";
WordprocessingMLPackage wordMLPackage;
if (inputfilepath==null) {
// Create a docx
// If this is to be saved..
inputfilepath = System.getProperty("user.dir") + "/tmp/output";
/*
* NB, this currently works nicely with
* viaIText, and viaXSLFO (provided
* you view with Acrobat Reader .. it
* seems to overwhelm pdfviewer, which
* is weird, since viaIText works in both).
*/
wordMLPackage = WordprocessingMLPackage.createPackage();
createContent(wordMLPackage.getMainDocumentPart());
} else {
// Load .docx or Flat OPC .xml
wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
}
// Set up font mapper
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
// Example of mapping missing font Algerian to installed font Comic Sans MS
PhysicalFont font
= PhysicalFonts.getPhysicalFonts().get("Comic Sans MS");
fontMapper.getFontMappings().put("Algerian", font);
// As of docx4j 2.5.0, only viaXSLFO is supported.
// The viaIText and viaHTML source code can be found in src/docx4j-extras directory
org.docx4j.convert.out.pdf.PdfConversion c
// = new org.docx4j.convert.out.pdf.viaHTML.Conversion(wordMLPackage);
= new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
// = new org.docx4j.convert.out.pdf.viaIText.Conversion(wordMLPackage);
if (save) {
((org.docx4j.convert.out.pdf.viaXSLFO.Conversion)c).setSaveFO(
new java.io.File(inputfilepath + ".fo"));
OutputStream os = new java.io.FileOutputStream(inputfilepath + ".pdf");
c.output(os, new PdfSettings() );
System.out.println("Saved " + inputfilepath + ".pdf");
}
}
public static void createContent(MainDocumentPart wordDocumentPart ) {
try {
// Do this explicitly, since we need
// it in order to create our content
PhysicalFonts.discoverPhysicalFonts();
Map<String, PhysicalFont> physicalFontMap = PhysicalFonts.getPhysicalFonts();
Iterator physicalFontMapIterator = physicalFontMap.entrySet().iterator();
while (physicalFontMapIterator.hasNext()) {
Map.Entry pairs = (Map.Entry)physicalFontMapIterator.next();
if(pairs.getKey()==null) {
pairs = (Map.Entry)physicalFontMapIterator.next();
}
String fontName = (String)pairs.getKey();
PhysicalFont pf = (PhysicalFont)pairs.getValue();
System.out.println("Added paragraph for " + fontName);
addObject(wordDocumentPart, sampleText, fontName );
// bold, italic etc
PhysicalFont pfVariation = PhysicalFonts.getBoldForm(pf);
if (pfVariation!=null) {
addObject(wordDocumentPart, sampleTextBold, pfVariation.getName() );
}
pfVariation = PhysicalFonts.getBoldItalicForm(pf);
if (pfVariation!=null) {
addObject(wordDocumentPart, sampleTextBoldItalic, pfVariation.getName() );
}
pfVariation = PhysicalFonts.getItalicForm(pf);
if (pfVariation!=null) {
addObject(wordDocumentPart, sampleTextItalic, pfVariation.getName() );
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void addObject(MainDocumentPart wordDocumentPart, String template, String fontName ) throws JAXBException {
HashMap substitution = new HashMap();
substitution.put("fontname", fontName);
Object o = XmlUtils.unmarshallFromTemplate(template, substitution);
wordDocumentPart.addObject(o);
}
final static String sampleText = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
+"<w:r>"
+"<w:rPr>"
+"<w:rFonts w:ascii=\"${fontname}\" w:eastAsia=\"${fontname}\" w:hAnsi=\"${fontname}\" w:cs=\"${fontname}\" />"
+"</w:rPr>"
+"<w:t xml:space=\"preserve\">${fontname}</w:t>"
+"</w:r>"
+"</w:p>";
final static String sampleTextBold = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" +"<w:r>"
+"<w:rPr>"
+"<w:rFonts w:ascii=\"${fontname}\" w:eastAsia=\"${fontname}\" w:hAnsi=\"${fontname}\" w:cs=\"${fontname}\" />"
+"<w:b />"
+"</w:rPr>"
+"<w:t>${fontname} bold;</w:t>"
+"</w:r>"
+"</w:p>";
final static String sampleTextItalic = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" +"<w:r>"
+"<w:rPr>"
+"<w:rFonts w:ascii=\"${fontname}\" w:eastAsia=\"${fontname}\" w:hAnsi=\"${fontname}\" w:cs=\"${fontname}\" />"
+"<w:i />"
+"</w:rPr>"
+"<w:t>${fontname} italic; </w:t>"
+"</w:r>"
+"</w:p>";
final static String sampleTextBoldItalic ="<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
+"<w:r>"
+"<w:rPr>"
+"<w:rFonts w:ascii=\"${fontname}\" w:eastAsia=\"${fontname}\" w:hAnsi=\"${fontname}\" w:cs=\"${fontname}\" />"
+"<w:b />"
+"<w:i />"
+"</w:rPr>"
+"<w:t>${fontname} bold italic</w:t>"
+"</w:r>"
+"</w:p>";
}