During XSL-FO Transformation we faced some problems in getting all the Unicode characters displayed within the generated XML file. Instead of the Lambda and Delta characters a "#" character was displayed.
To solve this problem you need to provide a Configuration for your FopFactory. Usually you can use the fop.xconf file for that. By providing the tag <auto-detect/>, which automatically tries to resolve the fonts specified within the FO file (works for FOP 0.95, in previous versions you had to manually create the font file in order to embed it into the PDF).
fopFactory.setUserConfig(createAutoDetect());
/**
* Creates the <auto-detect/> swtich to enable auto creation
* of specific fonts like Arial for particular Unicode characters
* like lambda, delta.
*
* Part of fop.xconf which is generated here:
* <?xml version="1.0"?>
* <fop version="1.0">
* <renderers>
* <renderer mime="application/pdf">
* <fonts>
* <auto-detect/>
* </fonts>
* </renderer>
* </renderers>
* </fop>
*
*
* @return auto-detection enabled
*/
private Configuration createAutoDetect() {
DefaultConfiguration fop = new DefaultConfiguration("fop");
DefaultConfiguration renderers = new DefaultConfiguration("renderers");
fop.addChild(renderers);
DefaultConfiguration renderer = new DefaultConfiguration("renderer");
renderer.addAttribute("mime", "application/pdf");
renderers.addChild(renderer);
DefaultConfiguration fonts = new DefaultConfiguration("fonts");
renderer.addChild(fonts);
DefaultConfiguration autoDetect = new DefaultConfiguration("auto-detect");
fonts.addChild(autoDetect);
return fop;
}
Freitag, 22. Oktober 2010
JaxB and Unicode
If you want to marshal Unicode code characters into XML with JaxB you should use the property JAXB_ENCODING with ISO-8859-1. Otherwise the output will result in some unreadable characters.
Applying this property will convert all Unicode characters in their escaped XML representation and it will also escape all control characters like greater.
Unicode characters and their escape sequence: http://www.fileformat.info/info/unicode/char/a.htm
Some Short, Self Contained, Correct (Compilable), Example:
public class TestJaxb {
public static void main(String[] args) throws JAXBException, IOException {
JAXBContext jc = JAXBContext.newInstance(MyElement.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
marshaller.marshal( new MyElement("ü UMLAUT > GREATER","\u03bb LAMBDA"), System.out );
marshaller.marshal( new MyElement("ü UMLAUT","\u03bb LAMBDA"), new FileOutputStream("jaxb.xml"));
}
@XmlRootElement static class MyElement {
@XmlElement public String x;
@XmlAttribute public String y;
MyElement() {}
MyElement(String x, String y) { this.x=x; this.y=y;}
}
}
Applying this property will convert all Unicode characters in their escaped XML representation and it will also escape all control characters like greater.
Unicode characters and their escape sequence: http://www.fileformat.info/info/unicode/char/a.htm
Some Short, Self Contained, Correct (Compilable), Example:
public class TestJaxb {
public static void main(String[] args) throws JAXBException, IOException {
JAXBContext jc = JAXBContext.newInstance(MyElement.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
marshaller.marshal( new MyElement("ü UMLAUT > GREATER","\u03bb LAMBDA"), System.out );
marshaller.marshal( new MyElement("ü UMLAUT","\u03bb LAMBDA"), new FileOutputStream("jaxb.xml"));
}
@XmlRootElement static class MyElement {
@XmlElement public String x;
@XmlAttribute public String y;
MyElement() {}
MyElement(String x, String y) { this.x=x; this.y=y;}
}
}
Abonnieren
Kommentare (Atom)