It is because you are sending too much data to the "add_textflow" function. Increasing the memory in the php.ini file won't fix the problem. The solution is to try buffering the data you send to the function.

In the second example we send less data, but we call the function more often. With the first example, I could only generate 3 or 4 pages of a PDF; with the latter example I could generate 200+ page PDF's.

This took me a while to debug; hope it saves someone else some time. :)