Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'd like to concatenate pdf documents without loosing any information (image resampling etc.). I only want to do lossless compressions, consolidate fonts etc.

Before merging, I'd like to remove the first and last page of each document. How can I combine this with the merging process, so that I don't need to run each file twice through gs?

To merge, I use the command from this excellent answer. I added dPDFSETTINGS=\prepress. I am not sure if this is necessary or a good idea.

 gs \
  -o book.pdf \
  -sDEVICE=pdfwrite \
  -dPDFSETTINGS=/prepress \
  -dColorConversionStrategy=/LeaveColorUnchanged \
  -dEncodeColorImages=false \
  -dEncodeGrayImages=false \
  -dEncodeMonoImages=false \
   title.pdf \
   content.pdf

Update:

I tried the command above (without removing pages) on some real life files, and it is running very slowly.

gswin32c.exe -dBATCH -dNOPAUSE -o temp.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -dColorConve
rsionStrategy=/LeaveColorUnchanged -dEncodeColorImages=false -dEncodeGrayImages=false -dEncodeMonoImages=false [list of pdf files] pdfmark

Running the above command for 20 2MB files creates nearly 2 GB temp files and then runs extremely slowly with little CPU utilization and a lot of HDD activity. It takes about 20 min, and the output file is 800 MB large.

I get this error message: GPL Ghostscript 9.10: Missing glyph CID=0, glyph=0028 in the font HiddenHorzOCR . The output PDF may fail with some viewers.

On the other hand, pdftk runs in 30s, and the output file is 40 MB. Adding the bookmarks with gs takes another minute and slims the file to 30 MB.

gswin32c.exe -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=out.pdf pdftk.pdf pdfmark

Is there any downside to using pdftk? I would like to preserve the images in exactly the same quality and not loose any details and not make the files unnecessarily large. Otherwise I don't care too much, the pdfs are mostly scanned scientific articles and book chapters. The pages that I would like to remove are cover pages that were scanned with each article, one in the front and one at the end.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

In regards to your update: pdftk preserves image quality settings but not metadata, chapter markers, etc.

sejda is a new pdf editing toolkit that can do everything that pdftk can and more. Most importantly, it preserves all quality settings and most of the file metadata. Sejda requires Java to work.

Here's the documentation on merge actions:

$sejda merge -h

Given a collection of pdf documents, creates a single output pdf document composed by the selected pages of each input document taken in the given order.

Example usage: sejda-console merge -f /tmp/file1.pdf /tmp/file2.pdf -o /tmp/output.pdf -s all:12-14:32,12-14,4,34-:

Usage: sejda-console merge options
  [--addBlanks] : add a blank page after each merged document if the number of pages is odd (optional)
  --bookmarks -b value : bookmarks merge policy. {discard, retain, one_entry_each_doc }. Default is 'retain' (optional)
  [--compressed] : compress output file (optional)
  [--copyFields] : input pdf documents contain forms (high memory usage) (optional)
  [--directory -d value] : directory containing pdf files to merge. Files will be merged in alphabetical order. (optional)
  [--files -f value...] : pdf files to operate on: a list of existing pdf files (EX. -f /tmp/file1.pdf or -f /tmp/password_protected_file2.pdf:secret123) (optional)
  [--filesListConfig -l value] : xml or csv file containing pdf files list to concat. If csv file in comma separated value format; if xml file <filelist><file value="filepath" /></filelist> (optional)
  [--help -h] : prints usage information. Can be used to detail options for a command '-h command' (optional)
  [--matchingRegEx -e value] : regular expression the file names have to match when the directory input is used (Ex -e "test(.*).pdf"). (optional)
  --output -o value : output file (required)
  [--overwrite] : overwrite existing output file (optional)
  --pageSelection -s value : page selection script. You can set a subset of pages to merge as a colon separated list of page selections. Order of the pages is relevant. Accepted values: 'all' or 'num1-num2' or 'num-' or 'num1,num2-num3..' 
    (EX. -f /tmp/file1.pdf /tmp/file2.pdf -s all:all:), (EX. -f /tmp/file1.pdf /tmp/file2.pdf /tmp/file3.pdf -s all:12-14:32,12-14,4,34-:) to merge file1.pdf, pages 12,13,14 of file2.pdf and pages 32,12,13,14,4,34,35.. of file3.pdf. 
    If -s is not set default behaviour is to merge document completely (optional)
  --pdfVersion -v value : pdf version of the output document/s {2, 3, 4, 5, 6 or 7}. Default is 6. (optional)

Say you have two PDF files, file1.pdf and file2.pdf, each 50 pages long, and would like to merge them while leaving out the first and last page in each case. This command should do the trick:

sejda-console merge -f file1.pdf file2.pdf -o merge.pdf -s 2-49:2-49:

Note: You might have to replace sejda-console with sejda-console.bat if you are on Windows.

As you can see you would still have to manually provide the page range for each document. If you want to automate this you could write a bash script or something similar to first calculate the PDF page count (e.g. by using libpoppler's pdfinfo) and then compose the sejda command-line accordingly.

share|improve this answer
    
Thanks, that is exactly what I was looking for. It is even faster than pdftk. I can get the page numbers with pdftk dump_info. Is there by chance a way to inject bookmarks for each file (and move existing bookmarks one level down)? –  Dan Jul 1 at 2:30
    
@Dan Glad to hear that! No, I don't think there's a setting for that. You can replace the bookmarks with one entry for each document (-b one_entry_each_doc) but that's it. I think this would be a great feature, though. If you've got the time, I'd suggest creating a feature request on sejda's bug tracker‌​. The dev team seems to be pretty active and I think you'd have a good chance at getting this implemented. If you create a feature request please do post it here. I'd be happy to voice some support on the bug tracker. –  Glutanimate Jul 1 at 14:08
1  
I've posted a feature request github.com/torakiki/sejda/issues/157 Thanks again for the great recommendation, never heard of sejda before. –  Dan Jul 2 at 4:19

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.