Refactored PDF/Image API to reduce code duplication