Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I want to merge several hundred pdf files in a directory automatically according to their file names.

E.g.

The files 1000.1.pdf 1000.2.pdf 1000.3.pdf 1000.x.pdf should be merged into 1000.pdf

and

2000.abc.pdf 2000.def.pdf 2000.ghi.pdf 2000.jkl.pdf 2000.5.pdf into 2000.pdf.

I don’t want to use solutions based on Preview/Automator (if available) because compared to third party software like Adobe Acrobat or PDFpen merging pdf files often (depending on the source documents) results in a significant increase in file size (see e.g. What causes PDF file size to increase when saving in Preview?)

Do you have any recommendations? Thank you!

share|improve this question
    
Combining PDFs will always increase file size, what exactly is your issue with that? –  patrix Aug 18 '13 at 13:21
    
@patrix I am speaking of hundreds of files to be merged. As linked above there can be a significant increase in file size with different tools included in Mac OS X. Why should I want a merged text file with an increase in size of sometimes several hundred percent? –  lejonet Aug 18 '13 at 14:47
1  
@patrix , lejone8 wants to have a automatic merge of PDF files but at 1+1=2 ratio, not with 1+1=5? or more in file size. Furthermore lejonet8 clearly points out that the use of Apple products is not acceptable due to they poor performances as compared to third party products! I do not know why you delete my comments, but so be it. –  Buscar웃 Aug 18 '13 at 15:39
1  
I do not understand the Automation request. It is very simple and fast to organize (sort) files by name, then select all in the wanted category, and do the merge in once click in any of the available programs like answered here, or others. The resulting file size will depend on type and content of the PDF files, so the 1+1=2 is not possible. The lejonet8 seems to be hang up on the argument why Apple programs create bigger pdf files then others. Good luck in answering that. –  Buscar웃 Aug 18 '13 at 15:59
2  
Can you please edit the question to describe in more details what "automated" means for you (it seems to mean different things for the people who took the time to propose solutions for your problem)? What should trigger the merging of the documents? Which patterns should be used to find matching files? Especially D.W.'s answer seems to be highly automated at first glance but maybe there is more behind your question than we know right now. –  patrix Aug 18 '13 at 17:28

2 Answers 2

up vote 5 down vote accepted

Try pdftk. It is command-line software that can join PDF files (and do lots of other stuff, too, but that isn't relevant here). You can download it from the official pdftk web page.

Sample syntax:

pdftk old1.pdf old2.pdf old3.pdf cat new.pdf

will create the file new.pdf that contains the concatenation of the files old1.pdf, old2.pdf, old3.pdf.

To solve your problem, with your example filenames:

pdftk 1000.*.pdf cat 1000.pdf
pdftk 2000.*.pdf cat 2000.pdf

and so on. You can use shell scripting to make this completely automatic if desired (but you'll have to spend a little time on your own learning how to write shell scripts).


Assuming all files are named 1000.x, 2000.x etc. a shell script could look somehow like this

#!/bin/bash

for n in {1..9}; do
    if [[ -r ${n}000.1.pdf ]]; then
        rm -f ${n}000.pdf
        pdftk ${n}000.*.pdf cat ${n}000.pdf && mv ${n}000.*.pdf ~/.Trash/
    fi
done
share|improve this answer
    
Thank you for your comment, but I don't see where is is a workflow for my needs (hundreds of files with similar names, merged according to these names). –  lejonet Aug 18 '13 at 1:19
1  
@lejonet8, that's exactly where command-line tools shine! I've edited my answer to give you an example of how to do that. Working out further details is probably beyond the scope of this question, and relates more to how to write shell scripts. –  D.W. Aug 18 '13 at 1:46
    
Thank you for your answer. Unfortunately running it on my files it resulted in this error message: Error: Unexpected text in page range end, here: 1000.pdf –  lejonet Aug 18 '13 at 10:19
1  
@lejonet8 Maybe you could elaborate on your automation requirements a bit more in the question. Are you looking for things like "Folder Actions" or such? What would trigger the concatenation process in the first place? –  patrix Aug 18 '13 at 15:47
2  
@lejonet8, You might have to experiment a bit to see how to make pdftk work for you. One error message, with no context, is unfortunately not enough for me to diagnose the problem. Try concatenating some pairs of files. Experiment. See if you can diagnose when pdftk does/doesn't work and the cause. Read the tutorial. Then, post a question about making pdftk work on a suitable Stack Exchange site or other question-and-answer site. FWIW, pdftk has been very reliable for me, working with a broad variety of pdf files generated by many different programs. Of course your experience may vary. –  D.W. Aug 18 '13 at 22:25

There’s a Python script hidden in Automator.app that joins .PDF-files

/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py --help
Usage: join [--output <file>] [--shuffle] [--verbose]

Example usage:

/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py --output all.pdf *.pdf
share|improve this answer
    
Thank you for your answer. I can’t comment on the technical background (there might be a difference in merging in Automator and Preview). Depending on the source files there can be a significant increase in file size as well. I just tested it again and four files with a size of 12 mb in total where joined to a 32 mb document. This is unacceptable. –  lejonet Aug 17 '13 at 21:05
    
Sorry, can’t help with that. –  akuhn Aug 19 '13 at 15:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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