Accessing Ghostscript API from PureBasic / Postscript to PDF
Posted: Wed Jul 21, 2004 10:50 pm
Nothing really tough, but I like the possibility to provide PDF creation from a PostScript file without any custom installation, but delivering Ghostscript with the program itself.
You can download all the needed files in case you want:
http://host4scripts.de/pub/pdfgs.zip
Hope it works for you & is useful. Feedback welcome.
You can download all the needed files in case you want:
http://host4scripts.de/pub/pdfgs.zip
Hope it works for you & is useful. Feedback welcome.
Code: Select all
;--- 00 Info
;
; Accessing the ghostscript (gs) api from PureBasic, 21. July 2004 by Max.
;
; Ghostscript API: http://www.cs.wisc.edu/~ghost/doc/cvs/API.htm
; Examples: http://www.cs.wisc.edu/~ghost/doc/gsapi.htm
; PureBasic Forum: http://forum.purebasic.com
; GNU Ghostscript 7.06 downloadable at:
; http://prdownloads.sourceforge.net/ghostscript/gs706w32.exe?download
; GPL Licensing: http://www.gnu.org/copyleft/gpl.html
;
; Goal was to evaluate if it is possible (and suitable) to create PDF files from PS input
; without the need of a seperate ghostscript installation, meaning:
; - all needed files, own program as ghostscript, should be packaged into one installer
; - no messing with existing gs installations
; - no access of registry required, so easy installation and even more easy deinstallation
;
; Not checked yet:
; - minimum requirement of ghostscript files (i.e. unneeded files)
; - passing PS input from StdIn directly to the gs api without a seperate input ps file
; - using the graphical rendering abilities of gs to create PDF output on the display
; - converting other file types
;
; Installation:
; - Copy and paste this code to PureBasic
; - Create a fitting directory structure and define the variables according to it
; I am using:
; Z:\PDFGS (for the main program, the gs DLL and the test input file
; Z:\PDFGS\GS (fonts and libs)
; - Save the PB file
; - Download and install GS 7.06 to the standard paths
; - Copy all files from :\gs\fonts, :\gs\gs.706\lib and the dll gsdll32.dll (:\gs\gs.706\bin)
; to your PB source directory
; - Rename the original gs installation directory, so it doesn't interfere.
;
; Good luck!
;--- 01 Program directory, defined for convenience
Path.s = "z:\\pdfgs"
;--- 02 Defining the structure and variable the function gsapi_revision requires
Structure TGS_Revision
strProduct.l
strCopyright.l
intRevision.l
intRevisionDate.l
EndStructure
GS_Revision.TGS_Revision
;--- 03 Other ghostscript variables
intGSInstanceHandle.l
callerHandle.l
;--- 04 Array that will hold the pointers to the parameters for the ghostscript call
Dim gsargv.l(9)
;--- 05 Passing the locations of the fonts, the libs and the gsdll32.dll as environment variable
; without this step, the ghostscript paths are sought in the registry, messing with a regular
; installation.
SetEnvironmentVariable_("GS_LIB",Path.s+"\\gs")
SetEnvironmentVariable_("GS_DLL",Path.s+"\\gsdll32.dll")
;--- 06 Opening the DLL
OpenGSDLL=OpenLibrary(0,Path.s+"\\gsdll32.dll")
If OpenGSDLL
;--- 07 Checking the revision of the ghostscript DLL (gs32dll.dll)
; if you use another version, change the revision check (or just remove it)
Result=CallFunction(0,"gsapi_revision",@GS_Revision,SizeOf(TGS_Revision))
If GS_Revision\intRevision=706
;--- 08 Revision fits, so start a new instance of ghostscript
Result = CallFunction(0,"gsapi_new_instance",@intGSInstanceHandle,0)
;--- 09 Define array of 10 parameters and pass them as pointers, hence the @
gsargv(0) = @"ps2pdf" ;actual value doesn't matter */
gsargv(1) = @"-dNOPAUSE"
gsargv(2) = @"-dBATCH"
gsargv(3) = @"-dSAFER"
gsargv(4) = @"-sDEVICE=pdfwrite"
gsargv(5) = @"-sOutputFile=c:\out.pdf" ;PDF output file
gsargv(6) = @"-c";
gsargv(7) = @".setpdfwrite";
gsargv(8) = @"-f";
gsargv(9) = @"z:\pdfgs\Testseite.ps" ;PostScript input file
gsargc=10 ;Number of parameters passed
;--- 10 Initializing the api & passing the array of arguments
Result = CallFunction(0,"gsapi_init_with_args",intGSInstanceHandle,gsargc,@gsargv())
;--- 11 Return value of 0 means "No Error"
Debug "Input file: "+PeekS(gsargv(9))
Debug "Output param: "+PeekS(gsargv(5))
If Result=0
Debug "No error."
Else
Debug "Error: "+Str(Result)
EndIf
;--- 12 Closing all gs related stuff
Result = CallFunction(0,"gsapi_exit",intGSInstanceHandle)
Result = CallFunction(0,"gsapi_delete_instance",intGSInstanceHandle)
EndIf
CloseLibrary(0)
EndIf