10

I have got two pdf files with same number of pages and want compare each page with the corresponding page in the other file. For this I would like to merge say page 1 of File1.pdf with page 1 of File2.pdf so it gets one page in the new document. Then page 2 of File1.pdf with page 2 of File2.pdf and make it page 2 of the new file.

In this question I learned already that I can put two pages on one page with the --nup option of the pdfjam command:

pdfjam File1.pdf File2.pdf --nup 2x1 --landscape --outfile File1+2.pdf

The same can be achieved with the ImageMagick package:

montage *.pdf merged.pdf

But this puts together page 1 and page 2 of the first file and does the same later on with the second file - not as intended.

What I did is to split the two documents. The first file got even numbers in the file name, the second odd numbers (actually I created the files anew with appropriate file names). Then I merged all files again with

pdftk *.pdf cat output merged.pdf

and finally put two pages on one with

pdfjam --nup 2x1 --landscape --outfile merged2up.pdf merged.pdf

I could write a script with a loop doing this, but I was wondering whether there is an easy one-liner to achieve this? Maybe I didn't find the right pdfjam, pdftk or ImageMagick command?

1
  • 1
    Still two commands, but my pdftk has a "shuffle" option, which looks like it would allow you to merge the two documents with alternating pages without needing to first split them to individual pages. At that point, many PDF viewers have an option to show two pages at once side by side, so you might not even need the "N up" operation at that point.
    – arcticmac
    Commented Apr 25, 2016 at 18:46

4 Answers 4

10

You can split File1.pdf and File2.pdf into pages and then combine those tmp files into File1+2.pdf like so:

# Split files, note the naming scheme
pdfseparate File1.pdf temp-%04d-file1.pdf
pdfseparate File2.pdf temp-%04d-file2.pdf

# Combine the final pdf
pdfjam temp-*-*.pdf --nup 2x1 --landscape --outfile File1+2.pdf

# Clean up
rm -f temp-*-*.pdf
3
  • Thanks for this solution. However, the merged file is not really size optimized. In my case it's nearly 100 times bigger than the source files. Does someone know how to reduce the size of the merged file? Commented Jun 12, 2018 at 13:01
  • @StefanEndrullis: For me it's smaller than the sum of the individual sizes... I suspect it'd PDF-dependent.
    – user541686
    Commented Apr 7, 2019 at 2:50
  • great answer. thanks
    – seralouk
    Commented Jun 14, 2021 at 9:47
1

I would use this:

sudo apt install psutils 
sudo apt install ghostscript

pdf2ps -sOutputFile=input1file%d.ps input1file.pdf input1file.ps # cut to individual pages
pdf2ps -sOutputFile=input2file%d.ps input2file.pdf input2file.ps
psmerge -oinput.ps *.ps # put them together page by page from the alternative files
pstops -p a4 "2:[email protected](21cm,0)[email protected](21cm,14.85cm)" input.ps output.ps # put 2 pages on one
ps2pdf output.ps output.pdf # convert back to pdf

May be you will appreciate that. I like it because it is small and fast, but the man pages need improvement. :-(

2
  • I looked through a lot of answers and this is one that's fast and doesn't seem to mess up the contents, I like it.
    – ferada
    Commented Aug 22, 2020 at 20:31
  • I had to use pdf2ps file1.pdf output-%04d.file1.ps and psjoin *.ps > /tmp/out.ps. Solution with pdfseparate and pdfjam is better.
    – Jiri B
    Commented Jun 10, 2021 at 15:41
0

(If I don't misunderstand what the OP needs,) here is a simple(r) solution

pdftk A=File1.pdf B=File2.pdf shuffle A B output tmp-Figure1+2.pdf
pdfjam tmp-Figure1+2.pdf --nup '2x1' --landscape --outfile Figure1+2.pdf
rm tmp-Figure1+2.pdf

With pdftk ... shuffle A B ..., you create an intermediate PDF file that will look like

File1-page1
File2-page1
File1-page2
File2-page2
. . . .

Then, with pdfjam, you merge the odd and even pages of the intermediate file:

File1-page1 File2-page1
File1-page2 File2-page2
. . . .

I've just come up with this solution and tested it.

-1

A, err, little late, but may be helpful to some one else.

The original approach of the OP, to display pages side-by-side, can be achieved by the other answer given.

However, the approach may be arduous if the aim is to find some small textual difference. To that goal, use a pdfdiff, of which there are a few from different authors, commercial, open-sourced, command-line and GUI alike.

https://duckduckgo.com/?q=pdfdiff&t=ffab&ia=software

Note that this approach is not as effective for images as "identical" images can be encoded differently.

1
  • 2
    Can you explain how to use this software? Giving link only is only a half-way solution. And you don't even link to where the software is located at. Please consider to expand your answer with the essential info.
    – Vylix
    Commented Nov 17, 2017 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .