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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| ;/////////////////////////////////////////////////////////////////////////////////
;GhostScriptHelper.
;
;©nsxSoftware 2011.
;==============
; Stephen Rodriguez (srod)
; Created with Purebasic 4.6 for Windows.
;
; Platforms: Windows.
; Fully Unicode compliant.
;
;Thanks to progi1984 for his GhostScript library from which I stole the ps2pdf routine and converted to Unicode etc.
;/////////////////////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////////////////////
;The following function attempts to locate the GhostScript library on the user's computer. This assumes that the GhostScript installer was used.
;If successful, returns the full path (including the library name) of the latest version of the library found on the machine,
;otherwise an empty string is returned.
;Failure does not necessarily mean that the library is not present - just that we cannot locate it.
Procedure.s GhostScriptHelper_GetGhostScriptInstallation()
Protected result$, hKey1, numSubkeys, maxSubKeyLen, nameBuffer, i, t1, t1$, key$, latestKey, latestKey$
Protected bufferSize, type
;Attempt to open the relevant registry parent key.
If RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, "Software\GPL Ghostscript", 0, #KEY_READ, @hKey1) = #ERROR_SUCCESS And hKey1
If RegQueryInfoKey_(hKey1, 0, 0, 0, @numSubkeys, @maxSubKeyLen, 0, 0, 0, 0, 0, 0) = #ERROR_SUCCESS
If numSubkeys And maxSubKeyLen
nameBuffer = AllocateMemory((maxSubKeyLen+1)<<(SizeOf(CHARACTER)-1))
If nameBuffer
For i = 0 To numSubkeys - 1
t1 = maxSubKeyLen+1
RegEnumKeyEx_(hKey1, i, nameBuffer, @t1, 0, 0, 0, 0)
key$ = PeekS(nameBuffer)
If key$
t1$ = RemoveString(key$, ".")
t1 = Val(key$)
If t1 > latestKey
latestKey = t1
latestKey$ = key$
EndIf
EndIf
Next
FreeMemory(nameBuffer)
If latestKey$
latestKey$ = "Software\GPL Ghostscript\" + latestKey$
EndIf
EndIf
EndIf
EndIf
RegCloseKey_(hKey1)
;latestKey$ contains the subkey name of the relevant registry entry. We just have to read it's value.
If latestKey$
If RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, latestKey$, 0, #KEY_READ, @hKey1) = #ERROR_SUCCESS And hKey1
RegQueryValueEx_(hKey1, "GS_DLL", 0, @type, 0, @bufferSize)
If type = #REG_SZ And bufferSize
nameBuffer = AllocateMemory(buffersize)
If nameBuffer
If RegQueryValueEx_(hKey1, "GS_DLL", 0, 0, nameBuffer, @bufferSize) = #ERROR_SUCCESS
result$ = PeekS(nameBuffer)
EndIf
FreeMemory(nameBuffer)
EndIf
EndIf
RegCloseKey_(hKey1)
EndIf
EndIf
EndIf
ProcedureReturn result$
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////////////////////
;The following uses GhostScript to convert a ps file to pdf.
;ghostScript$ should contain the complete path to the GhostScript dll. Either use a local copy of the dll stored in the application
;folder or, if prefering to use the GhostScript installation itself, try locating the GhostScript installation folder using the
;above GhostScriptHelper_GetGhostScriptInstallation() function.
;Returns zero if an error.
Procedure.i GhostScriptHelper_ps2pdf(ghostScript$, inputFile$, outputFile$="")
Protected result, libID, t1$, gsargc = 10, code, instance, utf8Buffer, *ptr
Protected Dim gsargv.i(gsargc)
If ghostScript$ And inputFile$ And FileSize(inputFile$) > 0
If outputFile$ = ""
t1$ = GetExtensionPart(inputFile$)
If t1$
outputFile$ = ReplaceString(inputFile$, t1$, "pdf")
Else
outputFile$ = inputFile$ + ".pdf"
EndIf
EndIf
;Create a UTF-8 array of command strings.
utf8Buffer = AllocateMemory(2048)
If utf8Buffer
*ptr = utf8Buffer
gsargv(0) = ?GhostScript_ps2pdf_ps2pdf
gsargv(1) = ?GhostScript_ps2pdf_dNOPAUSE
gsargv(2) = ?GhostScript_ps2pdf_dBATCH
gsargv(3) = ?GhostScript_ps2pdf_dSAFER
gsargv(4) = ?GhostScript_ps2pdf_sDEVICEpdfwrite
gsargv(6) = ?GhostScript_ps2pdf_C
gsargv(7) = ?GhostScript_ps2pdf_setpdfwrite
gsargv(8) = ?GhostScript_ps2pdf_F
PokeS(*ptr, "-sOutputFile="+outputFile$, -1, #PB_UTF8)
gsargv(5) = *ptr : *ptr + StringByteLength("-sOutputFile="+outputFile$, #PB_UTF8) + 1
PokeS(*ptr, inputFile$, -1, #PB_UTF8)
gsargv(9) = *ptr
libID = OpenLibrary(#PB_Any, ghostScript$)
If libID
code = CallFunction(libID, "gsapi_new_instance", @instance, #Null)
If code >= 0
code = CallFunction(libID, "gsapi_init_with_args", instance, gsargc, @gsargv())
If code >=0
result = #True
EndIf
CallFunction(libID, "gsapi_exit", instance)
CallFunction(libID, "gsapi_delete_instance", instance)
EndIf
CloseLibrary(libID)
EndIf
FreeMemory(utf8Buffer)
EndIf
EndIf
ProcedureReturn result
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////////////////////
;DATA SECTION (UTF8 STRINGS).
DataSection
GhostScript_ps2pdf_ps2pdf: ;'ps2pdf'
Data.a 112, 115, 50, 112, 100, 102, 0
GhostScript_ps2pdf_dNOPAUSE: ;'-dNOPAUSE'
Data.a 45, 100, 78, 79, 80, 65, 85, 83, 69, 0
GhostScript_ps2pdf_dBATCH: ;'-dBATCH'
Data.a 45, 100, 66, 65, 84, 67, 72, 0
GhostScript_ps2pdf_dSAFER: ;'-dSAFER'
Data.a 45, 100, 83, 65, 70, 69, 82, 0
GhostScript_ps2pdf_sDEVICEpdfwrite: ;'-sDEVICE=pdfwrite'
Data.a 45, 115, 68, 69, 86, 73, 67, 69, 61, 112, 100, 102, 119, 114, 105, 116, 101, 0
GhostScript_ps2pdf_C: ;'-c'
Data.a 45, 99, 0
GhostScript_ps2pdf_setpdfwrite: ;'.setpdfwrite'
Data.a 46, 115, 101, 116, 112, 100, 102, 119, 114, 105, 116, 101, 0
GhostScript_ps2pdf_F: ;'-f'
Data.a 45, 102, 0
EndDataSection
;///////////////////////////////////////////////////////////////////////////////// |