Tuesday, July 24, 2012

Create PDF file from Postscript

It is really hard to create PDF from program such as VB, C, Foxpro or any programming language. Most people use Crystal Report or create a Word document then use the PDF printer to print it as a PDF. After the research I did, I can manage to create PDF from a Postscript file. Basically the postscript file is a text file, so you can use any programming language to generate it. Then use the Adobe distiller to convert the .ps file to .pdf. Let me show you the sample file.

%% Postscript file header
%!PS-Adobe-3.0




%% Font Encoding for French accent
/Courier
  << /Courier findfont () forall >>
  begin
    /Encoding ISOLatin1Encoding 256 array copy def currentdict
  end
  definefont pop


%% Change the font to 8 points Univers, move to X:40, Y:20 and display Hello World
/Univers 8 selectfont
  40 20 moveto
  (Hello World) show


%% Change to 10 points Courier and display back slash and brackets
/Courier 10 selectfont
  40 50 moveto
  (this is a back slash : \\) show
  40 70 moveto
  (these are brackets : \(\)) show


%% Change the width and height scale and display the text
gsave
  /Univers 10 selectfont 1.6 0.9 scale
  40 90 moveto
  (This is a test) show
grestore


%% Set Univers Bold font
/Univers-Bold 10 selectfont




%% Line, Box, EPS and Colour settings


%% Change to black colour, set the line width
0 setgray
0.2 setlinewidth


%% Draw a line
35 678 moveto
600 678 lineto
stroke


%% Drawing a box
0.7 setgray
0 setlinewidth
newpath
  160 756 moveto
  540 756 lineto
  540 770 lineto
  160 770 lineto
closepath
fill


%% Draw a EPS polygon
0.5 0.2 0.56 setrgbcolor
newpath
  16 69 moveto
  38 139 56 239 286 209 curveto
  286 215 31 192 16 69 curveto
closepath
fill




%% Finally use show page to complete the current page
showpage


With using PS file to PDF is much much easier than any other method I tried, but the postscript actually has a lot move comments then I mentioned. I am still trying to gather more information and post it to this blog. any comment, please post it here for me.