It would be better to check error code after saving PDF. You are using static path (every time same for a record), it can't overwrite some time.
There is no error code when I run the script. It just gives an incorrect page count.
Any suggestion on what to use rather than static path?
depending on script, you are going to different layouts (form, list? how are they structured? we don't know) so obviously the output might and will change. The page count as well.