refactor to make pdf rendering more memory friendly