You have to use not "invisible" because that's need space in the pdf.
You have to use always "hidden".
The next step is that you use 1 dynamic page not 1, 2, 3..pages..
Please show at the screenshot in the hierarchy.
Only then (hidden and a correct hierarchy) will the content flowed correct and you have no more empty pages.
Unfortunality you have to change your scripts because the path is changed.
Hope it's helpful,
Mandy