1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| ;--- 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 = "C:pdf_ghost"
;--- 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
Global Dim gsargv(10)
;--- 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)
Debug GetEnvironmentVariable("GS_LIB")
SetEnvironmentVariable("GS_DLL",Path.s+"\gs\gsdll32.dll")
Debug GetEnvironmentVariable("GS_DLL")
;--- 06 Opening the DLL
Debug path
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))
Debug GS_Revision\intRevision
Debug GS_Revision\intRevisionDate
Debug GS_Revision\StrCopyright
If GS_Revision\intRevision=920
;--- 08 Revision fits, so start a new instance of ghostscript
Result = CallFunction(0,"gsapi_new_instance",@intGSInstanceHandle,0)
Debug result
gsargv(0) =@"ps2pdf"
gsargv(1) = @"-dNOPAUSE"
gsargv(2) = @"-dBATCH"
gsargv(3) = @"-dSAFER"
gsargv(4) = @"-sDEVICE=pdfwrite"
gsargv(5) = @"-sOutputfile=page1.pdf"
gsargv(6) = @"-c"
gsargv(7) = @".setpdfwrite"
gsargv(8) = @"-f"
gsargv(9) = @"avis1.ps"
gsargc.i=10 ;Number of parameters passed
Result= CallFunction(0,"gsapi_set_arg_encoding",intGSInstanceHandle,1)
Debug result
;--- 10 Initializing the api & passing the array of arguments
Debug "Ligne 114"
Debug @gsargv()
nombre.l = @gsargv()
Result = CallFunction(0,"gsapi_init_with_args",intGSInstanceHandle,gsargc, @gsargv())
Debug Str(result) + "Ligne 123"
;--- 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 |
Partager