#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.2 text2pdf Version: 0.5 Author: Martin Wildam, with big thanks to Mihai Iancu Script Function: Usage: text2pdf.exe "" ".pdf" "COURIER|ARIAL|TIMES|CALIBRI|GARAMOND" "" ["title"] [debug] #ce ---------------------------------------------------------------------------- #pragma compile(FileVersion, 0.0.5.0) #pragma compile(LegalCopyright, 'Martin Wildam') #pragma compile(CompanyName, 'Optimal-Systems') #pragma compile(Console, false) #include #include #include #include #include ; Read commandline parameters and prepare input variables Local $inputFile = "" Local $outputFile = "" Local $fontName = "COURIER" Local $fontSize = 11 Local $title = "" Global $debug = 0 If ($CmdLine[0] >= 2) Then ; check number of commandline parameters $inputFile = $CmdLine[1] $outputFile = $CmdLine[2] EndIf If ($CmdLine[0] >= 3) Then $fontName = $CmdLine[3] EndIf If ($CmdLine[0] >= 4) Then $fontSize = Int($CmdLine[4]) EndIf If ($CmdLine[0] >= 5) Then $title = $CmdLine[5] EndIf If ($CmdLine[0] >= 6) Then If ($CmdLine[6] == "debug") Then $debug = 1 EndIf Local $text = "" If ($inputFile <> "") Then ;text2PDFSimple($inputFile, $outputFile, $title) text2PDFNice($inputFile, $outputFile, $title, $fontName, $fontSize) EndIf Func text2PDFNice($inputFile, $outputFile, $title, $fontName, $fontSize) Local $drive = "" Local $path = "" Local $fileName = "" Local $extension = "" Local $nameParts = _PathSplit($outputFile, $drive, $path, $fileName, $extension) If ($title == "") Then $title = $fileName _Txt2PDFEx($inputFile, $outputFile, $title, $fontName, Int($fontSize)) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Txt2PDFEx ; Description ...: Convert a text file to pdf ; Syntax ........: _Txt2PDFEx( $inputFile, $outputFile, $title, $fontName, $fontSize ) ; Parameters ....: $inputFile - Input file path. ; $outputFile - Output file path. ; $title - Title for PDF property and headline. ; $fontName - Text font name (one of COURIER, ARIAL, TIMES, CALIBRI or GARAMOND), COURIER is the default. ; $fontSize - Font size (default is 11). ; Return values .: None ; Author(s) .....: Mihai Iancu (taietel at yahoo dot com) and Martin Wildam (mwildam at gmail dot com) ; Modified ......: ; Remarks .......: If the string is very long, it will be scaled to paper width ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _Txt2PDFEx($inputFile, $outputFile, $title, $fontName, $fontSize) If $fontName == "" Then $fontName = "COURIER" If $fontSize == 0 Then $fontSize = 11 ;MsgBox(0, "Test", "inputFile=" & $inputFile & @CRLF & "outputFile=" & $outputFile & @CRLF & "title=" & $title & @CRLF & "fontName=" & $fontName & @CRLF & "fontSize=" & $fontSize & @CRLF & "fontAlias=" & $fontAlias & @CRLF) If $debug == 1 Then MsgBox(0, "Test", "inputFile=" & $inputFile & @CRLF & "outputFile=" & $outputFile & @CRLF & "title=" & $title & @CRLF & "fontName=" & $fontName & @CRLF & "fontSize=" & $fontSize & @CRLF) ;initialize the pdf _SetTitle($title) _SetSubject("Converted by text2pdf") _SetKeywords("pdf, AutoIt") If $debug == 1 Then _OpenAfter(True);open after generation Else _OpenAfter(False) EndIf _SetUnit($PDF_UNIT_CM) _SetPaperSize("A4") _SetZoomMode($PDF_ZOOM_CUSTOM, 100) _SetOrientation($PDF_ORIENTATION_PORTRAIT) _SetLayoutMode($PDF_LAYOUT_CONTINOUS) _InitPDF($outputFile) Local $fontAlias = "F1" $fontName = StringUpper($fontName) Switch $fontName Case "COURIER" $font = $PDF_FONT_COURIER Case "ARIAL", "HELVETICA" $font = $PDF_FONT_ARIAL Case "TIMES", "ROMAN", "TIMESNEWROMAN", "NEWTIMES", "TIMESNEW" $font = $PDF_FONT_TIMES Case "CALIBRI" $font = $PDF_FONT_CALIBRI Case "GARAMOND" $font = $PDF_FONT_GARAMOND Case Else $font = $PDF_FONT_COURIER EndSwitch _LoadFontTT($fontAlias, $font, $PDF_FONT_NORMAL) Local $hFile = FileOpen($inputFile) Local $sText = FileRead($hFile) FileClose($hFile) Local $iUnit = Ceiling(_GetUnit()) Local $iX = 2 Local $iY = Ceiling(_GetPageHeight() / _GetUnit()) - 1.5 Local $iPagina = Ceiling(_GetPageWidth() / $iUnit) - $iX Local $iWidth = Ceiling($iPagina - $iX);, 1) Local $lScale ;Local $iRanduri = StringSplit($sText & @CRLF & @CRLF & @CRLF & @CRLF, @CRLF, 3) ; Original from Mihai Local $iRanduri = StringSplit($sText & @CRLF, @CRLF & @CR & @LF, 2) ; We want to support also text files coming from a linux system ;Local $factor = $iY / ($fontSize * $iUnit) ; Old HR calculation ;Local $iHR = 0.5 * Ceiling($factor) ; Old HR calculation Local $factor = $fontSize / $iUnit Local $iHR = 0.75 * $factor If $debug == 1 Then MsgBox(0, "Test", "iY=" & $iY & @CRLF & "iUnit=" & $iUnit & @CRLF & "factor=" & $factor & @CRLF & "iHR=" & $iHR & @CRLF) Local $iPages = Ceiling((UBound($iRanduri)) * $iHR / $iY) Local $iNrRanduri = Ceiling(UBound($iRanduri) / $iPages-2) Local $nrp Local $inPage = 0 Local $isFinished = 0 Local $iH = $iY For $j = 0 To $iPages + 2 $nrp = _BeginPage() $inPage = 1 $iH = $iY _DrawText(_GetPageWidth()/_GetUnit()-1, 1, $nrp, $fontAlias, $fontSize, $PDF_ALIGN_CENTER) For $i = 0 To $iNrRanduri - 1 Local $sLength = Round(_GetTextLength($iRanduri[$i + $j * $iNrRanduri], $fontAlias, $fontSize)) $iH = $iH - $iHR Select Case $iH < 1 _EndPage() $inPage = 0 Case $i + $j * $iNrRanduri = UBound($iRanduri) - 1 _EndPage() $inPage = 0 $isFinished = 1 ExitLoop Case $sLength > $iWidth - 1 $lScale = Ceiling($iWidth * 100 / $sLength) _SetTextHorizontalScaling($lScale) _DrawText($iX, $iH, $iRanduri[$i + $j * $iNrRanduri], $fontAlias, $fontSize, $PDF_ALIGN_LEFT, 0) _SetTextHorizontalScaling(100) Case Else _DrawText($iX, $iH, $iRanduri[$i + $j * $iNrRanduri], $fontAlias, $fontSize, $PDF_ALIGN_LEFT, 0) EndSelect Next If $inPage == 1 Then _EndPage() If $isFinished == 1 Then ExitLoop Next If $debug == 1 Then MsgBox(0, "Test", "Finished") ;write the buffer to disk ;Very strange - the _ClosePDFFile() may not be called within _Txt2PDFEx - then it does not work _ClosePDFFile() EndFunc ;==>_Txt2PDF