Home » Php » Join multiple PDF files into a single page PDF – positioned join using PHP

Join multiple PDF files into a single page PDF – positioned join using PHP

Posted by: admin July 12, 2020 Leave a comment

Questions:

This is not an attempt to join separate PDFs to one single PDF with multiple pages. It is an attempt to create a single page PDF from few other PDFs that already exists. I need to do this using PHP and the server is CentOS.

I am already generating a set of single page PDF files based on individual templates (Using FPDF). The requirement is to allow the user to mix and match existing PDF files and position them together to create a single PDF.

join multiple PDFs using dynamic positions to single page PDF

My current attempt is to convert the PDFs to images (using ghostscript) and use the output jpegs to create the final PDF using FPDF. While this is possible, the quality of the PDF is compromised a little during conversion.

I would like to know if there is a better solution that we can use?

UPDATE

The solution doesn’t have to be PHP. It most probably wont be. I’m certainly up for any commandline tool that can do the job. Example command would be as follows:

$ newtool -o outfile.pdf -s WidthXHeight -i in1.pdf in2.pdf in2.pdf -g p1Xp2 p3Xp4 p5Xp6

Above is an example only – not a real command – I just wish if a tool has that kind of functionality. Specify set of input files and their positions on the output file.

How to&Answers:

I’ve done this in the past with a combination of Ghostscript (to position an existing PDF page on a larger empty PDF page) and pdftk (to overlay/merge two equally-sized PDF pages into a new one).

Have a look at these answers (@Superuser.com) to get an idea how this works:

My procedure uses commandline and/or scripts. However, this could also be extended to do it programmatically from PHP with the Ghostscript .dll/.so