1

I'm on Windows 10. I have 2,000 PDF files, each has two or three pages (with exactly one page blank) and is only 40~50 KiB in size, summing up to less than 100 MiB. I want to concatenate all pages in all files into a single PDF file. The current approach I'm using is Acrobat DC → Tools → Combine files. I drag in all the files into the tool and hit start. After some estimation I find that it needs more than 12 hours to do that (Core i7-4710HQ laptop, 16 GiB RAM and SSD). That's fairly impractical for me. Is there a faster way?

1

3 Answers 3

2

If you care to use python there are several python scripts discussed in this previous thread: https://stackoverflow.com/questions/3444645/merge-pdf-files

Due to the way the python PDF library works all files are opened first and only when writing the output file the contents are read. You should expect high memory consumption because of this. Workaround would be to split the files into several folders.

You could easily expand this script to, for example, combine all PDFs in a subtree and all of it's subfolders.

This program supports optional flags for verbose output, and for skipping the last page of each input file. Wildcards are allowed for the input file pattern.

from argparse import ArgumentParser
from glob import glob
from PyPDF2 import PdfFileReader, PdfFileWriter



def PDF_cat(files, output_filename, skiplastpage, verbose):
    # First open all the files, then produce the output file, and
    # finally close the input files. This is necessary because
    # the data isn't read from the input files until the write
    # operation. Thanks to
    # https://stackoverflow.com/questions/6773631/problem-with-closing-_
    #    python-pypdf-writing-getting-a-valueerror-i-o-operation/6773733

    writer = PdfFileWriter()
    skip = 1 if skiplastpage else 0

    # collect and open input files
    inp = [open(f,'rb') for f in glob(files) if f != output_filename]
    n = len(inp)
    print 'merging %d files' % n
    for i, fh in enumerate(inp, 1):
        reader = PdfFileReader(fh)
        for pg in range(reader.getNumPages() - skip):
            writer.addPage(reader.getPage(pg))
        if verbose: print '%d/%d %s' % (i, n, fh.name)

    print('writing output file...')
    with open(output_filename, 'wb') as fout:
        writer.write(fout)
    # finallly...
    for fh in inp:
        fh.close()

if __name__ == '__main__':
    parser = ArgumentParser()

    # add more options if you like
    parser.add_argument('-o', '--output',
                        dest='output_filename',
                        help='write merged PDF files to FILE',
                        metavar='FILE')
    parser.add_argument(dest='files',
                        help='PDF files to merge')
    parser.add_argument('-s', '--skiplastpage',
                        dest='skiplastpage',
                        action='store_true',
                        help='skip last page of each merged PDF')
    parser.add_argument('-v', '--verbose',
                        dest='verbose',
                        action='store_true',
                        help='show progress')
    parser.set_defaults(output_filename='mergedPDFs.pdf', files='.\*.pdf',
                        skiplastpage=False, verbose=False)

    args = parser.parse_args()
    PDF_cat(args.files, args.output_filename, args.skiplastpage, args.verbose)

A quick test: merging 501 identical PDFs of 91 KB each took 61 s on my notebook, using PDFtk.exe took 83 s. Output files were not of the same size but displayed identical.

2
  • I'm eager to use Windows Subsystem for Linux (that's my daily working platform). I'll try it out tomorrow.
    – iBug
    Jan 21, 2018 at 14:53
  • Good one. It finished 2,000 PDF files in less than half a minute. I'll wait for some user-friendly solutions before accepting this answer.
    – iBug
    Jan 21, 2018 at 15:20
0

There are other alternatives to Acrobat that you can try. These tools may help you somehow.

1. PDFSam

  • Merge and Split a PDF file at given page numbers, at given bookmarks level or in files of a given size
  • Extract pages from PDF
  • Rotate PDF files, every page or just the selected pages
  • Merge PDF files together taking pages alternatively from one and the other.

2. PDFMerge

  • Secure File merging and Handling
  • Provides Online Platform for merging PDFs
  • Also DEsktop Version available

3. PDFtk

  • Simple yet very powerful toolkit
  • Comes with a Command-line tool that makes it easy to interact with multiple pdf's easily on the command line.

For now, I would suggest you to use pdftk, as It's command-line tool is very powerful and saves a ton of time and effort.

Feel free to edit the list with any other tool.

1
  • I would have used the Python code compared to a command-line tool.
    – iBug
    Jan 21, 2018 at 15:23
0

I use Ghostscript for this. I've combined 4000 pdf files using it. I found it less likely to break PDF content than PyPDF. Combining 4000 pdfs takes a couple of minutes.

You must log in to answer this question.

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