HTML2PDF : undefined offset

While this blog is mainly about code-examples, I’d like to post an occasional bugfix when it’s a nasty one. This is one of them.

The HTML2PDF library for PHP is a great library for converting HTML to PDF. I ran into a problem in an application where an exception was thrown by this library with the vague message ‘Undefined offset 5‘.

At first I thought it was my code, but as I wasn’t using any arrays I started inspecting the HTML closer and finally noticed a table-element was causing the problem. Because of a miscalculation with the rowspan-attribute of one of the table-cells, not all the table-rows contained an equal amount of columns. This explains the undefined offset issue; the library has to convert to table to pdf and uses arrays in the background to convert the table data. A mismatch in columns will break this code. The library does not account for invalid html in this case and continues on the assumption the table-markup is correct. This explains the vague error message.

I Hope this solves your issue. If this is not the case, start leaving out parts of your HTML untill the error dissapears to discover what element might be causing it.