2003.10_Gs, Ps2, Pdf2-Converting File Types.pdf

(381 KB) Pobierz
Layout 1
LINUX USER
gs & PSUtils
Ghostwriter
PostScript is a platform independent programming language that transmits
Simple text files are processed with
a2ps [5]. This program was originally
designed as a conversion tool (text to
PostScript). Later versions of this Post-
Script filter can handle compressed files,
LaTeX documents and even graphic for-
mats (see Figure 1). For a complete
overview of the program’s features, type:
text and images to printers. In addition to the well-known viewers for these
huhn@huhnix:~> a2ps --list= U
delegations
Applications configured for U
delegation
[...]
Delegation `ImageMagick', from U
png to ps
#{del.imagick} 'png:$f' ps:- U
| #{del.psselnup}
[...]
formats there are a number of useful and quick tools for editing and viewing
.ps files in the command line. This article looks into intelligent PostScript pro-
cessing with Ghostscript and the PSUtils.
BY HEIKE JURZIK
G hostscript [1] can handle both
PostScript and PDF files, and
provides numerous drivers
(“devices”) for converting to other for-
mats. Thus, the tool not only offers
printer drivers, but can also convert
PostScript to PDF or process files before
displaying them on screen.
The PSUtils [2] are a collection of pro-
grams for editing PostScript files,
including the following tools psnup
(places multiple pages on a single page),
psresize (changes the page size), psselect
(selects individual pages from a Post-
Script file), pstops (document
manipulation, such as scaling up or
down), psbook (organizes the pages like
in a book), and epsffit (scales an EPS file
to a specific size).
Before we delve into the depths of
command line PostScript processing,
let’s have a look at the basics: What does
the internal format of a PostScript file
look like?
quite common. This format is used to
transport information (mostly images)
between programs and can be embedded
and scaled in other documents. The orig-
inal PostScript code is placed in a
so-called “bounding box” that defines
the horizontal and vertical co-ordinates
and specifies the bottom left and top
right corners of the image.
The Delegation `ImageMagick’, from png
to ps entry indicates that a2ps can use an
external program called ImageMagick to
convert images in PNG format (“Portable
Network Graphics”) to PostScript.
Basics – Part III
There are numerous GUIs for Ghostscript
that allow you to read, convert or print
PostScript, such as Acrobat Reader,
KGhostView, GNOME-GhostView, and
gv . If you need to automate a process (in
a shell script, for example), you can
access the Ghostscript program, gs ,
natively.
Basics – Part II
There are numerous approaches to creat-
ing PostScript and PDF files. If you use
LaTeX [4], you can simply convert .dvi
files to PostScript in the command line:
dvips file.dvi -o .
Box 1: Manual PostScript
Ken Monk’s Website [3] includes a comprehensive tutorial that starts with simple examples (such
as lines and squares), takes you through complex font variants and ends up with numerous tips
and tricks. A simple “Hello World” document would appear as follows:
%!PS-Adobe-1.0 % Version number (PostScript versions 1-3)
/Times-Roman findfont % Find font
48 scalefont % Set font to 48 point
setfont % Make the selected font the current font
100 500 moveto % Set the starting point for text output
(Hello World!) show % Output 'Hello World!'
showpage % Output the current page
Ideally, PostScript files follow rules and conventions to facilitate processing by other programs. For
example, a page description is always split up into a prolog (definitions) and script (which uses
definitions to define the page). In the case of multi-page documents, the individual pages are typ-
ically described independently of one another.
Our simple example demonstrates a few important characteristics:
•Comments start with a percent sign and end with a new line.
• Structure information is a special kind of comment and starts with %! or %% . (Note that these
characters must occur at the beginning of a line, as they are interpreted as “normal” comments
otherwise.)
•PostScript documents always start with %! ,preferably with a version number
Basics – Part I
You could theoretically create a Post-
Script-File manually, just using an editor
(see Box 1), as the format is pure ASCII.
But this task is normally performed by
the print routine of a program. Devices
that process information in PostScript
files need to have a PostScript inter-
preter, in order to output the file on
screen or to a printer – this device could
be a PostScript printer that uses a
firmware based PostScript interpreter.
In addition to .ps files, .eps files
(“Encapsulated PostScript”), are also
84
October 2003
www.linux-magazine.com
Command Line: gs & PSUtils
593050815.002.png 593050815.003.png
gs & PSUtils
LINUX USER
You can additionally specify the page size
– either by stipulating the dimensions, as
in -wWidth and -hHeight (the program
can handle either point or centimetre
units), or set the -p flag and specify a
well-known format a3, a4, a5 etc.
As the name would suggest, the psre-
size tool changes the page format. If the
file is in a European format, such as
<A4>, you can use the following syn-
tax to convert it
psresize -PA4 -pletter file.ps U
new_file.ps
Figure 2: Different quality – ps2ascii vs. pstotext
psselect extracts specific pages – for
example, you can use the -e flag to
extract pages with even numbers, or -o
for odd numbers. This is useful if you
need to print both sides of the sheet: If
you have a laser printer, you could print
all the even pages first, then re-insert the
stack of paper and print all the odd
pages. The -p1,2,3,4 syntax allows you to
specify the pages you need as a comma-
separated list, which can include blocks
of several pages, for example
gs can be launched on the command
line with or without a PostScript or PDF
file as a parameter. Besides a display
window, you will also see output such as
the following in your terminal window:
tribution: the current “AFPL Ghostscript”
version is 8.10).
To use a specific driver (device), spec-
ify the device as a parameter when
calling gs , for example, -sDEVICE=epson
file.ps , if you want to output the file on
an Epson printer, or -sDEVICE=x11
file.ps to output the file on screen. Alter-
natively, launch gs and enter the
following at the prompt:
huhn@huhnix:~> gs abc.ps
ESP Ghostscript 7.05 U
(2003-02-05)
[...]
Loading NimbusRomNo9L-Regu font U
from /usr/share/ghostscript/ U
fonts/n021003l.pfb... 2052880 U
687555 1642520 342518 0 done.
[...]
>>showpage, press <return> to U
continue<<
psselect -p1-5,7-9 abc.ps new.ps
huhn@huhnix:~> gs
[...]
GS> (x11) selectdevice
GS> (abc.ps) run
But there is no need to decipher complex
instructions, just to quickly view a Post-
Script file – ps2ascii outputs the
document in cleartext format (see Figure
2). pstotext is another tool that fulfills
the same task, but achieves better
results. ps2ascii is included with the
Ghostscript package, and pstotext is
available from [6].
If you do not want to print the file
directly, you can use the -sOutputFile
option instead: gs is capable of creating
images from PostScript files. The driver
list includes a jpg option, for example.
The following syntax:
If you press [Return] at this point, or
launch gs without specifying a filename,
the GS> prompt is displayed. You can
quite the interpreter by typing quit , or by
pressing [Ctrl-C]. gs -h or gs -? displays
the help text, which includes an
overview of a few drivers and Ghost-
script switches, plus the version number
(GNU Ghostscript is currently at version
7.05.6.
There is another fork, released under
a more restrictive license, although it
basically only restricts commercial dis-
INFO
huhn@huhnix:~> gs -sDEVICE=jpeg U
-sOutputFile=abc.jpg abc.ps
[1] Ghostscript:
http://www.cs.wisc.edu/~ghost/
[2] PSUtils: ftp://ftp.dcs.ed.ac.uk/pub/ajcd/
psutils.tar.gz
[3] PostScript Tutorial by Ken Monk:
http://www.mis.coventry.ac.uk/~kenmk/
pstut/psindex.htm
[4] LaTeX: Heike Jurzik, LaTeX Workshop:
www.linux-magazine.com/issue/26/
LaTeX_Part1.pdf
www.linux-magazine.com/issue/28/
LaTeX_Workshop_Part2.pdf
[5] a2ps:
http://www.infres.enst.fr/~demaille/a2ps/
[6] pstotext: http://www.research.compaq.
com/SRC/virtualpaper/pstotext.html
converts a PostScript document called
abc.ps to a JPEG file called abc.jpg .
Miscellaneous Tools
The PSUtils package contains a number
of utilities for processing and re-format-
ting PostScript files. The psnup tool
allows you to place several document
pages on a single page, for example:
GLOSSARY
dvi file :When LaTeX runs (“latex file.tex”) it
creates numerous files in a directory for fur-
ther processing. These include a file with the
“.dvi” (device independent) suffix. This is a
device independent output file that can be
used for screen or printer output, provided
you supply an appropriate driver.
huhn@huhnix:~> psnup -2 file.ps U
2page_file.ps
[1] [2] [3] [4] [5] Wrote 5 U
pages, 464732 bytes
www.linux-magazine.com
October 2003
85
593050815.004.png 593050815.005.png 593050815.001.png
Zgłoś jeśli naruszono regulamin