Pypdf throws EOF marker error

Issue caused due to files being open after ceating the pdf file added
with context block around create pdf function to close the files
properly