#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=TextToPDF.ico #AutoIt3Wrapper_Outfile=./WinTextToPDF.exe #AutoIt3Wrapper_Res_Description=WinTextToPDF - converts text files to PDF and prints #AutoIt3Wrapper_Res_Fileversion=1.26.0.23 #AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y #AutoIt3Wrapper_Res_LegalCopyright=Script by Edward Mendelson #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker #AutoIt3Wrapper_AU3Check_Stop_OnWarning=y #AutoIt3Wrapper_Run_After=d:\dropbox\signfilesem.exe "%out%" #AutoIt3Wrapper_Run_Tidy=y #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ; WinTexttoPDF.exe - by Edward Mendelson http://wpdos.org #include #include #include #include #include #include Dim $a, $b, $batch, $cmln, $copyexe, $dir, $drv, $ext, $inuse, $t Dim $outext, $pth, $scrpt, $view, $q, $hdr, $mydir, $outstring Dim $print, $printexe, $select, $getname, $outlength, $sixtysix Global $textfile Global $currentprinter Global $defaultprinter Global $printername Global $printer_list[1] Global $printer_list_ext[1] Global $printer_radio_array[1] Global $imprimante Global $printername Global $gh Global $namefile Global $outpdf Global $specified Global $pgSize $scrpt = @ScriptName $pth = @ScriptDir $t = "WinTextToPDF" $qt = Chr(34) $space = " " $view = 0 $print = 0 $select = 0 $specified = 0 $silent = 0 $batch = 0 $desktop = 0 $sixtysix = 0 $afour = 0 $fixcr = 0 $landscape = 0 $pjlstring = "" $getpdfname = 0 $blank = @CRLF & @CRLF $cmd1 = "" $cmd2 = "" $cmd3 = "" $cmd4 = "" $cmd5 = "" $testext = "" $outfile = "" $font = "Courier" $options = '' $dosPrintOpts = " " $useDosPrint = 0 $doOptions = 0 ; wait for another instance to stop running before this one runs While 1 If _SingletonEx($scrpt, 1) = 0 Then $iSleep = @AutoItPID * 10 Sleep($iSleep) Else ExitLoop EndIf WEnd $mydir = @TempDir & "\TextToPDF" DirCreate($mydir) Dim $setting If FileExists(@ScriptDir & "\DOSPrinter.exe") Then $useDosPrint = 1 EndIf $cmln = $CmdLine[0] If $cmln = 0 Then $textfile = FileOpenDialog($t & " - Select a text file to convert to PDF", @HomeDrive & "\", "All (*.*)", 3) ; *.pcl If @error Then Cancelled() EndIf EndIf Local $gotoutfile = 0 If $cmln >= 1 Then If (($CmdLine[1] = "/?") Or ($CmdLine[1] = "-?") Or ($CmdLine[1] = "/help") Or ($CmdLine[1] = "?")) Then usagemsg() Else If FileExists($CmdLine[1]) Then $textfile = $CmdLine[1] Else MsgBox(0, $t, "File not found: " & $CmdLine[1]) Exit EndIf EndIf EndIf If $cmln >= 2 Then If (StringInStr(StringLower($CmdLine[2]), ".pdf")) Then ; MsgBox(0, '', $CmdLine[2]) $gotoutfile = 1 $outfile = $CmdLine[2] EndIf EndIf Local $nameSpec = 0 If $cmln >= 2 Then For $i = 2 To $CmdLine[0] Local $setdone = 0 If StringInStr($CmdLine[$i], "/namefile:") Then $nameSpec = 1 Local $nameLongString = $CmdLine[$i] $namefile = (StringRight($nameLongString, (StringLen($nameLongString)) - 10)) ;MsgBox(0, '2', $namefile) ;Exit EndIf If StringInStr($CmdLine[$i], "/printername:") Then $specified = 1 Local $printerLongString = $CmdLine[$i] $printername = (StringRight($printerLongString, (StringLen($printerLongString)) - 13)) ;MsgBox(0, '2', $printername) ;Exit EndIf If StringInStr($CmdLine[$i], "/options:") Then $doOptions = 1 Local $optionsLongString = $CmdLine[$i] $options = (StringRight($optionsLongString, (StringLen($optionsLongString)) - 9)) $options = $options & " " ConsoleWrite($options & @CRLF) EndIf Switch $CmdLine[$i] Case "/view" $setting = "/open" $view = 1 $setdone = 0 Case "/print" $setting = "/print" $setdone = 1 $print = 1 Case "/select" $setting = "/select" $setdone = 1 $select = 1 Case "/desktop" $setting = "/desktop" $setdone = 0 $desktop = 1 Case "/pjl" $setting = "/pjl" $setdone = 0 $pjl = 1 EndSwitch If $setdone = 0 Then If $CmdLine[$i] = "/batch" Then $batch = 1 $silent = 1 ElseIf $CmdLine[$i] = "/silent" Then $silent = 1 EndIf EndIf Next EndIf $textfile = _PathFull($textfile) ConsoleWrite("$textfile: " & $textfile & @LF) If $gotoutfile = 1 Then $outpdf = _PathFull($outfile) $outext = StringLower(StringRight($outpdf, 4)) If $outext = ".pdf" Then Else If $batch = 0 Then MsgBox(262144, $t, "The output filename must have a PDF extension." & @CRLF & @CRLF & _ "(This message also appears if you dropped more than one file on the application.)") EndIf Exit EndIf Else $testext = StringUpper(StringRight($textfile, 4)) If ($testext = ".TXT" Or $testext = ".PRN" Or $testext = ".ASC") Then $outstring = (StringLeft($textfile, (StringLen($textfile) - 4))) Else $outstring = $textfile EndIf If $desktop = 1 Then $outpdf = "" Local $datestring = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC $outpdf = $datestring & ".pdf" Else $outpdf = $outstring & ".pdf" EndIf EndIf If $nameSpec = 1 Then GetPDFName() EndIf ConsoleWrite($outpdf & @CRLF) ; if script/app name includes "v" then view PDF after conversion If (StringInStr(StringLower($scrpt), "v")) Then $view = 1 $print = 0 $select = 0 ElseIf (StringInStr(StringLower($scrpt), "print")) Then $print = 1 $view = 0 ElseIf (StringInStr(StringLower($scrpt), "select")) Then $print = 1 $view = 0 $select = 1 EndIf If (StringInStr(StringLower($scrpt), "66")) Then $sixtysix = 1 EndIf If (StringInStr(StringLower($scrpt), "a4")) Then $afour = 1 EndIf If (StringInStr(StringLower($scrpt), "land")) Then $landscape = 1 EndIf If (($view = 1) And ($print = 1)) Then If $batch = 0 Then MsgBox(262144, $t, "The viewing and printing options cannot both be active." & @CRLF & @CRLF & _ "Please consult the instructions for this program.") EndIf Exit EndIf If (($view = 1) And ($select = 1)) Then If $batch = 0 Then MsgBox(262144, $t, "The viewing and select-printer options cannot both be active." & @CRLF & @CRLF & _ "Please consult the instructions for this program.") EndIf Exit EndIf If (($specified = 1) And ($select = 1)) Then If $batch = 0 Then MsgBox(262144, $t, "The printername and select-printer options cannot both be active." & @CRLF & @CRLF & _ "Please consult the instructions for this program.") EndIf Exit EndIf If (($select = 1) And ($print = 1)) Then $print = 0 EndIf ;;;;;;;;;;;;;;;;; ;;; TESTING STRINGS HERE ; $print = 0 ; $select = 1 ;;;;;;;;;;;;;;;; If $select = 1 Then $print = 1 If (($select = 1) Or ($print = 1)) Then If (($desktop = 0) Or ($gotoutfile = 0)) Then Local $datestring = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC $outpdf = $mydir & "\" & $datestring & ".pdf" EndIf $printexe = $mydir & "\PDFXCview.exe" EndIf If FileExists($outpdf) Then If $batch = 1 Then FileDelete($outpdf) Else If $print = 0 Then $a = MsgBox(262449, $t, "Output file " & $outpdf & " exists. Overwrite it?") If $a = 2 Then Exit EndIf Else If $gotoutfile = 0 Then FileDelete($outpdf) EndIf EndIf EndIf EndIf If $print = 0 Then $finalpdf = $outpdf EndIf Local $iID = _WinAPI_GetUserDefaultLCID() $locString = _WinAPI_GetLocaleInfo($iID, $LOCALE_SLANGUAGE) If (StringInStr($locString, "United States")) Or (StringInStr($locString, "Canada")) Then Else $afour = 1 EndIf If $afour = 1 Then $pgSize = " -A4 -l66 " Else $pgSize = " " EndIf If $useDosPrint = 0 Then If FileExists(@ScriptDir & "\fixcr.txt") Then $fixcr = 1 If $fixcr = 1 Then FileDelete(@TempDir & "\fixcrout.txt") $f = FileOpen($textfile, 0) $sText = FileRead($f) $sText = StringRegExpReplace($sText, '\r(?!\n)', @CRLF) FileWrite(@TempDir & "\fixcrout.txt", $sText) FileClose($f) $textfile = (@TempDir & "\fixcrout.txt") EndIf EndIf If $useDosPrint = 0 Then If FileExists(@ScriptDir & "\TextToPDF.ini") Then Local $p = FileOpen(@ScriptDir & "\TextToPDF.ini") $options = FileReadLine($p, 1) FileClose($p) $options = $options & " " EndIf FileInstall("D:\Dropbox\text2pdf.exe", $mydir & "\text2pdf.exe", 1) ConsoleWrite(@ComSpec & " /c " & $mydir & "\" & "text2pdf.exe" _ & $pgSize & "-s10 -c100 -f" & $font & " " & _ $options & $qt & $textfile & $qt & " > " & $qt & $outpdf & $qt & @CRLF) RunWait(@ComSpec & " /c " & $mydir & "\" & "text2pdf.exe" _ & $pgSize & "-s10 -c100 -f" & $font & " " & _ $options & $qt & $textfile & $qt & " > " & $qt & $outpdf & $qt, $pth, @SW_HIDE) Else If $afour = 1 Then $dosPrintOpts = " /PageA4 " & $dosPrintOpts & " " EndIf If FileExists(@ScriptDir & "\DOSPrintOpts.ini") Then Local $p = FileOpen(@ScriptDir & "\DOSPrintOpts.ini") $dosPrintOpts = FileReadLine($p, 1) FileClose($p) $dosPrintOpts = $dosPrintOpts & " " EndIf ConsoleWrite(@ComSpec & " /c " & @ScriptDir & "\" & "DOSPrinter.exe" & " /PDF /FILE'" & $outpdf & "'" & $dosPrintOpts & $qt & $textfile & $qt & @CRLF) RunWait(@ComSpec & " /c " & $qt & $qt & @ScriptDir & "\" & "DOSPrinter.exe" & $qt & " /PDF /FILE'" & $outpdf & "'" & $dosPrintOpts & $qt & $textfile & $qt & $qt, $pth, @SW_HIDE) EndIf ConsoleWrite("desktop: " & $desktop & @LF) If $desktop = 1 Then $finalpdf = @DesktopDir & "\" & $datestring & ".pdf" FileMove($outpdf, $finalpdf, 1) FileDelete($outpdf) If $view = 1 Then ShellExecute($finalpdf) EndIf Exit EndIf If $useDosPrint = 0 Then If $fixcr = 1 Then FileDelete(@TempDir & "\fixcrout.txt") EndIf EndIf If $batch = 0 Then If $view = 1 Then ; MsgBox(0, '', "Next: " & $outpdf & @LF & $finalpdf) If Not $outpdf = $finalpdf Then FileMove($outpdf, $finalpdf, 1) FileDelete($outpdf) EndIf $inuse = _FileInUse($finalpdf) If $inuse = 0 Then ShellExecute($finalpdf) Else MsgBox(262144, $t, "Another version of this file is already open in your PDF viewer." & _ @CRLF & @CRLF & "Please close that copy of the file before proceeding.") Exit EndIf Exit EndIf EndIf If $select = 1 Then GetPrinter() Do Sleep(100) Until $printername $printtostring = " " & $qt & "/printto:" & $qt & $qt & $printername & $qt & " " Else $printtostring = " /print " EndIf If (($select = 1) Or ($print = 1)) Then FileInstall("d:\dropbox\GhostPCL\PDFV\PDFXCView.exe", $mydir & "\PDFXCView.exe", 0) FileInstall("d:\dropbox\GhostPCL\PDFV\Settings.dat", $mydir & "\Settings.dat", 0) FileInstall("d:\dropbox\GhostPCL\PDFV\resource.dat", $mydir & "\resource.dat", 0) FileInstall("d:\dropbox\GhostPCL\PDFV\Portable.dat", $mydir & "\Portable.dat", 0) If $select = 1 Then ; MsgBox(0, '', $outpdf) If Not @Compiled Then ConsoleWrite("Select:" & @LF & @ComSpec & " /c " & $printexe & $printtostring & $qt & $outpdf & $qt & @LF) RunWait(@ComSpec & " /c " & $printexe & $printtostring & $qt & $outpdf & $qt, $mydir, @SW_HIDE) ElseIf $specified = 1 Then ; MsgBox(0, '', $outpdf) $printtostring = $qt & $printername & $qt If Not @Compiled Then ConsoleWrite("Select:" & @LF & @ComSpec & " /c " & $printexe & $printtostring & $qt & $outpdf & $qt & @LF) RunWait(@ComSpec & " /c " & $printexe & $printtostring & $qt & $outpdf & $qt, $mydir, @SW_HIDE) Else If Not @Compiled Then ConsoleWrite(@ComSpec & " /c " & $printexe & $printtostring & $outpdf & @LF) RunWait(@ComSpec & " /c " & $printexe & " /print " & $outpdf, $mydir, @SW_HIDE) EndIf $inuse = _FileInUse($outpdf) While $inuse = 1 Sleep(10) WEnd If $gotoutfile = 0 Then FileDelete($outpdf) FileDelete($outpdf) EndIf EndIf Exit Func NotPCLFile() If $silent = 0 Then ; MsgBox(0, '', $textfile) $b = MsgBox(262452, "", "This file seems not to be a standard PCL file." & @CRLF & @CRLF & _ "Create a PDF file from it anyway?") If $b = 7 Then Exit EndIf EndIf ; Else ; Exit ;; EM testing 25 April 2013 EndFunc ;==>NotPCLFile Func Cancelled() If $batch = 0 Then MsgBox(262144, $t, "Script cancelled.") EndIf Exit EndFunc ;==>Cancelled Func _FileInUse($sFile) Local $hFile = _WinAPI_CreateFile($sFile, 2, 2, 0) If $hFile Then _WinAPI_CloseHandle($hFile) Return 0 EndIf Local $Error = _WinAPI_GetLastError() Switch $Error Case 32 ; ERROR_SHARING_VIOLATION Return 1 Case Else Return SetError($Error, 0, 0) EndSwitch EndFunc ;==>_FileInUse ;=============================================================================== ; Function Name: _FileInUse() ; Description: Checks if file is in use ; Parameter(s): $sFilename = File name ; Return Value(s): 1 - file in use (@error contains system error code) ; 0 - file not in use ;=============================================================================== ;~ Func _FileInUse($sFilename) ;~ Local $aRet, $hFile ;~ $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _ ;~ "str", $sFilename, _ ;lpFileName ;~ "dword", 0x80000000, _ ;dwDesiredAccess = GENERIC_READ ;~ "dword", 0, _ ;dwShareMode = DO NOT SHARE ;~ "dword", 0, _ ;lpSecurityAttributes = NULL ;~ "dword", 3, _ ;dwCreationDisposition = OPEN_EXISTING ;~ "dword", 128, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL ;~ "hwnd", 0) ;hTemplateFile = NULL ;~ $hFile = $aRet[0] ;~ If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1 ;~ $aRet = DllCall("Kernel32.dll", "int", "GetLastError") ;~ SetError($aRet[0]) ;~ Return 1 ;~ Else ;~ ;close file handle ;~ DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile) ;~ Return 0 ;~ EndIf ;~ EndFunc ;==>_FileInUse ;================================ Func GetPrinter() $regprinters = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Devices" $currentprinter = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\", "Device") $defaultprinter = StringLeft($currentprinter, StringInStr($currentprinter, ",") - 1) Dim $i = 1 Dim $erreur_reg = False While Not $erreur_reg $imprimante = RegEnumVal($regprinters, $i) $erreur_reg = @error If Not $erreur_reg Then _ArrayAdd($printer_list, $imprimante) _ArrayAdd($printer_list_ext, $imprimante & "," & RegRead($regprinters, $imprimante)) EndIf $i = $i + 1 WEnd _ArrayDelete($printer_list, 0) _ArrayDelete($printer_list_ext, 0) If UBound($printer_list) >= 2 Then ;if 2 or more printers available, we show the dialog Dim $groupheight = (UBound($printer_list) + 1) * 25 ;30 Dim $guiheight = $groupheight + 50 Dim $buttontop = $groupheight + 20 Opt("GUIOnEventMode", 1) $gh = GUICreate("Select a printer for this PDF file.", 400, $guiheight) Dim $font = "Verdana" GUISetFont(10, 400, 0, $font) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") GUISetFont(10, 400, 0, $font) GUICtrlCreateGroup("Available printers:", 10, 10, 380, $groupheight) Dim $position_vertical = 5 ; 0 For $i = 0 To UBound($printer_list) - 1 Step 1 GUISetFont(10, 400, 0, $font) $position_vertical = $position_vertical + 25 ;30 $radio = GUICtrlCreateRadio($printer_list[$i], 20, $position_vertical, 350, 20) _ArrayAdd($printer_radio_array, $radio) If $currentprinter = $printer_list_ext[$i] Then GUICtrlSetState($radio, $GUI_CHECKED) EndIf Next _ArrayDelete($printer_radio_array, 0) GUISetFont(10, 400, 0, $font) $okbutton = GUICtrlCreateButton("OK", 10, $buttontop, 50, 25) GUICtrlSetOnEvent($okbutton, "OKButton") Local $AccelKeys[2][2] = [["{ENTER}", $okbutton], ["^O", $okbutton]] GUISetAccelerators($AccelKeys) GUISetState() EndIf EndFunc ;==>GetPrinter Func OKButton() For $i = 0 To UBound($printer_radio_array) - 1 Step 1 If GUICtrlRead($printer_radio_array[$i]) = 1 Then $printername = StringLeft($printer_list_ext[$i], StringInStr($printer_list_ext[$i], ",") - 1) EndIf Next GUIDelete($gh) ; MsgBox(0, "", $printername) If Not @Compiled Then ConsoleWrite($printername & @LF) EndFunc ;==>OKButton Func CLOSEClicked() ;$printername = $defaultprinter GUIDelete($gh) Cancelled() ; MsgBox(0, "", $printername) ; Return $printername EndFunc ;==>CLOSEClicked Func GetPDFName() If FileExists($namefile) Then Sleep(10) $f = FileOpen($namefile) $outpdf = FileReadLine($f, 1) FileClose($f) Sleep(10) FileDelete($namefile) If (StringLeft($outpdf, 1)) = $qt Then $outlength = (StringLen($outpdf) - 1) $outpdf = (StringMid($outpdf, 2, $outlength)) EndIf If (StringRight($outpdf, 1)) = $qt Then $outlength = (StringLen($outpdf) - 1) $outpdf = (StringMid($outpdf, 1, $outlength)) EndIf If (StringRight($outpdf, 1)) = " " Then $outlength = (StringLen($outpdf) - 1) $outpdf = (StringMid($outpdf, 1, $outlength)) EndIf If (StringRight($outpdf, 1)) = " " Then $outlength = (StringLen($outpdf) - 1) $outpdf = (StringMid($outpdf, 1, $outlength)) EndIf If (StringLower($outpdf)) = ".pdf" Then $dt = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC $outpdf = @DesktopDir & "\" & "NameError-" & $dt & ".pdf" EndIf $outext = StringLower(StringRight($outpdf, 4)) If $outext = ".pdf" Then Else $outpdf = $outpdf & ".pdf" EndIf Else $dt = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC $outpdf = @DesktopDir & "\" & $dt & ".pdf" $n = MsgBox(1, $t, "Namefile not found. I will write a PDF named " & $dt & ".pdf to the desktop.") If $n = 2 Then Exit ; $dt = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC ; $outpdf = @DesktopDir & "\" & $dt & ".pdf" EndIf ; MsgBox(0, '', $outpdf) ; Exit EndFunc ;==>GetPDFName ; #FUNCTION# ==================================================================================================================== ; Name...........: _SingletonEx ; Description ...: Enforce a design paradigm where only one instance of the script may be running. ; Syntax.........: _SingletonEx($sOccurenceName[, $iFlag = 0]) ; Parameters ....: $sOccurenceName - String to identify the occurrence of the script. This string may not contain the \ character unless you are placing the object in a namespace (See Remarks). ; $iFlag - Behavior options. ; |0 - Exit the script with the exit code -1 if another instance already exists. ; |1 - Return from the function without exiting the script. ; Return values .: Success - The handle to the object used for synchronization (a mutex). ; Failure - 0 ; Author ........: Valik ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _SingletonEx($sOccurenceName, $iFlag = 0) Local Const $ERROR_ALREADY_EXISTS = 183 Local $tSecurityAttributes = 0 Local $handle = DllCall("kernel32.dll", "handle", "CreateMutexW", "struct*", $tSecurityAttributes, "bool", 1, "wstr", $sOccurenceName) If @error Then Return SetError(@error, @extended, 0) Local $lastError = DllCall("kernel32.dll", "dword", "GetLastError") If @error Then Return SetError(@error, @extended, 0) If $lastError[0] = $ERROR_ALREADY_EXISTS Then DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $handle[0]) If @error Then Return SetError(@error, @extended, 0) If BitAND($iFlag, 1) Then Return SetError($lastError[0], $lastError[0], 0) Else Exit -1 EndIf EndIf Return $handle[0] EndFunc ;==>_SingletonEx Func usagemsg() MsgBox(0x0, $t, "WinTextToPDF command-line parameters" & @CRLF & "(All parameters are optional)" & _ $blank & "1st: " & @CRLF & _ "2nd: | /view | /print | /select | /silent | /batch" & @CRLF & _ "3rd, etc: /view | /print | /select | /silent | /batch" & $blank & _ "/view = open PDF file in PDF viewer" & @CRLF & _ "/print = print PDF file to default printer" & @CRLF & _ "/select = select printer and print PDF file" & @CRLF & _ "/desktop = write PDF file to Windows desktop" & @CRLF & _ "/printername:" & $qt & "" & $qt & " print to named printer" & @CRLF & _ "/options:" & $qt & "-L etc." & $qt & " = options (see web page)" & @CRLF & _ "/batch = no prompt for errors or overwrite" & $blank & _ "also: /namefile:D:\PathTo\FileWithName" & @CRLF & _ "where FileWithName is a one-line text file containing" & @CRLF & _ "the full path (in quotes if needed) for the output PDF;" & @CRLF & _ "the FileWithName text file is deleted after use.") Exit EndFunc ;==>usagemsg