14

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?

Karan
  • 57,289
Marc
  • 181

8 Answers8

22

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
Karan
  • 57,289
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)
    
terdon
  • 54,564
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}
Seegras
  • 21
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
1

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!

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

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
keiki
  • 403
  • 5
  • 12