Velocity PDF rendering problem

Hi,

We are running in to a problem with Velocity. We are using it to render HTML into PDF. We are using a rich text editor for portions of this process. The problem with this is that in those editors (tinyMCE, CKEditor, native Servoy editor in 6) you can’t control the html code that gets generated. As it turns out Velocity-PDF (as in iText) does not handle nested DOM elements in the correct way. It look like all formatting that is nested is ignored completely.

So when I have a template with in it contents like:

<p><span style="font-family: Tahoma; font-size: 20px;">This is my text in <strong>bold</strong></span></p>

It will print the full text in plain face (not bold), but when I try:

<p><span style="font-family: Tahoma; font-size: 20px;">This is my text in</span><strong><span style="font-family: Tahoma; font-size: 20px;">bold</span></strong></p>

It will ignore the font-face and pixel-size in the “bold” word.

I know when you write the entire html by hand I could combine the font,size and bold inside one span, but that is not the way these editors work. Also, doing it like in the examples is valid html.

Wouter.

Have you tried with the latest Velocity version? I’m now using the latest CSS/renderer.

Alternatively, some html editors have ways to alter the kind of html that is produced, you might want to have a look at their advanced documentation. If there is a way to do it in the javascript configuration of the editor, I can guide you to do it in the ServoyHtmlEditor.

Hi Patrick,

I am using velocity 2.0.4. We found a solution using a different way to generate the PDF. This uses our own PDF plugin that uses the same libs (iText, FC). This leads to think that Velocity is not configured in the correct way to use these libs. The good news is, it can be fixed. Now only to find out how.

W.

What iText library version are you using? And what xhtmlRenderer lib are you using?
Is your plugin Open Source? What kind of custom configuration do you do?

Hi Patrick,

We use the exact same lib versions that Velocity uses (otherwise we have lib versioning problems).
The code that we use to render the PDF:

        try {
        	
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            builder.setEntityResolver(FSEntityResolver.instance());
            Document doc = builder.parse(new StringBufferInputStream(html));
            ITextRenderer renderer = new ITextRenderer();           
            renderer.setDocument(doc, null);

            renderer.layout();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            renderer.createPDF(out);
            out.close();
            return out.toByteArray();
        } catch (Exception ex) {
            //exception handling
            ...
       }
        return null;

I only see a small difference between the Velocity plugin and our plugin in the way the Document object is instantiated.

Regards,
Norbert

Thanks, I’ll have a look next week. I can’t just now because I’m out of my office for the week.
One thing I also do is use a subclass of ITextRenderer so I’ll check this as well.

I’ve changed the instanciation of the Document to use the DocumentBuilderFactory and FSEntityResolver, and it seems to work better now.

Only thing I’ve changed is the use of StringBufferInputStream which is deprecated since Java 1.1, so in my case it reads:

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
builder.setEntityResolver(FSEntityResolver.instance());
return builder.parse(new ByteArrayInputStream(content.getBytes("UTF8")));

The change will be part of v2.0.12, thanks for checking it on your end.