I am hopping somebody can help me... it seems like what I am trying to do should be fairly simple but I have been fighting this thing for over a day now and am out of ideas. I have found lots of information on StackOverflow and the Internet at large but nothing has helped me resolve this issue.
I am trying to use itext-2.0.8 along with core-renderer-R8 to create a PDF with an embedded font. I am trying to generate the PDF from valid XHTML and am embedding the font using the @font-face style tag. I have confirmed that the @font-face tag is including the font by opening the file in a browser. And I am always careful to keep the TTF fiel relative to teh XHTML/CSS doc.
In order to try and work my way through this I have created a small 'Hello World' type program to try and embed a font. I have taken two separate approaches and both of them fail to produce the desired result. I have place a copy of this little Eclipse program at http://christopherluft.com/FlyingSaucer.zip
The program produces a PDF in both instances but neither has the PDF embedded as expected. The first method using a file with setDocument produces no errors but also no fonts. And the second method produces a PDF but shows a java.net.MalformedURLException in the debug output.
I have tried numerous permutations of the various paths and URLs; however, none fail to produce the desired result. My suspicion is that I am failing to understand something about ITextRenderer.setDocument; however, I have had a really hard time finding any proper documentation specific to my use case.
The first method I am trying is:
public static void main(String[] args) throws IOException, DocumentException {
System.getProperties().setProperty("xr.util-logging.loggingEnabled",
"true");
XRLog.setLoggingEnabled(true);
String inputFile = "sample.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
And the second method I am using (which is closer to the actual way we use it in our app) is:
public static void main(String[] args) throws IOException, DocumentException {
System.getProperties().setProperty("xr.util-logging.loggingEnabled", "true");
XRLog.setLoggingEnabled(true);
String inputFile = "sample.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
DocumentBuilder documentBuilder;
org.w3c.dom.Document xhtmlContent;
try
{
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
documentBuilder.setEntityResolver(new XhtmlEntityResolver(new SuppressingEntityResolver()));
xhtmlContent = documentBuilder.parse(url);
System.out.println(url);
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(xhtmlContent,".");
renderer.layout();
renderer.createPDF(os);
System.out.println("Finishing up....");
os.close();
}
catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
And the @font-face inclusion in the XHTML looks like this:
@font-face {
font-family: 'MisoRegular';
src: url("miso-regular-webfont.ttf");
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
Now I feel like this is a really common use case and I imagine that I am just failing to perform one simple step in order to get this to work... the problem is that I have been at this for a while and think I am unable to see the forest through the trees. Any help anybody could offer me would be greatly appreciated. Thank you for your time.