I once did a PHP project generating PDF. I used FPdf.
I never had any memory problems. It's free, it's pure PHP code. You don't have to load any extensions.
I don't know if there's some helpers to auto-generate document from a query, but in the website, you have some scripts that shows how to use it with MySql.
HTH