This is generally a good advice, but for pdf generation may not be enough.
The pdf is generated by spinning a headless Chrome and rendering a form in it. If I remember correctly, after the pdf has been generated or after 30 seconds the headless Chrome is killed because it’s a memory hog. A background task can last longer than 30 seconds, but the headless Chrome can’t, not even in a background task. Here is a similar discussion.
I use a background task to collect, calculate and serialize all I need, then pass it to the pdf renderer. The first part can take a minute or two, then the rendering of a pdf with up to 20 pages works just fine.
I used to get timeout errors once in a while, but I haven’t seen them in months. I think because of some improvements on the Anvil side that sped up the rendering.