Refactor PDF export code