Issue 59: Two errors with PDF generation - and their fixes
Status:  Fixed
Owner: ----
Closed:  Nov 2007
Reported by dirk.mic...@googlemail.com, Nov 22, 2007
I found two new errors when exporting a jmesa table to pdf:

1. When any of the exported data contains the ampersant ('&') character,
wich is common for german company names, the PDF generation is aborted with
an Exception: " org.xml.sax.SAXParseException: Der Entitätenname muss
unmittelbar nach dem '&' im Entitätenverweis folgen'.

2. When any of the exported data contains german umlauts (äöüÄÖÜß), the PDF
generation is aborted with an Exception: ' org.xml.sax.SAXParseException:
Ungültiges Byte 2 in 2-Byte-UTF-8-Folge'.

These errors were found in the version jmesa-2.2.4-pdf.

I found a fix for both errors that you should adopt into your next release.
I changed the method getBytes() of the class org.jmesa.view.pdf.PdfView to
this:

--- snip ---
public byte[] getBytes() {
    String render = ((String) render()).replaceAll("\\&", "&");

    byte[] bytes = null;
    try {
        bytes = render.getBytes("UTF8");
    } catch (UnsupportedEncodingException e) {
        // TODO do something wise here!
        e.printStackTrace();
    }
    return bytes;
}
--- snip ---

Explanation:
1. the call of replaceAll replaces all '&' caracters with the html
enconding '&'.
2. the call of render.getBytes("UTF8") encodes the german umlauts to valid
utf-8 byte values.

Enjoy ;-)

Dirk Michaelsen
Nov 23, 2007
#1 extremec...@gmail.com
Thank you for the information! For sure I will add the render.getBytes("UTF8") fix.
The other one I am trying to think of something more generic. Really you would want
all characters to be encoded as the ampersand is just one possibility. Maybe we need
a generic wrapper CellEditor for PDF exports.... I will look into it!
Nov 23, 2007
#2 extremec...@gmail.com
(No comment was entered for this change.)
Status: Accepted
Nov 27, 2007
#3 extremec...@gmail.com
I implemented a fix for this. What I did was a custom body method and decorated the
CellEditor with an editor that will escape xml syntax. I have not had the chance to
really test it out yet, but I would be happy to do a build if you would like to test it!

I was able to reproduce the error with the & symbol so I know it worked to fix that.

http://jmesa.googlecode.com/svn/trunk/jmesa/src/org/jmesa/view/pdf/PdfView.java
Status: Started
Nov 28, 2007
#4 dirk.mic...@googlemail.com
Good shot but no hit ;-)

I'm affraid to tell you that your code is still buggy. In case the column value is no
String (in my case it is an Integer) you get a ClassCastException. But if you change
the getValue method of the EscapeXmlCellEditor class like the following, everything
works like expected:

        public Object getValue(Object item, String property, int rowcount) {
            Object value = cellEditor.getValue(item, property, rowcount);
            if (value != null) {
                return StringEscapeUtils.escapeXml(value.toString());
            }

            return null;
        }

So from my point of view you can close this issue.

Thanks a lot
Dirk Michaelsen
Nov 28, 2007
#5 extremec...@gmail.com
Got it! I checked in the changes. Let me know if you find anything else. The PDF view
is getting better and better as we work with it more!
Status: Fixed