Skip to content

Instantly share code, notes, and snippets.

@jdraths
Last active September 22, 2017 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdraths/3f8c242a740b9a6e471934ff06d05b3f to your computer and use it in GitHub Desktop.
Save jdraths/3f8c242a740b9a6e471934ff06d05b3f to your computer and use it in GitHub Desktop.
# [putting-together-pdf-files](https://www.linux.com/news/putting-together-pdf-files)
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=finished.pdffile1.pdf file2.pdf
# \t
# gs -- starts the Ghostscript program\t
# -dBATCH -- once Ghostscript processes the PDF files, it should exit. If you don't include this option, Ghostscript will just keep running\t
# -dNOPAUSE -- forces Ghostscript to process each page without pausing for user interaction\t
# -q -- stops Ghostscript from displaying messages while it works\t
# -sDEVICE=pdfwrite -- tells Ghostscript to use its built-in PDF writer to process the files\t
# -sOutputFile=finished.pdf -- tells Ghostscript to save the combined PDF file with the name that you specified
# When using Ghostscript to combine PDF files, you can add any PDF-relatedoption to the command line.
# For example, you can compress the file, target it to an eBook reader, or encrypt it.
# See the Ghostscript documentation for more information.
## https://www.ghostscript.com/Documentation.html
gs \
-o book.pdf \
-sDEVICE=pdfwrite \
-dColorConversionStrategy=/LeaveColorUnchanged \
-dDownsampleMonoImages=false \
-dDownsampleGrayImages=false \
-dDownsampleColorImages=false \
-dAutoFilterColorImages=false \
-dAutoFilterGrayImages=false \
-dColorImageFilter=/FlateEncode \
-dGrayImageFilter=/FlateEncode \
title.pdf \
content.pdf
def combine_files(output: "book.pdf", directory: nil, files: [], options: ['-dColorConversionStrategy=/LeaveColorUnchanged', '-dDownsampleMonoImages=false', '-dDownsampleGrayImages=false', '-dDownsampleColorImages=false', '-dAutoFilterColorImages=false', '-dAutoFilterGrayImages=false', '-dColorImageFilter=/FlateEncode', '-dGrayImageFilter=/FlateEncode'])
# output = "/Users/me/Downloads/whatever.pdf"
if directory
files = Dir[directory + "/*.pdf"]
# and maybe
# files = files.sort_by{|f| f.split('_').last.split('.').first.to_i}
end
files = files.map{|f| Shellwords.escape(f)}
output = Shellwords.escape(output)
command = ['gs', '-o', output, '-sDEVICE=pdfwrite', options, files].flatten.join(' ') # add '-' as the last argument to print results inline
`#{command}`
end
def alt_combine_files(output: "book.pdf", directory: nil, files: [], options: ['-dColorConversionStrategy=/LeaveColorUnchanged', '-dDownsampleMonoImages=false', '-dDownsampleGrayImages=false', '-dDownsampleColorImages=false', '-dAutoFilterColorImages=false', '-dAutoFilterGrayImages=false', '-dColorImageFilter=/FlateEncode', '-dGrayImageFilter=/FlateEncode'])
if directory
files = Dir[directory + "/*.pdf"]
# and maybe
files = files.sort_by{|f| f.split('_').last.split('.').first.to_i}
end
files = files.map{|f| Shellwords.escape(f)}
output = Shellwords.escape(output)
command = ['gs', '-o', output, '-sDEVICE=pdfwrite', options, files].flatten.join(' ') # add '-' as the last argument to print results inline
`#{command}`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment