Tech Tip: Using Ghostscript to Convert and Combine Files
Ghostscript gives you the power to combine files, convert files, and much more, all from the command line.
It is easy to combine several input files into one combined PDF using Ghostscript:
gs -sDEVICE=pdfwrite \
-dNOPAUSE -dBATCH -dSAFER \
-sOutputFile=combined.pdf \
first.pdf \
second.pdf \
third.pdf [...]
Your input files don't even need to be PDF files. You can also use PostScript or EPS files, or any mixture of the three:
gs -sDEVICE=pdfwrite \
-dNOPAUSE -dBATCH -dSAFER \
-sOutputFile=combined.pdf \
first.pdf \
second.ps \
third.eps [...]
The combined.pdf file will contain the input files in the order given on the commandline. If you don't want the combined file to be PDF, but PostScript instead, you may want to use this:
gs -sDEVICE=pswrite \
-dNOPAUSE -dBATCH -dSAFER \
-sOutputFile=combined.ps \
first.pdf \
second.ps \
third.eps [...]
Should you for whatever reason want PostScript level 1 output, add a language level parameter:
gs -sDEVICE=pswrite \
-dLanguageLevel=1 \
-dNOPAUSE -dBATCH -dSAFER \
-sOutputFile=combined.ps \
first.pdf \
second.ps \
third.eps [...]
The default PostScript language output level is 2. Using "1.5" is also supported, which is language level 1 with color extensions.
You can convert color input files into black/white or non-color/gray PostScript like this:
gs -sDEVICE=psgray \
-dNOPAUSE -dBATCH -dSAFER \
-sOutputFile=combined.ps \
first.pdf \
second.ps \
third.eps [...]
gs -sDEVICE=psmono \
-dNOPAUSE -dBATCH -dSAFER \
-sOutputFile=combined.ps \
first.pdf \
second.ps \
third.eps [...]
Should you for some reason need a series of single-page EPS files made up of pages from various input files, try this:
gs -sDEVICE=epswrite \
-dNOPAUSE -dBATCH -dSAFER \
-sOutputFile=p%08d.eps \
5page-first.pdf \
7page-second.ps \
1page-third.eps [...]
The resulting files will be nicely named as p00000001.eps .... p00000013.eps ...
But be aware, converting PDFs back to PostScript (or EPS), like the last 6 commands did, may loose some or much of the original quality. For example, PostScript can't handle transparencies directly (it fakes them by converting them into bitmap patterns), and converting such a PostScript file back to PDF will not restore the original transparency feature. Also, some other aspects of the graphic quality from the input PDFs may be deteriorated.
So in general, it's better to stay with PDFs and avoid roundtrip conversions to PostScript and back to PDF...
Should you need TIFFs or JPEGs from all pages of your input files, try this:
gs -sDEVICE=tiffg4 \
-dNOPAUSE -dBATCH -dSAFER \
-sOutputFile=p%08d.tif \
-r600x600 \
5page-first.pdf \
7page-second.ps \
1page-third.eps [...]
gs -sDEVICE=jpeg \
-dNOPAUSE -dBATCH -dSAFER \
-r600x600 \
-sOutputFile=p%08d.jpg \
5page-first.pdf \
7page-second.ps \
1page-third.eps [...]
Graphic gurus, check this out. To create color separations (CMYK), use:
gs -sDEVICE=tiffsep \
-dNOPAUSE -dBATCH -dSAFER \
-r600x600 \
-sOutputFile=p%08d.tif \
5page-first.pdf \
7page-second.ps \
1page-third.eps [...]
We included an extra parameter in the last few examples to make the output resolution 600dpi, because we don't like the default 72dpi when it comes to pure full page image files. Now, you may be surprised: for each single page of the input files you automatically get 5 different files:
p000000XX.tif p000000XX.Cyan.tif p000000XX.Magenta.tif p000000XX.Yellow.tif p000000XX.Black.tif
The *.tif file will be the biggest, since it contains a single 32 bit composite CMYK file (tiff32nc format). The four *.Colorname.tif files are not really colored (as one might think from their names), but in reality they are tiffgray files meant for creating offset printing plates for the respective separation in 4-color CMYK printing. If Ghostscript autodetected so called "spot colors" in the input files, these will get their own separation files, with a naming convention of *.s1.tif, *.s2.tif,... etc. (up to 64 different process and spot colors are supported).
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Linux Systems Administrator
- Validate an E-Mail Address with PHP, the Right Way
- Introduction to MapReduce with Hadoop on Linux
- RSS Feeds
- Weechat, Irssi's Little Brother
- New Products
- Developer Poll
- Reply to comment | Linux Journal
18 min 26 sec ago - Reply to comment | Linux Journal
1 hour 3 min ago - Didn't read
1 hour 14 min ago - Reply to comment | Linux Journal
1 hour 19 min ago - Poul-Henning Kamp: welcome to
3 hours 29 min ago - This has already been done
3 hours 30 min ago - Reply to comment | Linux Journal
4 hours 15 min ago - Welcome to 1998
5 hours 3 min ago - notifier shortcomings
5 hours 27 min ago - heroku?
7 hours 4 min ago
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
Did not work to combine 2 PDF files
I am having problems getting the first command to successfully combine 2 pdf files. My resulting combined.pdf file only contains the last pdf file specified in the list. Is there a command syntax error in the example?
Works for me
Works for me.
separation
i have pdf file. i want to print it separated (just only cyan, magenta, yellow or black) with pdf generic printer likes CUPS-PDF. would you like to help me to tell me the script. My pdf files in /arif/pdffiles.pdf
Thanks, how about reversed (combining separations)
Thanks for the tip on the TIFFSEP option! I will be giving it a try soon.
But actually I was looking for a way to de the reverse:
I have TIFF-files that are already separated into C, M, Y and K 1 bit tiff-files, but for proofing purposes I want them combined back into a PDF (CMYK or RGB).
Does anyone know a trick to do that?
Thank in advance,
NachtVorst
combined back
I,
just found your message and i'm trying to do the same. Did you find a way to combine back separated the 4 1 bit tiff files?
Thanks if you can help me. Thanks too if not
Yves
Thank you for this tutorial,
Thank you for this tutorial, it saved me *quite* some time. Good job!
create grayscale graphics
Kurt Pfeifle shows how to make black / white or grayscale graphics using postscript.
However if you are using psmono or psgray as devices, you'll end up having a raster image.
To retain your vectors you can follow the hints in:
http://handyfloss.net/2008.09/making-a-pdf-grayscale-with-ghostscript/
so for converting an eps file into gray you might want to use something like this:
gs -sOutputFile=gfile_gray.pdf -sDEVICE=pdfwrite -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -sEPSCrop gfile.eps
pdftops gfile_gray.pdf gfile_gray.ps
ps2eps -f gfile_gray.pdf
enjoy
gs won't accept jpg as input
Used gwenview to view the JPG then print it to the PDF pseudo-printer. The resulting PDF file separated without problems. GS will display the resulting TIFs. Used 'gm convert' to change original JPG to TIF, but GS won't display it, even though 'file' says it's OK, and gwenview will display it.
Not an Image Viewer
GS is not an image viewer. You're not going to be able to view jpegs or tiffs with it. Errr, what in this tech-tip made you think you could do that?
Mitch Frazier is an Associate Editor for Linux Journal.
Au Contraire
Actually, GS is a very good image viewer, for postscript files. Try it.
The article indicated GS could handle multiple different types of input files. I mistakenly thought it could handle jpegs. Mea culpa.
Image Files
I don't think of postscript files as image files, but yes it is an "image" viewer for postscript and related things.
It can create output files that are jpegs.
Mitch Frazier is an Associate Editor for Linux Journal.
Didn't work, no idea why
Didn't work for me, even when I used GraphicsMagick to scale color jpg down to 400x400. Ran 'gs -h', it shows I have the tiffsep device. Got this error:
Error: /undefined in ���
Operand stack:
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1905 1 3 %oparray_pop 1904 1 3 %oparray_pop 1888 1 3 %oparray_pop 1771 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval--
Dictionary stack:
--dict:1152/1684(ro)(G)-- --dict:0/20(G)-- --dict:92/200(L)--
Current allocation mode is local
Current file position is 5
GPL Ghostscript 8.61: Unrecoverable error, exit code 1
Anyone have any idea what happened? Vanilla Kubuntu 8.04 with latest updates.
error postscript
You have an postscript error in your input file. This one should only be a ps or a pdf file.