Refactor pdf generation