Join multiple PDF files into a single page PDF - positioned join using PHP [closed]
Asked Answered
W

1

8

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.

Waxbill answered 21/12, 2011 at 12:10 Comment(0)
G
1

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

Gainly answered 21/12, 2011 at 12:52 Comment(7)
Thanks for the answer. I'm happy to use commandline tools to get the result. While the references are useful (I have seen the second one before) is there a particular example on how to place an existing PDF on an empty PDF that is larger? Your answer on the first point is super awesome by the way :-)Waxbill
sorry - ignore my comment for a bit. I will try pdftk joining and then get back to you.Waxbill
I have toyed around with ghostscript but I can only see a way to place 1 PDF file in to position on a newly created larger file. I cannot see how I can position multiple files in to multiple positions. Any pointers for this?Waxbill
You can only position 1 page (or file) on a newly created larger file at once. You cannot position multiple files into multiple positions. That's why you need to use pdftk for overlaying the different newly created on each other. (Maybe you can do 3 at once by using the "stamp" and "background" actions in one command...)Gainly
Holy crap!! it does work :D @pipitas Thank you so much for your direction. There are few things to workout, but the logic is working, getting it right with right positioning and dpi settings is another issue.Waxbill
@dakdad: if you have questions re. dpi settings, just ask. The pdfwrite device by default uses 720dpi. On the Ghostscript commandline you'd use -r600 or -r300x600 if you wanted to go for a different resolution. Note, that I used an explicit setting of -g5950x8420 to set the medium size to 5950pixels by 8420pixels (which maps to 595points by 842points at the implicitely used 720dpi)....Gainly
...a command line snippet with your solution would be really appreciated!Durban

© 2022 - 2024 — McMap. All rights reserved.