Binary Static

Interleaving two PDF files…

Posted in Applications, Hacking, Linux by Chris on October 17, 2010

Lightbulb…with no scripting required.  You’ll need the pdftk package though.  Say you’ve got a PDF file that has a two-sided layout and you want to trim the pages.  You could do this with pdfnup — however, your file may cut off from the even pages what it adds to the odd ones.  What now?  The solution is simple:

1. Use pdftk to split the file in even and odd pages seperately:

pdftk in.pdf cat 1-endeven output out.even.pdf
pdftk in.pdf cat 1-endodd output out.odd.pdf

2. Now would be the time to apply some pdfnup trimming magic to the two new files.

3. Use pdftk to burst these two files into single pages:

pdftk out.odd.pdf burst output %04d_A.pdf
pdftk out.even.pdf burst output %04d_B.pdf

4. Use pdftk to merge the pages back into one file, e.g.:

pdftk *.pdf cat output out.pdf

The important point here lies in step 3 when appending A or B to the odd and even page file names, because this allows glob sorting the pages like 0001_A.pdf 0001_B.pdf 0002_A.pdf 0002_B.pdf and so on.  I can’t believe I started to write a script before I realized how to solve this the simple way…

Tagged with: , , ,

17 Responses

Subscribe to comments with RSS.

  1. UIOD said, on January 9, 2011 at 7:32 pm


    Would you know how to merge 2 PDF :
    odd.pdf contains 1 3 5 …
    even.pdf contains 2 4 6…

    and into a single pdf: 123456.pdf ?

    thanks in advance

    • Chris said, on January 10, 2011 at 12:44 am

      Consider steps 3 and 4 above: use the program pdftk to extract a single file for every page in your input files. Be sure to name these files in the way described above (append “_A” to the numbered odd page files and “_B” to the numbered even page files). Then merge these page files again with pdftk into one new file — by simply using the glob mechanism of your shell, i.e. giving “*.pdf” as input parameter (in a directory containing only the single page files extracted from you two input files). This mechanism will replace “*.pdf” with the names of your page files in the correct order, giving you’ve named them correctly.

      • Lenneth said, on June 18, 2011 at 2:37 am

        Truly thanks for this little treat. Works like wonders.

        I had to archive like three thousands double sided pages of documents. I just loaded the feeding tray of the scanner a dozen of times (its a Ricoh Aficio, it scans to pdf over network) bursted the huge pdfs and stitched them back. Flawlessly.

        Then again, crammed them togheter in the right order.

        Then looking at the opened pdf, it has been just a matter of an hour to determine the groups of pages to be saved as individual documents.

        Saved me ages of work. Maybe Aeons.

        And now i also automatized it🙂

        At my workplace we have a windows box. I installed dropbox on it.

        Then i shared a folder with my linux box at home.

        Ol’ good cron checks it every ten minutes, eats the pdfs it sees, run a script that splices and mix back odd and even pages, and saves them to separate folders and then cleans up.

        I’m working to make it also able parse a text file so the final result can be spliced automagically into separate pdfs with the right name (Document id, document date and so on in the filename)

        I really owe you big Chris, ty.

  2. Anonymous said, on June 20, 2011 at 12:59 pm

    Step 3. and 4. look a bit complicated. Why not using the shuffle operation of pdftk?

    pdftk A=out.odd.pdf B=out.even.pdf shuffle A B output out.pdf

    This should merge files out.odd.pdf and out.even.pdf with interleaving odd and even pages, i.e., mix alternating pages from both files.

    • Chris said, on June 20, 2011 at 10:08 pm

      Absolutely. That seems to be an easier approach for UIOD’s type of problem. However, my original task was to modify odd and even pages in a different way before stitching them back together again — that’s why I would need the two-stage approach.

  3. […] di avere due documenti, dispari.pdf e pari.pdf, da voler alternare. Il trucco l’ho trovato sul blog Binary Static, e consiste nel fare il burst dei due PDF usando nomi opportuni per le pagine, in modo che poi […]

  4. Bobby said, on September 30, 2011 at 1:52 am

    Thanks, this helped me!

  5. […] them alternatively. There are several tools available: use pdftk package as described here, or use the mix alternate pages function of […]

  6. Mark Copley said, on March 15, 2012 at 12:58 am

    Thanks! Just what I needed

  7. […] [5] Interleaving two PDF files… by Chris (Acessado em: Abril/2012) […]

  8. packetnews said, on December 24, 2012 at 1:46 am

    To reverse order all pages, use PDFTools. To merge PDFs interleaving pages, use jPDF Tweak.

  9. Francesco said, on February 12, 2013 at 3:45 pm


  10. Jason Beringer said, on May 28, 2013 at 8:26 am

    Bravo also worked for me

  11. […] a Chris, ho però scoperto che non serve uno script, se solo si usa un piccolo […]

  12. jan said, on May 12, 2016 at 11:58 am

    excellent, works great, thank You!

  13. […] [5] Interleaving two PDF files… by Chris (Acessado em: Abril/2012) […]

  14. tkalfaoglu said, on October 29, 2016 at 2:06 pm

    It’s sad that PDFTK was abandoned by releases because it uses a copyrighted library..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: