CiviCRM uses a pdf generator that is basically pretty rubbish & runs out of memory
CiviCRM supports use of a better one -
" wkhtmltopdf is an alternative utility for generating PDF's which may provide better performance especially if you are generating a large number of PDF letters or receipts. Your system administrator will need to download and install this utility, and enter the executable path here."
but it needs to be installed on the server - from https://wkhtmltopdf.org/ (& we provide a path to the excutable)