Refactoring generate pdf method