added cleanup at the beginning of convert to pdf function