Easy way to concatenate PDF files in Ubuntu Linux

One liner:

gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf file1.pdf file2.pdf file3.pdf [...] lastfile.pdf

49 thoughts on “Easy way to concatenate PDF files in Ubuntu Linux

  1. Yes, it’s May 2015 and once again this did (almost) exactly what I wanted. Fast and easy.

  2. Pingback: Concatenating pdf files the easy way « twm’s blog

  3. So, cool ! Thanks ! I just removed the PAPERSIZE option and concatenated pdfs of varying widths beautifully.

  4. I was using
    $cat file1.pdf file2.pdf >> output.pdf
    in fedora, but in ubuntu it doesn’t work as expected.

    But, this page helped me a lot to save my time.


  5. I have used same command several times, google always bring me here.
    Command works great.
    Thanks for your time posting it!

  6. Why not:
    pdftk file_a.pdf file_b.pdf file_c.pdf cat file_d.pdf output file_abc.pdf
    or even, with the classic bash trick
    pdftk file_[a-d].pdf output file_abcd.pdf
    PS: unfortunately xpdf does not work well in release 12-X

  7. Being an amature with Linux I am a bit nervous of command line work however I tried this to concat 8.pdf pages and it worked very well. Thank you

  8. Thank you. This is a big help to me because one of pdftk’s requirements requires an older version of gcc be compile. This helps quite a bit. I ended putting this in my .bashrc in case I needed it later:

    pdfcat () {
      gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
      -sOutputFile=concatenated.pdf "$@"
  9. In comments someone suggest pdftk.
    It’s great but if we have protected pdf files it doesn’t work while gs does.

  10. David D Short -> wanker.

    It does produce selectable text depending on input files. Text input from latex generated files -> selectable text.

  11. Dear Davids,

    You can get A4 paper size by changing the appropriate variable (-sPAPERSIZE=a4). In my case I didn’t really need specific paper size, I only wanted a few plots glued together.

    As for not working, likely there was a problem with your installation. Regarding quality, it again depends on what you are looking for. My threshold was pretty low ;)

    But it seems you figured it out already so, great! Thanks for the pdftk example!

  12. David, you still haven’t said what the problem was, but that’s ok; seems you found the solution; great!
    So the problem was with your installation?!?
    Meaning that your comment “Doesn’t work. Plus, it should use A4 by default. This is 2011.” was just a … what? Rhetorical rant?

  13. What the hell is wrong with it indeed?

    Let’s answer that question (pretending that it wasn’t purely rhetorical, and ignoring your silly copycat insults).

    First solution: reinstall GhostScript. Result: it does now generate a PDF!

    However, the text is no longer selectable, implying that it has been converted to vector graphics. The lossiness of the JPEG graphics seems much higher (as can be seen from the reduced filesize).

    Second solution: use convert from ImageMagick. Results: horrible. The text is converted to bitmap.

    Third solution: install pdftk.

    pdftk file1.pdf file2.pdf file3.pdf […] lastfile.pdf cat output output.pdf

    Results: perfect.

  14. Since April 2009, a lot of people had put it to work, so what the hell is wrong with it? If you can’t put it to work, ask for help but don’t blame the post. So, if you’re not half-brained you’re certainly “an enormous, presumptuous wanker”.

  15. Pingback: Concatenating PDFs « Linux Sagas

  16. Just to complement, it allows to process well-formed file names. I had slides named MMM1 thru MMM16, and it accepted MMM*.pdf .

    Thanks a lot for the tip!

  17. Uma solução ainda mais simples:

    convert -page a4 page1.pdf page2.pdf …. all_pages.pdf

    (o “convert” faz parte do ImageMagik)

  18. Não ponhas esta cena no Whathaveidone que não é preciso! Que cena altamente man. Acabei de fazer o comando e urinei o dito boxerzinho 3 vezes.

    ALTAMENTE C******

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s