Published by Fabian on 13 Jan 2010 at 01:03 pm
Ugliest Code of the Year
Usually I rant about code from others. But this is something different. this is my code:
PdfPCell pdfPCell = table.getRow(0).getCells()[0]; List compositeElements = pdfPCell.getCompositeElements(); Paragraph p = (Paragraph) compositeElements.get(0); Chunk c = (Chunk) p.get(0); Jpeg jpeg = (Jpeg) (((Object[])c.getAttributes().get("IMAGE"))[0]);
Now comes my excuse for this crap. I need to parse this HTML and render it in a PDF (currently using iText):
<table cellspacing="0" cellpadding="0" border="0"> <tr> <td width="1%"><img height="100" border="0" width="150" src="/pat_to.jpg" /></td> <td>some fancy text</td> </tr> </table>
iText cannot automatically resize columns. So the column needs an explicit width. I don’t know the width of the table, because its not on the page, so I need the page width. I need to delve into the cell of the table which I know to contain the image. I need to know that its an Jpeg and that it had no alignment because if it had it would not be wrapped as chunk in a paragraph.
This is among the ugliest I ever wrote. But now my excuse. It is not my faul
The iText API (in 2.7.1) is not consistent at all, you have Arrays, Lists, Maps mixed and matched, keys as String, Constant (public or private constant) or no keys at all. Properties of images stored for each type in a different way. And you need to know a lot on yout parsed html structure, because iText cannot discover it. The way the HTML parser is implemented is way to basic and not extendable.
Currently thinking about switching to POI or rewriting the iTextParser….
Ingo on 13 Jan 2010 at 9:51 pm #
Did you take a look to the PDFLib (www.pdflib.com)? It´s commercial but IMHO it´s the best Library for PDF Creation.
Friedel on 15 Jan 2010 at 11:15 am #
if you are embedded developer you would probably may have written worse
Have you ever fixed addresses because the linker does something wrong?