Strapi v3 not returning pdfs

UPDATE

Looking at the logs in the terminal, the problem appears to be that strapi is sending its response back before the endpoint has finished processing. So, How do I stop it sending the response until after the process has finished?