I was having the same error and I solved it by making sure all the data required for the rendering is collected before starting the rendering.
The form was making many server calls to collect info and was taking longer than 30 seconds.
I changed it so the background task spends time collecting data and pass it to the form, where the rendering is fast.
The pdf generation sometimes still takes longer than 30 seconds, but the form has been rendered, so it doesn’t time out.
If your pdf is so long that the simple rendering causes it to time out, then I guess you are out of luck.