2

I'm looking for a way (preferably using the command line on Linux) to merge separate PDFs of book chapters into a single PDF of the book in the correct order (maybe based on the page numbers within the PDFs, or on some heuristics).

So I want to be able to go to the dir containing all the individual chapters and just do

joinpdf-magic-command *.pdf

and have the output PDF be in the correct order.

I've found plenty of tools to merge PDFs (pdfjoin, pdftk) but they all put the PDFs in the order that they appear on the command line.

edit:

An example list of chapters might be:

1-Introduction.pdf
2-The-analog-digital-interface.pdf
3-Adaptive-digital-systems.pdf
4-Non-linear-applications.pdf
5-Spectral-analysis-and-modulation.pdf
6-Introduction-to-Kalman-filters.pdf
7-Data-compression.pdf
8-Error-correcting-codes.pdf
9-Digital-signal-processors.pdf
Appendix-1-Solutions-to-problems.pdf
Appendix-2-A-MATLAB-Simulink-primer.pdf
Glossary.pdf
Index.pdf
Preface.pdf
References.pdf

But for other books there will be other sections, their names might be different, and even sections with the same name might even be intended to go in different orders. That's why I mentioned that using the page numbers might be the key.

  • please specify the OS if you want more specific help. Have you tried using a step to rename the files into the correct order? – Julian Knight May 22 '15 at 10:52
  • Questions seeking product, service, or learning material recommendations are off-topic because they become outdated quickly and attract opinion-based answers. Instead, describe your situation and the specific problem you're trying to solve. Share your research. – LPChip May 22 '15 at 11:10
  • @LPChip Ok I removed the part about looking for a tool. I just want to know if there is a way to do it. – dshepherd May 22 '15 at 11:19
  • 1
    ImageMagick convert can do things like this, though I don't know exactly how it handles you particular case. Try it: install ImageMagick, then convert chap1.pdf chap2.pdf chap3.pdf book.pdf – a CVn May 22 '15 at 12:33
  • 2
    @MichaelKjörling: OMG, you may want to delete your comment again, despite of the upvote it got. To create multipage PDFs with convert is a crime! It's a crime against sobriety and common sense, because it will mince-meat all nice vector elements from PDF contents into full page raster images. Especially the form of the command you gave -- it will use the default resolution of 72 DPI, loosing lots and lots and lots of the original quality, and throwing away all font information, searchability and accessibility. I can't even... – Kurt Pfeifle May 24 '15 at 0:25
3

There is no magic you can use to sort the chapters of your book automatically -- not, if the file naming convention doesn't support it. This is an act that has to be accomplished by a human.

The only way to do it fast is by putting the ordered filenames into a text file, similar to what you quoted in your OP.

Then simply run:

pdftk $(cat mychapters.txt) cat output book.pdf
2

Personally, I would not look for a new tool but rather to wrap your workflow in a script to enforce the correct order.

I assume you have some way of identifying the correct order otherwise this will not be possible at all. Ideally, you would have the files named such that they automatically fall into the correct order:

01ChapterTitleOne.pdf
02ChapterTitleTwo.pdf
etc.

and so on. You might have the chapter number at the end though (as in Michael's example) which breaks the sort. In that case, you should add a step to your workflow script that renames the files so they fall into the correct order.

As long as the files are consistently named, this should not be an issue. If you can share more information about the naming, I'm sure we can come up with a BASH script to do this.

Once you have everything correctly named, you should be able to use the same command you started with as the second step of the workflow script. If that didn't work, then you would need a slightly more complex BASH script that loops over the files in the correct order.

All of this is standard stuff for BASH scripting but it all revolves around having consistent naming of the files.

  • I've edited the question to add an example. The problem with an approach like this is that you often can't rely on the file names to give you the order (although it might be possible to make a good guess with a complicated enough script). – dshepherd May 22 '15 at 15:20
  • 1
    But PDF's don't really contain structured data so detecting the page numbers would be very challenging, possibly not possible. Easier to work back upstream and get writers to adopt a naming convention. – Julian Knight May 22 '15 at 15:31
  • Yeah looks like it :( Ah well thanks anyway. – dshepherd May 22 '15 at 15:37

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.