@ font-face with Flying Saucer

Iโ€™m jumping that someone can help me ... it seems that what I'm trying to do should be pretty simple, but Iโ€™ve been struggling with this for more than a day now and donโ€™t know. I found a lot of information about StackOverflow and the Internet in general, but nothing helped me solve this problem.

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 a PDF from a valid XHTML and embed a font using the style @ font-face tag. I confirmed that the @ font-face tag enables the font by opening the file in a browser. And I always try to keep the TTF-fiel relative to the XHTML / CSS document.

To try to make my way, I created a small program like Hello World to try to embed a font. I took two separate approaches, and both of them could not achieve the desired result. I posted a copy of this little Eclipse program at http://christopherluft.com/FlyingSaucer.zip

The program creates a PDF file in both copies, but none of them has an embedded PDF file. The first method, using a file with setDocument, does not create errors, but also fonts. The second method creates a PDF file, but java.net.MalformedURLException is displayed in the debug output.

I have tried many permutations of various paths and URLs; however, none of them gives the desired result. My suspicion is that I do not understand something about ITextRenderer.setDocument; however, it is very difficult for me to find the appropriate documentation specific to my use case.

The first method I'm trying to do 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 use (which is closer to the actual use method in our application):

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 inclusion of @ font-face in 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 that this is a very common use case, and I believe that I just canโ€™t do one simple step to get this to work ... the problem is that I have been there for a while and I think I canโ€™t see forest through the trees. Any help I can offer would be greatly appreciated. Thank you for your time.

+3
2

Flying Saucer, , , , , , , , . , , . , " " FontBook OS X, :

  • 'name'
  • 'name'

FlyingSaucer Times-Roman Times-Bold . , FontBook, , .

, FlyingSaucer, , , , FontFamily "" , , .

: FlyingSaucer , , , .

+4

, tekgrunt, , , PDF (Computer Modern). :

  • OTF ( iText)
  • TTF DID , : CSS , , .
    org.xhtmlrenderer.pdf.ITextFontResolver.addFontFaceFont()
    , fontFamily. , CSS, , .

, .

+2

All Articles