Fixes #509 out of memory when generating huge pdf files.