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.

dakdad
  • 2,947
  • 2
  • 22
  • 21

1 Answers1

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

Community
  • 1
  • 1
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • 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 :-) – dakdad Dec 22 '11 at 09:49
  • sorry - ignore my comment for a bit. I will try pdftk joining and then get back to you. – dakdad Dec 22 '11 at 09:51
  • 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? – dakdad Dec 22 '11 at 11:06
  • 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...) – Kurt Pfeifle Dec 22 '11 at 11:21
  • 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. – dakdad Dec 22 '11 at 16:24
  • @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).... – Kurt Pfeifle Dec 23 '11 at 12:47
  • 1
    ...a command line snippet with your solution would be really appreciated! – caneta Jun 15 '17 at 19:24