wkhtmltopdf vs maximum pages

wkhtmltopdf use 2 file descriptor per page (one each for header and footer) which are required for generating the per-page custom variables.
By default Linux allows users to open up to 1024 files. So in case if your document contains more than 512 pages you have to change this limit up to a higher value.
In Ubuntu you can change the parameter (no files) in /etc/security/limits.conf. Just add these lines to the file.