Discussion:
center pages with ghostscript
(too old to reply)
Thomas Guignard
2004-10-26 09:17:52 UTC
Permalink
Hi there

I have a bunch of ps and pdf files with diverse page formats that I
would like to compile in one single file, in DIN-A4 format.

In a first step, I want to create ps files that are all in A4 format.
Here is the gs command-line I have worked out so far (comes mostly from
the pdf2ps script):

gs -q -dNOPAUSE -dBATCH -dSAFER -dFIXEDMEDIA
-sDEVICE=pswrite -sOutputFile=output.ps -sPAPERSIZE=a4 -c save pop -f
input.pdf

Trouble is, despite the PAPERSIZE and FIXEDMEDIA options, if I open
output.ps in gv, it still uses the old bounding box (that is, not A4),
which is no good since it will likely mess up the following steps. And
also, if I display the output.ps in A4, the pages are not centered.

Is there a gs wizard out there that can tell me:
- how to rewrite the bounding box so that it's clean A4 for all output files
- how to center the smaller pages on the new A4 pages

Thanks!
Tom
--
Thomas Guignard
Laboratory of Electromagnetics and Acoustics
Swiss Federal Institute of Technology, Lausanne
Helge Blischke
2004-10-26 11:46:00 UTC
Permalink
Post by Thomas Guignard
Hi there
I have a bunch of ps and pdf files with diverse page formats that I
would like to compile in one single file, in DIN-A4 format.
In a first step, I want to create ps files that are all in A4 format.
Here is the gs command-line I have worked out so far (comes mostly from
gs -q -dNOPAUSE -dBATCH -dSAFER -dFIXEDMEDIA
-sDEVICE=pswrite -sOutputFile=output.ps -sPAPERSIZE=a4 -c save pop -f
input.pdf
Trouble is, despite the PAPERSIZE and FIXEDMEDIA options, if I open
output.ps in gv, it still uses the old bounding box (that is, not A4),
which is no good since it will likely mess up the following steps. And
also, if I display the output.ps in A4, the pages are not centered.
- how to rewrite the bounding box so that it's clean A4 for all output files
- how to center the smaller pages on the new A4 pages
You'd have to write a little PostScript program that will be fed into gs
as the
very first file. It must intercept the setpagedevice operator and, if
the
operator's param dictionary contains a /PageSize key, provide the proper
image shift and - if the requested page size is greater than your A4 -
downscaling.


Helge
--
Helge Blischke
Softwareentwicklung
SRZ Berlin | Firmengruppe besscom
http://www.srz.de
tel: +49 30 75301-360
Thomas Guignard
2004-10-26 14:12:41 UTC
Permalink
Post by Helge Blischke
You'd have to write a little PostScript program that will be fed into gs
Thanks. So it seems I have no other way out of this than by taking a few
hours and learn how to program in PS... I've never done this, is it a
complicated language?
--
Thomas Guignard
Laboratory of Electromagnetics and Acoustics
Swiss Federal Institute of Technology, Lausanne
Jim Land
2004-10-26 15:57:11 UTC
Permalink
Post by Thomas Guignard
Post by Helge Blischke
You'd have to write a little PostScript program that will be fed into gs
Thanks. So it seems I have no other way out of this than by taking a few
hours and learn how to program in PS... I've never done this, is it a
complicated language?
http://www.geocities.com/SiliconValley/5682/Programming.html
Mark Carroll
2004-10-27 00:07:48 UTC
Permalink
Post by Thomas Guignard
Post by Helge Blischke
You'd have to write a little PostScript program that will be fed into gs
Thanks. So it seems I have no other way out of this than by taking a few
hours and learn how to program in PS... I've never done this, is it a
complicated language?
No, quite the opposite, if you can easily get your head around a
stack-based language. I spent ages wishing that something like
PostScript existed before I realised that PostScript was it! (I had
previously only seen PostScript generated by printer drivers and
suchlike, and didn't realise how human-usable it is.)

-- Mark
Russell Lang
2004-10-28 09:22:05 UTC
Permalink
Thomas,
I have a bunch of ps and pdf files with diverse page formats that I would like to compile in one
single file, in DIN-A4 format.
In a first step, I want to create ps files that are all in A4 format. Here is the gs command-line
gs -q -dNOPAUSE -dBATCH -dSAFER -dFIXEDMEDIA
-sDEVICE=pswrite -sOutputFile=output.ps -sPAPERSIZE=a4 -c save pop -f input.pdf
Trouble is, despite the PAPERSIZE and FIXEDMEDIA options, if I open output.ps in gv, it still uses
the old bounding box (that is, not A4), which is no good since it will likely mess up the
following steps. And also, if I display the output.ps in A4, the pages are not centered.
- how to rewrite the bounding box so that it's clean A4 for all output files
- how to center the smaller pages on the new A4 pages
Try the following, which doesn't do exactly as you asked.

Create a file adjustA4.ps
%!
<< /Policies << /PageSize 3 >>
/PageSize [595 842]
/InputAttributes currentpagedevice /InputAttributes get
mark exch {pop << /PageSize [595 842] >> } forall >>
Post by Helge Blischke
setpagedevice
Then use the command line
gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=out.pdf -c .setpdfwrite -f adjustA4.ps
file1.ps file2.ps file3.ps file4.ps
This will work if each file leaves the coordinate system unchanged. Otherwise you will need
... -c save -f file1.ps -c restore save -f file2.ps -c restore
Helge Blischke
2004-10-28 11:59:42 UTC
Permalink
Post by Russell Lang
Thomas,
I have a bunch of ps and pdf files with diverse page formats that I would like to compile in one
single file, in DIN-A4 format.
In a first step, I want to create ps files that are all in A4 format. Here is the gs command-line
gs -q -dNOPAUSE -dBATCH -dSAFER -dFIXEDMEDIA
-sDEVICE=pswrite -sOutputFile=output.ps -sPAPERSIZE=a4 -c save pop -f input.pdf
Trouble is, despite the PAPERSIZE and FIXEDMEDIA options, if I open output.ps in gv, it still uses
the old bounding box (that is, not A4), which is no good since it will likely mess up the
following steps. And also, if I display the output.ps in A4, the pages are not centered.
- how to rewrite the bounding box so that it's clean A4 for all output files
- how to center the smaller pages on the new A4 pages
Try the following, which doesn't do exactly as you asked.
Create a file adjustA4.ps
%!
<< /Policies << /PageSize 3 >>
/PageSize [595 842]
/InputAttributes currentpagedevice /InputAttributes get
mark exch {pop << /PageSize [595 842] >> } forall >>
shouldn't there be a test for the /Priority key in the body of the
forall loop?
Post by Russell Lang
Post by Helge Blischke
setpagedevice
Then use the command line
gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=out.pdf -c .setpdfwrite -f adjustA4.ps
file1.ps file2.ps file3.ps file4.ps
This will work if each file leaves the coordinate system unchanged. Otherwise you will need
... -c save -f file1.ps -c restore save -f file2.ps -c restore
--
Helge Blischke
Softwareentwicklung
SRZ Berlin | Firmengruppe besscom
http://www.srz.de
tel: +49 30 75301-360
Russell Lang
2004-10-29 10:21:34 UTC
Permalink
Helge,
Post by Helge Blischke
Post by Russell Lang
Create a file adjustA4.ps
%!
<< /Policies << /PageSize 3 >>
/PageSize [595 842]
/InputAttributes currentpagedevice /InputAttributes get
mark exch {pop << /PageSize [595 842] >> } forall >>
shouldn't there be a test for the /Priority key in the body of the
forall loop?
I hadn't considered that. I got the basic code from someone else.
The code worked because ghostscript didn't have Priority defined.
So the corrected code would be

%!
<< /Policies << /PageSize 3 >>
/PageSize [595 842]
/InputAttributes currentpagedevice /InputAttributes get
mark exch {1 index /Priority eq not
{pop << /PageSize [595 842] >>} if
} forall >>
Post by Helge Blischke
Post by Russell Lang
setpagedevice
lamond
2004-11-22 22:31:03 UTC
Permalink
I auto create eps files out of Cad and need to create PDF's at the
same papersize as the original,
eg. if an EPS is created with at 100 X 2000 mm them the PDF paper size
needs to be the same.
I cannot find the answer PLEASE HELP, STRESSED !

currently I fix the sheet size ("shtsizeX and shtsizeY")
as shown below but need to change this to create the original EPS
input size????????????? HOW ???

BoxText("No. of Error Files:
%ErrFCnt%%@CRLF%%CurrFile%%@CRLF%Building Parameters File...")
FileWrite(FileHndl, "-dNOPAUSE")
FileWrite(FileHndl, "-dBATCH")
FileWrite(FileHndl, "-r1200")
FileWrite(FileHndl, "-dDEVICEWIDTHPOINTS=%ShtSizeX%")
FileWrite(FileHndl, "-dDEVICEHEIGHTPOINTS=%ShtSizeY%")
FileWrite(FileHndl, "-dPDFSETTINGS=/printer")
FileWrite(FileHndl, "-dCompatibilityLevel=1.3")
FileWrite(FileHndl, "-sFONTMAP=")
FileWrite(FileHndl, "-sSUBSTFONT=Helvetica")
FileWrite(FileHndl, "-sFONTPATH=c:\psfonts")
FileWrite(FileHndl, "-sDEVICE=pdfwrite")
FileWrite(FileHndl, "-sOutputFile=%TargetD%\%TargetF%")
Ken Sharp
2004-11-23 08:23:36 UTC
Permalink
Post by lamond
I auto create eps files out of Cad and need to create PDF's at the
same papersize as the original,
EPS files don't have a papersize, or a Page Size. That's because they
are meant to be placed inside other documents.

What they do have is a BoundingBox, and possibly a HiResBoundingox.
These are inserted as comments (ie preceeded by %%) in the header of the
file.

More information on these, and on the required manipulation of EPS
files, can be found in Adobe Tech Note 5002, which can be found
somewhere on the Adobe Partners web site http://partners.adobe.com
Post by lamond
eg. if an EPS is created with at 100 X 2000 mm them the PDF paper size
needs to be the same.
I cannot find the answer PLEASE HELP, STRESSED !
You will need to parse out the bounding box comments, calculate the page
size from those, and emit a setpagedevice whose dictionary operand
contains a suitable /PageSize key and value. You may also need to move
the content of the page using the translate operator.

Finally you may need to append a showpage, EPS files need not contain
one. You probably also ought to start by defining the regular showpage
as a no-op too. Again, more details on this can be found in the relevant
Tech Note.


Ken
Russell Lang
2004-11-23 10:11:44 UTC
Permalink
Post by Ken Sharp
Post by lamond
I auto create eps files out of Cad and need to create PDF's at the
same papersize as the original,
EPS files don't have a papersize, or a Page Size. That's because they
are meant to be placed inside other documents.
What they do have is a BoundingBox, and possibly a HiResBoundingox.
These are inserted as comments (ie preceeded by %%) in the header of the
file.
If the file claims to be EPS, then ghostscript will do the encapsulation
and add the showpage. Add the command line option -dEPSCrop
which is documented in doc/Use.htm This sets the page size from
the EPS bounding box.

Thomas Guignard
2004-10-28 16:02:08 UTC
Permalink
Hi Russel

I tried your tipp and I get the following error (see bottom). Is this
what you were afraid of, Helge?

Thanks for your help,
T.

(sorry, was not able to learn postscript so fast, I'm still struggling
with the tutorials, so Russ' tip came in handy)


[***@lemapc77 bricole]$ gs -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrit
e -sOutputFile=out.pdf -c .setpdfwrite -f adjustA4.ps test.ps
GNU Ghostscript 7.05 (2002-04-22)
Copyright (C) 2002 artofcode LLC, Benicia, CA. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Loading NimbusRomNo9L-Regu font from /usr/share/fonts/default/Type1/n02100
3l.pfb... 2429084 1059918 1642520 347466 0 done.
Loading NimbusSanL-Regu font from /usr/share/fonts/default/Type1/n019003l.
pfb... 2783948 1382528 1662616 358654 0 done.
Using NimbusSansL-Regu font for NimbusSanL-Regu.
Error: /unmatchedmark in --.dicttomark--
Operand stack:
--dict:3/3(L)--
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostri
ngval-- 2 %stopped_push --nostringval-- --nostringval-- --nostri
ngval-- false 1 %stopped_push 1 3 %oparray_pop 1 3 %opar
ray_pop 1 3 %oparray_pop .runexec2 --nostringval-- --nostringv
al-- --nostringval-- 2 %stopped_push --nostringval-- --nostringv
al-- --nostringval--
Dictionary stack:
--dict:1051/1123(ro)(G)-- --dict:0/20(G)-- --dict:88/200(L)--
Current allocation mode is local
Current file position is 179
GNU Ghostscript 7.05: Unrecoverable error, exit code 1
--
Thomas Guignard
Laboratory of Electromagnetics and Acoustics
Swiss Federal Institute of Technology, Lausanne
Russell Lang
2004-10-29 10:26:22 UTC
Permalink
I tried your tipp and I get the following error (see bottom). Is this what you were afraid of,
Helge?
e -sOutputFile=out.pdf -c .setpdfwrite -f adjustA4.ps test.ps
GNU Ghostscript 7.05 (2002-04-22)
Copyright (C) 2002 artofcode LLC, Benicia, CA. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Loading NimbusRomNo9L-Regu font from /usr/share/fonts/default/Type1/n02100
3l.pfb... 2429084 1059918 1642520 347466 0 done.
Loading NimbusSanL-Regu font from /usr/share/fonts/default/Type1/n019003l.
pfb... 2783948 1382528 1662616 358654 0 done.
Using NimbusSansL-Regu font for NimbusSanL-Regu.
Error: /unmatchedmark in --.dicttomark--
Because the error message is after the messages about the fonts,
the error must be occuring within your PostScript code,
rather than in adjustA4.ps.

The code I wrote changes the list of available page sizes to say
"You can have any page size you want, so long as it is A4".

It worked with my test.ps. I don't know what is in your test.ps.
Thomas Guignard
2004-11-01 16:20:00 UTC
Permalink
Just wanted you to know that I solved my problem by using psresize (how
come I never thought about using it...?)

I intend now to write a little shell script that will create booklets
automatically (computing needed values and so on).
I will post the script when I'm finished.

In the mean time, here are the steps I use now to create A4 booklets
(that is, two A5 pages on each A4 sheet) out of weird-sized pdf files.

In this example
- the input.pdf file has 94 pages
- one blank page is missing after the title page, which causes the left
pages to appear on right, this is corrected on step 2
- the paper size of the input.pdf file is 7x9inches (obtained via
acroread, I plan to get these values automatically)
- I want to make only one booklet, this is why I choose the "signature"
to be 96 in step 3

1. pdf2ps input.pdf input.ps
2. psselect -p1,_,2-94 input.ps fixed.ps
3. psbook -s96 fixed.ps reordered.ps
4. psresize -pA4 -W7in -H9in reordered.ps resized.ps
5. psnup -pA4 -2 resized.ps book.ps

Then I can print the book.ps (paying attention that the spooler I choose
is the one configured to handle short-edge flipping duplex!).

I'm pretty happy with the results, although there are still (at least)
two minor problems:

- the "BoundingBox" property is not changed on step 4. I have absolutely
no idea on the consequences of this. Everything seems fine, it appears
OK in gv after I set the paper size to A4 (by default, gv sticks to this
BBox property), and it prints OK. But I guess it would be cleaner if
this property was set right. Any clues/hints/comments?
(it is probably because of this that I must use the -pA4 option again on
step 5)

- Printer margins are not taken into consideration. In the document I
wanted to print, there were directory-style chapter titles on the outter
page edges. These were put (correctly) on the paper edges in the A4
version, but they do not appear correctly when printed, since they are
outside the printer margins. For now, I have no clue as how to handle
this better. I just live without these.

Thanks for your help, even if I end up not writing any PS scripts after
all. I have nothing against PS, it's just that I know how to write shell
scripts but not PS ones (still).

T.
--
Thomas Guignard
Laboratory of Electromagnetics and Acoustics
Swiss Federal Institute of Technology, Lausanne
Thomas Guignard
2004-11-02 08:44:31 UTC
Permalink
Post by Thomas Guignard
I will post the script when I'm finished.
Here is the script I was referring to:
http://www.timtom.ch/downloads/scripts/pdf2book.sh

Please tell me what you think about it. The same issues I already
discussed apply to it as well. Plus, the grep routines to get pages and
size info are very unefficient and need to be changed, I guess.

T.
--
Thomas Guignard
Laboratory of Electromagnetics and Acoustics
Swiss Federal Institute of Technology, Lausanne
Horst Kiehl
2004-11-02 16:50:31 UTC
Permalink
- Printer margins are not taken into consideration. In the document I wanted to print, there were directory-style chapter titles
on the outter page edges. These were put (correctly) on the paper edges in the A4 version, but they do not appear correctly when
printed, since they are outside the printer margins. For now, I have no clue as how to handle this better. I just live without
these.
You could insert another step that uses pstops. It can do precise
scaling and shifting of the content on the page; you could scale and
shift the page contents according to the printable area of your
printer.

According to the manual page (PSUtils Release 1 Patchlevel 17), pstops
first does the scaling/shifting and then clipping to the specified page
size, so this should be an easy step before or after your step 4
(depending on the page size you'd like to operate on).

Horst
Loading...