You might consider having a look at the FPDF_ImportPages api found in the fpdf_ppo.h public header. I think it might do most of the heavy lifting of what you are trying to do