Convert and process postscript to pdf

I know barely enough to survive in this digital world.

I have many one-page postscript files (graphics / images) that I want to convert to pdf and automatically crop to a narrow border. I'm on windows right now (I'm using linux too, so feel free to post code for Linux)

In the past, I was successful by combining Ghostscript gswin32c.exe and Caliber pdfmanipulate.exe. This is probably a familiar approach to many here.

But this approach is fraught with problems for several reasons.

One problem arose after I “upgraded” to the 64-bit gswin64c.exe. The 32-bit version of gswin32c.exe still works on my system, so I can't complain too much.

Another issue occurred while working with postscript files that may have been incorrectly encoded. There seem to be at least two problems, but I'm not sure who, if any, is responsible, or if both of them. One of the problems is that the bounding box line, for example, %% BoundingBox: 135 179 484 587 is not always placed on the second line from the top. I understand that this can be a problem. Another problem is that the bounding box above corresponds to the Portrait orientation in Ghostscript, but cropping follows the Landscape orientation. Another problem that I have not identified is that for some files, trimming seems pretty random.

So here is my 32-bit approach (which works for high-quality files), followed by a 64-bit adaptation that doesn't work (perhaps because it calls some kind of pypdf script on my machine, and not a fixed script, caliber provided, if I understand https://bugs.launchpad.net/ubuntu/+source/calibre/+bug/800551 and http://www.mobileread.com/forums/archive/index.php/t-103097. html , but I just guess and don't know the workaround):

@echo off echo batch processing with Latex ps2pdf followed by Ghostscript gswin64c.exe and Calibre2 pdfmanipulate.exe for %%I in (*.ps,*.eps) do ( "C:\Program Files\MiKTeX 2.9\miktex\bin\x64\ps2pdf" %%I ) for %%I in (*.pdf) do ( "C:\Program Files (x86)\Ghostscript\gs9.00\bin\gswin32c.exe" -dSAFER -dNOPAUSE -dBATCH
-sDEVICE#bbox "%%I" 2> bounding "C:\Program Files (x86)\Calibre2\pdfmanipulate.exe" crop -o "%%~nICropped32.pdf" -b bounding "%%I" pause "C:\Program Files\Ghostscript\gs9.04\bin\gswin64c.exe" -dSAFER -dNOPAUSE -dBATCH
-sDEVICE#bbox "%%I" 2> bounding "C:\Program Files (x86)\Calibre2\pdfmanipulate.exe" crop -o "%%~nICropped64.pdf" -b bounding "%%I" pause )

The above 32-bit approach works with high quality files, for example. Postscript 3 level created by PSTricks or the standard Maple 2D graphics adapter, but not for older files, for example. A Level 2 postscript (if any) created by the Maple classic graphics driver.

. epstopdf LaTeX (MiKTeX). Maple. , postscript, PSTricks , Matlab.

, . , , . BoundingBox Portrait/Landscape, .

. linux . , "".

, , , , () pdf.

EDIT: . gswin32c/pdfmanipulate postscript 3, "bounding" , :

%% BoundingBox: 34 128 567 667 %% HiResBoundingBox: 34.364390 128.875004 566.054069 666.071980

. %% BoundingBox %% HiResBoundingBox

postscript 2 (, , ), "" :

%% BoundingBox: 189 137 574 467 %% HiResBoundingBox: 189.485994 137.843996 573.299983 466.668478

%% BoundingBox: 135 179 484 587 (135 179 484 587) , postscript ( ), , Ghostview/Ghostscript, .

Ghostscript...

, 189 137 574 467 - ...

2. :

, , ,

, --- , , - :

, 2 , , Ghostscript.

Ghostscript, . Ghostscript/Calibrate http://www.mobileread.com/forums/archive/index.php/t-72885.html , , .

, Ghostscript, , .

" , " " 64- gswin64c.exe"    , , ? , ......

, , : https://bugs.launchpad.net/ubuntu/+source/calibre/+bug/800551, http://www.mobileread.com/forums/archive/index.php/t-103097.html, - .

, , PostScript . PostScript, "%", . BoundingBox .

, . , %%, Ghostview. Ghostview , . , . , - , , Ghostview. %% Bounding Box, Caliber/pdfmipulate pdf , , %%. , "" .

, .....

Adobe. adobe,

" DSC EPS , , EPS. . "

http://partners.adobe.com/public/developer/en/ps/5002.EPSF_Spec.pdf

Adobe "". , , PDF eps, .

Ghostscript DSC, , ProcessDSC true, ( BoundingBox ).

pdfmanipulate pdf .

. , LaTeX ps2pdf, PostScript, Ghostscript PDF. , Ghostscript , ?

.

, " ", , - , .....

, , "" ( , ) . , , , . , , , . .

, , , http://www.charlietanksley.net/philtex/reading-pdfs-on-portables/

, , ,

. , , , , , , Ghostscript epstopdf , , , , ...

, 2 , Ghostscript, BoundingBox, . 4 , ( ). "translate" PostScript (, 0,0, ). , , width = right-left height = top-bottom. PostScript Ghostscript pdfwrite, PDF.

, . , pdfwrite, , , . .

, , , , . - . , , . , , .

, Matlab, Maple, PSTricks, (, , ), , %%.

%% Bounding Box , , Ghostscript Caliber pdfwrite - .

, , , .

, postscript ? 420 .

, , .

EDIT 3. .

postscript , : %% BoundingBox: 135 179 484 587

Ghostscript gswin64c/gswin32c , viz

for %%I in (*.ps,*.eps) do ("C:\Program Files\Ghostscript\gs9.04\bin\gswin64c.exe" -dSAFER -dNOPAUSE -dBATCH -dAutoRotatePages=/None -sDEVICE#bbox "%%I" 2> bounding)

:

%% BoundingBox: 145 189 475 574 %% HiResBoundingBox: 145.331574 189.485994 474.155986 573.299983

ps2pdf, Ghostscript gswin64c, ..

for %%I in (*.ps,*.eps) do ("C:\Program Files\MiKTeX 2.9\miktex\bin\x64\ps2pdf" %%I)
for %%I in (*.pdf) do ("C:\Program Files\Ghostscript\gs9.04\bin\gswin64c.exe" -dSAFER -dNOPAUSE -dBATCH -dAutoRotatePages=/None -sDEVICE#bbox "%%I" 2> bounding)

:

%% BoundingBox: 189 137 574 467 %% HiResBoundingBox: 189.395994 137.843996 573.299983 466.668478

, , ps pdf ps2pdf , . , ps2pdf - , , eps2pdf . , . , Ghostcript, . ( ) . - :

for %%I in (*.eps,*.ps) do ("C:\Program Files\MiKTeX 2.9\miktex\bin\x64\epstopdf" %%I)
for %%I in (*.pdf) do (
"C:\Program Files\Ghostscript\gs9.04\bin\gswin64c.exe" -dSAFER -dNOPAUSE -dBATCH -dAutoRotatePages=/None -sDEVICE#bbox "%%I" 2> bounding
"C:\Program Files (x86)\Calibre2\pdfmanipulate.exe" crop -o "%%~nICropped.pdf" -b bounding "%%I"
)
+5
5

, , , , ....

, BoundingBox PDF, PDF. , , (, , ), 90 .

, , , , . , BoundingBox PDF .

PostScript , :

1pass.ps

BoundingBox PostScript . , "SourceFileName" Eg, :

gs -sDEVICE=pdfwrite -sSourceFileName=classic.ps -o out.pdf 1pass.ps

out.pdf, BoundingBox PDF , .

%!PS  

%% redefine setpagedevice to prevent changes by the PostScript program  
%% But keep a copy under a different name, so we cna use it.  
/Oldsetpagedevice /setpagedevice load def  
/setpagedevice {pop} bind def  

(File to process is ) print SourceFileName ==  

/SourceFile SourceFileName (r) file def  
/BoxString 65535 string def  
/LLx 0 def  
/LLy 0 def  
/URx 0 def  
/URy 0 def  
/FoundBox false def  

/GetValues {  
  token {                   % read a PostScript token  
    /LLx exch def               % Assume its a number for now  
    token {  
      /LLy exch def  
      token {  
        /URx exch def  
        token {  
          /URy exch def  
          pop                       % Get rid of any remaining string data  
          true              % return success code  
        }{  
          (Failed to read a number from the string) ==  
          false             % return failure code  
        } ifelse  
      }{  
        (Failed to read a number from the string) ==  
        false               % return failure code  
      } ifelse  
    }{  
      (Failed to read a number from the string) ==  
      false                 % return failure code  
    } ifelse  
  } {  
    (Failed to read a number from the string) ==  
    false                   % return failure code  
  } ifelse  
} bind def  

{  
  SourceFile BoxString readline {  
    (%%BoundingBox:) anchorsearch {  
      pop                           %% discard matching string  
      GetValues             %% extract BBox  
      /FoundBox exch def        %% Note success/failure  
      exit                  %% exit this loop  
    } {  
      pop                   %% discard string, no match  
    } ifelse  
  } {  
    (Failed to find a %%BoundingBox comment) ==  
    exit                            %% No more data, exit loop  
  } ifelse  
} loop  

SourceFile closefile            %% close the file  

FoundBox {  
  (LLx = ) print LLx ==  
  (LLy = ) print LLy ==  
  (URx = ) print URx ==  
  (URy = ) print URy ==  
  > Oldsetpagedevice  
  LLx neg LLy neg translate  
  SourceFileName run  
} if  

2pass.ps

, , 1pass.ps:

  • PDF, PostScript , %%BoundingBox.
  • BoundingBox .

, , , PDF.

: , bbox, , . , , , :

:

  gs \
   -sDEVICE=bbox \
    classic.ps 2> bounding.txt

:

  gs \
   -sDEVICE=pdfwrite \
   -sBoxFileName=bounding.txt \
   -sPostScriptFileName=classic.ps \
   -o out.pdf \
    2pass.ps

PostScript classic.ps:

%!PS  

%% redefine setpagedevice to prevent changes by the PostScript program  
%% But keep a copy under a different name, so we cna use it.  
/Oldsetpagedevice /setpagedevice load def  
/setpagedevice {pop} bind def  

(Bounding Box parameters in file ) print BoxFileName ==  
(File to process is ) print PostScriptFileName ==  

/BoxFile BoxFileName (r) file def  
/BoxString 256 string def  
/HiResBoxString 256 string def  
/LLx 0 def  
/LLy 0 def  
/URx 0 def  
/URy 0 def  

BoxFile BoxString readline  % Read first line from file  
{  
  /BoxString exch def       % redefine string to be the one we read  
}{  
  (Encountered EOF before newline reading %%BoundingBox) == flush  
} ifelse  

BoxFile HiResBoxString readline % Read first line from file  
{  
  /HiResBoxString exch def      % redefine string to be the one we read  
}{  
  (Encountered EOF before newline reading %%HiResBoundingBox) == flush  
} ifelse  

BoxFile closefile               % close the file  

BoxString (%%BoundingBox:) anchorsearch  
{  
  pop                       % Get rid of the mathcing string  
  token {                   % read a PostScript token  
    /LLx exch def               % Assume its a number  
    token {  
      /LLy exch def  
      token {  
        /URx exch def  
        token {  
          /URy exch def  
          pop                       % Get rid of any remaining string data  
        }{  
          (Failed to read a number from the string) ==  
        } ifelse  
      }{  
        (Failed to read a number from the string) ==  
      } ifelse  
    }{  
      (Failed to read a number from the string) ==  
    } ifelse  
  } {  
    (Failed to read a number from the string) ==  
  } ifelse  
}{  
  print (does not contain a BoundingBox) ==  
} ifelse  

(LLx = ) print LLx ==  
(LLy = ) print LLy ==  
(URx = ) print URx ==  
(URy = ) print URy ==  

> Oldsetpagedevice  
LLx neg LLy neg translate  

PostScriptFileName run  
+4

BoundingBox , , ghostscript .

sh script ( Windows!)

for i in *.pdf ; 
do 
    gs -dSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox "$i" 2> bounding ; 
    pdfmanipulate crop -o "${i%.pdf}-cropped.pdf" -b bounding "$i" ; 
done

, grep :

for i in *.pdf ; 
do 
    grep '%%BoundingBox' "$i" > bounding ; 
    pdfmanipulate crop -o "${i%.pdf}-cropped.pdf" -b bounding "$i" ; 
done

Windows, cygwin script.

+4

" , Ghostscript gswin32c.exe Caliber pdfmanipulate.exe. , , ".

, 2 , , Ghostscript.

" , " " 64- gswin64c.exe"

, , ? , ......

, , PostScript . PostScript, "%", . BoundingBox .

, ( , DSC), PostScript, DSC. , , . PostScript %! PS-Abode-m.n, m n , DSC , , , - "m.n". BoundingBox PostScrip, DSC . : , .....

Ghostscript DSC, , ProcessDSC true, ( BoundingBox ).

. , LaTeX ps2pdf, PostScript, Ghostscript PDF. , Ghostscript , ?

, " ", , - , .....

, , , , 2 , Ghostscript, BoundingBox, . 4 , ( ). 'translate' PostScript (, 0,0, ). , , width = right-left height = top-bottom. PostScript Ghostscript pdfwrite, PDF.

, , , , . - . , , . , , .

, , , .

+2

, DSC, Ghostview , Ghostview:

  • Ghostscript (, )
  • , DSC.

PostScript , Ghostscript .

" "; ( DSC), . , .

, Ghostscript pdfwrite PDF . , , PDF PDF- (, PDF/A PDF/X).

PDF, , Ghostscript:

 gswin32c ^
  -o out.pdf ^
  -sDEVICE=pdfwrite ^
  -dPAGEWIDTHPOINTS=xx -dPAGEHEIGHTPOINTS=yy ^
  -dFIXEDMEDIA ^
  -c "-x -y translate" ^
  -f input.ps

xx, yy, x y BoundingBox , PostScript . , , , .

:

  • xx = urx - llx,
  • yy = ury - lly,
  • x = llx,
  • y = lly

, , PostScript , .

"ken.sharp AT artifex.com" URL-. , BoundingBox , ....

URL-, , 64- Ghostscript. , Ghostscript ?

+2

Answer calibration /pdfmanipulate.exe

Caliber has removed pdfmanipulate.exe from recent releases. I found that I need to go back to version 0.8.66 to get pdfmipulate, I downloaded the portable version: calibre-portable-0.8.66.zip

0
source

All Articles