11

I have a lot of PDF files with 1 to 4 pages each. I need a solution which automatically generates a new file for each of these files. The new files should contain the content of the original files twice (i.e. pages 1 through to the end, followed by the same pages in the same order again).

How do I accomplish this?

1
  • 3
    Please clarify, we cannot read your mind. What bill? You want to generate a new pdf file that contains every page of the original twice? In what order? Do you want page 1a followed by page 1b or do you want all 4 pages first and then all four pages again? What Operating system are you using? What tool do you use to create the pdfs?
    – terdon
    May 28, 2013 at 15:52

8 Answers 8

17

Solution for Windows using PDFtk (which you seem to be using as per your tags):

This will result in a PDF with pages 1-end followed by 1-end again:

pdftk in.pdf cat 1-end 1-end output out.pdf

If you want each page to be duplicated together (as in 1,1,2,2,...), use the following batch file:

@echo off
set pages=
setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('pdftk in.pdf dump_data ^| find /i "NumberOfPages"') do for /l %%b in (1,1,%%a) do set pages=!pages! %%b %%b
pdftk in.pdf cat!pages! output out.pdf
3
  • Thanks a lot: "pdftk in.pdf cat 1-end 1-end output out.pdf" exactly does what I need. The only thing missing is a loop that takes all pdf files from a directory and creates all the new files.Thanx a lot for helping me again. I'll be grateful and invite my wife for a dinner with the time saved ;-)))
    – Marc
    May 29, 2013 at 19:58
  • @Marc: The loop is easily achieved. Just go to the directory with the PDFs and at the command prompt type for %a in (*.pdf) do @pdftk "%~a" cat 1-end 1-end output "%~na (Duplicated)%~xa". For each in.pdf (name can be anything), this will result in a corresponding in (Duplicated).pdf. Enjoy your dinner and don't forget to accept the answer (click the green check mark to the left) if it helped you!
    – Karan
    May 30, 2013 at 14:06
  • infinete thanks to you - you have saved my life! I am very very grateful for your help!
    – Marc
    May 30, 2013 at 19:19
2

I might give you a better solution if you answer the questions in my comment but for the sake of your wife, here are couple of suggestions.

ImageMagick is a cross-platform command line tool for image manipulation. Once you install it, you should be able to use its convert tool to do what you want. The details depend on your operating system. I am assuming you want two copies of the entire file, not each page doubled.

  1. Linux/OSX/Unix etc

    for n in *pdf; do convert -density 150 "$n" "$n" "$n"; done
    

    This will overwrite the existing files, you may want to backup first.

  2. Windows. This may well be slightly wrong, I don't use Windows so I cannot test it but the general idea should be something like this

    for %f in (*.pdf) do (convert.exe %f %f %f)
    
5
  • Your for loop is missing the done and $n should probably be quoted. Maybe I'm missing how convert works, but what does convert do when you pass three times the same file? I can't test it right now.
    – slhck
    May 28, 2013 at 20:51
  • Thanks @slhck, you're right on both counts as usual. The general convert format is convert infile1 infile2 ... infileN outfile. So, if you pass it the same file three times it will take it twice as input and then write to the same file as output. That one I did test :).
    – terdon
    May 28, 2013 at 21:29
  • Oh, cool, then I learned something as well :)
    – slhck
    May 28, 2013 at 21:30
  • Sounds good to me. Thanks a lot. The comment above: I gave that one the first try and it worked perfectly, only the right loop I am still seraching for.... could you help me again? Many thanks in advance!
    – Marc
    May 30, 2013 at 5:50
  • @Marc sorry, I'm not sure what you mean. Which comment? Which loop? The windows or the bash one?
    – terdon
    May 30, 2013 at 14:16
2

If you want each page to be duplicated together (as in 1,1,2,2,...) on Linux, this script will do it:

#!/bin/bash
INPUTFILE=$*
PAGENUM=`pdftk ${INPUTFILE} dump_data | grep NumberOfPages | cut -d : -f 2  | cut -d " " -f 2`
PAGES=`seq 1 ${PAGENUM}`
DUPAGES=`for i in ${PAGES} ; do echo $i $i | tr "\n" " " ; done`
OUTPUT=`basename ${INPUTFILE} .pdf`.dup.pdf
pdftk ${INPUTFILE} cat ${DUPAGES} output ${OUTPUT}
1

For duplicating pages in a 4 page pdf (1,1,2,2,3,3,4,4) in Linux with pdftk already installed this entry worked for me in a C shell script:

pdftk output.pdf cat 1 1 2 2 3 3 4 4 output out.pdf
0

If you have pdftk not installed, but poppler-utils, then use this command:

for f in *
do
    pdfjoin $f $f
done

This is especially relevant on Fedora 21, where pdftk is not available in the repository any more.

0

Simple use pdfunite. This can probably be further simplified with more scripting knowledge.

Be aware that the last argument is the resulting file. If you forget that then this file will be overwritten.

pdfunite 1.pdf 2.pdf 3.pdf 4.pdf result.pdf
pdfunite result.pdf result.pdf result.pdf 3-times-result.pdf
0

For duplicating all pages (1,1,2,2,...) of a pdf file (named in.pdf), you can shuffle it with itself (using pdftk):

pdftk A=in.pdf B=in.pdf shuffle A B output out.pdf
0

To duplicate a pdf multiple times into a new pdf, I prefer pdfjam. In Windows this might be hard to come by, but in Linux (or via wsl in Windows) the texlive-extra-utils package should be easy to install.

I use it like this:

pdfjam input.pdf 1,1 --a4paper --scale 0.97 --pdftitle Title... --outfile output.pdf

The advantage of pdfjam over pdftk: the resulting pdf is much smaller. A duplicated result without duplicating the actual data!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.