#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.2 text2pdf Version: 1.0 Author: Martin Wildam, with big thanks to Mihai Iancu See https://www.autoitscript.com/forum/topic/118827-create-pdf-from-your-application/ Usage: text2pdf.exe "" ".pdf" "COURIER|ARIAL|TIMES|CALIBRI|GARAMOND" "" [""] [<options>] ["<extraheader>"] <options> ::= <option>{,<option>} ... Comma separated list of option keywords where <option> can be: <option> ::= nodate|nopagenr|noheader|noheading|leftheading|repeatfirstline|landscape|openfile|debug|debugfine|debugfiner|debugfinest Option debug is equivalent with debugfiner. Known issue: When using repeatfirstline option together with some font sizes (e.g. 10 or 12) you may experience unregular line spaces. Change font size a little in these cases. Use font size 11 or 13 instead for example. #ce ---------------------------------------------------------------------------- #AutoIt3Wrapper_Change2CUI=y #pragma compile(FileVersion, 1.0.0.0) #pragma compile(LegalCopyright, 'Martin Wildam') #pragma compile(CompanyName, '') #pragma compile(Console, true) #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include <File.au3> #include <Date.au3> #include <WinAPIFiles.au3> #include <MPDF_UDF.au3> ; Read commandline parameters and prepare input variables Local $inputFile = "" Local $outputFile = "" Local $fontName = "COURIER" Local $fontSize = 11 Local $title = "" Global $debug = 0 Global $currentDate = _Now() Global $dateFooter = 1 Global $pageNumbers = 1 Global $writeHeader = 1 Global $writeHeading = 1 Global $leftHeading = 0 Global $orientation = "PORTRAIT" Global $repeatFirstLine = 0 Global $extraHeader = "" Global $openFile = 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 (StringInStr($CmdLine[6], "debug", 0) > 0) Then $debug = 2 If (StringInStr($CmdLine[6], "debugfine", 0) > 0) Then $debug = 1 If (StringInStr($CmdLine[6], "debugfiner", 0) > 0) Then $debug = 2 If (StringInStr($CmdLine[6], "debugfinest", 0) > 0) Then $debug = 3 If (StringInStr($CmdLine[6], "nodate", 0) > 0) Then $dateFooter = 0 If (StringInStr($CmdLine[6], "nopagenr", 0) > 0) Then $pageNumbers = 0 If (StringInStr($CmdLine[6], "noheader", 0) > 0) Then $writeHeader = 0 If (StringInStr($CmdLine[6], "noheading", 0) > 0) Then $writeHeading = 0 If (StringInStr($CmdLine[6], "leftheading", 0) > 0) Then $leftHeading = 1 If (StringInStr($CmdLine[6], "landscape", 0) > 0) Then $orientation = "LANDSCAPE" If (StringInStr($CmdLine[6], "repeatfirstline", 0) > 0) Then $repeatFirstLine = 1 If (StringInStr($CmdLine[6], "openfile", 0) > 0) Then $openFile = 1 EndIf If ($CmdLine[0] >= 7) Then $extraHeader = $CmdLine[7] If $debug >= 2 Then ConsoleWrite($CmdLine[0] & " parameters given." & @CRLF) Local $text = "" If ($inputFile <> "") Then text2PDFNice($inputFile, $outputFile, $title, $fontName, $fontSize) Else ConsoleWrite("text2PDF v1.0.0, 2019-02-28 by Martin Wildam" & @CRLF & "Usage:" & @CRLF) ConsoleWrite("text2pdf.exe ""<textfile>"" ""<outputfile>.pdf"" ""COURIER|ARIAL|TIMES|CALIBRI|GARAMOND"" ""<fontsize>"" [""<title>""] [<options>] [""<extraheader>""]" & @CRLF) ConsoleWrite(@CRLF) ConsoleWrite("<options> ::= <option>{,<option>} ... Comma separated list of option keywords where <option> can be:" & @CRLF) ConsoleWrite("<option> ::= nodate|nopagenr|noheader|noheading|leftheading|repeatfirstline|landscape|openfile|debug|debugfine|debugfiner|debugfinest" & @CRLF) ConsoleWrite("Option debug is equivalent with debugfiner." & @CRLF) ConsoleWrite("Known issue: When using repeatfirstline option together with some font sizes (e.g. 10 or 12) you may experience unregular line spaces." & @CRLF) ConsoleWrite("Change font size a little in these cases. Use font size 11 or 13 instead for example." & @CRLF) 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 ($CmdLine[0] < 5) Then If ($title == "") Then $title = $fileName EndIf If $debug >= 2 Then ConsoleWrite("Convert request for '" & $inputFile & "' to output '" & $outputFile & "' with title '" & $title & "' using font '" & $fontName & "' in size " & $fontSize & "." & @CRLF) _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 ConsoleWrite("========================================" & @CRLF) ConsoleWrite("inputFile=" & $inputFile & @CRLF & "outputFile=" & $outputFile & @CRLF & "title=" & $title & @CRLF & "fontName=" & $fontName & @CRLF & "fontSize=" & $fontSize & @CRLF) ConsoleWrite("Current date: " & $currentDate & @CRLF) ConsoleWrite("Processing..." & @CRLF) ConsoleWrite("----------------------------------------" & @CRLF) EndIf ;initialize the pdf _SetTitle($title) _SetSubject("Converted by text2pdf") _SetKeywords("pdf, AutoIt") If ($debug >= 1) Or ($openFile == 1) Then _OpenAfter(True) ;open after generation Else _OpenAfter(False) EndIf _SetUnit($PDF_UNIT_CM) _SetPaperSize("A4") _SetZoomMode($PDF_ZOOM_CUSTOM, 100) If ($orientation == "LANDSCAPE") Then _SetOrientation($PDF_ORIENTATION_LANDSCAPE) Else _SetOrientation($PDF_ORIENTATION_PORTRAIT) EndIf _SetLayoutMode($PDF_LAYOUT_CONTINOUS) _InitPDF($outputFile) Local $fontAlias = "F1" Local $fontTitleAlias = "F2" $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) _LoadFontTT($fontTitleAlias, $font, $PDF_FONT_BOLD) Local $hFile = FileOpen($inputFile) Local $sText = FileRead($hFile) FileClose($hFile) Local $bigSize = $fontSize + 5 Local $smallSize = $fontSize - 2 If ($fontSize < 12) Then $smallSize = $fontSize If ($fontSize >= 20) Then $smallSize = 20 ;Local $iUnit = Ceiling(_GetUnit()) Local $iUnit = _GetUnit() Local $iX = 2 ;Local $iY = Ceiling(_GetPageHeight() / _GetUnit()) - 2.5 Local $iY = (_GetPageHeight() / $iUnit) - 2.5 ;Local $iPagina = Ceiling(_GetPageWidth() / $iUnit) - $iX Local $iPagina = (_GetPageWidth() / $iUnit) - $iX Local $iWidth = Ceiling($iPagina - $iX) - 1.5 ;, 1) Local $lScale Local $iRanduri = StringSplit($sText & @CRLF, @CRLF & @CR & @LF, 2) ; We want to support also text files coming from a linux system Local $iHR = 0.75 * ($fontSize / $iUnit) Local $iHRBig = 0.75 * ($bigSize / $iUnit) ;If (($writeHeader == 0) OR ($title == "")) And ($extraHeader == "") Then $iY = $iY + $iHR Local $lowerBorder = 2.5 If ($dateFooter == 0) And ($pageNumbers == 0) Then $lowerBorder = $lowerBorder - 1 If $debug >= 2 Then ConsoleWrite(@CRLF & "Calculated arguments:" & @CRLF & "iY=" & $iY & @CRLF & "iUnit=" & $iUnit & @CRLF & "iHR=" & $iHR & @CRLF & @CRLF) Local $lineCount = UBound($iRanduri) Local $iPages = Ceiling((UBound($iRanduri)) * $iHR / $iY) Local $iNrRanduri = Ceiling(UBound($iRanduri) / $iPages-2) if $debug >= 1 Then ConsoleWrite("Input text analysis: " & $lineCount & " lines of text, " & $iPages & " pages (estimated)" & @CRLF) Local $firstLine = "" Local $nrp = 1 Local $i = 0 Local $isFinished = 0 Local $iH = $iY Local $insertPageBreak = 0 Local $sLength = 0 Local $textLine = "" While $i < $lineCount If $i == 0 Then $insertPageBreak = 1 ;In case of very first line we want a new page If $insertPageBreak == 1 Then ;Create a new page If $i > 0 Then _EndPage() ;If we are not at the first line then we are already writing on some page so we need to end that page prior to create a new one. $nrp = _BeginPage() If $debug >= 2 Then ConsoleWrite("========== Page " & $nrp & "..." & @CRLF) $iH = $iY ;PDF page starts at vertical page height and not at 0 so to proceed to the next line (see below) we need to substract vertical text height. ;Write header at fixed vertical location ( page height + 1) If ($writeHeader == 1) And ($title <> "") Then _DrawText($iX, $iY+1, $title, $fontAlias, $smallSize, $PDF_ALIGN_LEFT) EndIf ;If an additional info text (extraheader) was provided on commandline then we write this to the upper right corner of each page If ($extraHeader <> "") Then _DrawText((_GetPageWidth()/_GetUnit())-2, $iY+1, $extraHeader, $fontAlias, $smallSize, $PDF_ALIGN_RIGHT) EndIf ;Write footers at fixed vertical location ( 1 ) If ($dateFooter == 1) Then _DrawText($iX, 1, $currentDate, $fontAlias, $smallSize, $PDF_ALIGN_LEFT, 0) If ($pageNumbers == 1) Then _DrawText((_GetPageWidth()/$iUnit)-2, 1, $nrp, $fontAlias, $smallSize, $PDF_ALIGN_RIGHT) ;Write title line (with larger font size) If ($i == 0) And ($title <> "") And ($writeHeading == 1) Then $iH = $iH - $iHR ; For title (using larger font size) we need to substract more from current vertical position. $sLength = Round(_GetTextLength($title, $fontTitleAlias, $bigSize)) If ($sLength > ($iWidth - 1)) Then $lScale = Ceiling($iWidth * 100 / $sLength) _SetTextHorizontalScaling($lScale) _DrawText((_GetPageWidth() / 2) / $iUnit, $iH, $title, $fontTitleAlias, $bigSize, $PDF_ALIGN_CENTER, 0) _SetTextHorizontalScaling(100) Else If ($leftHeading == 1) Then _DrawText($iX, $iH, $title, $fontTitleAlias, $bigSize, $PDF_ALIGN_LEFT, 0) Else _DrawText((_GetPageWidth() / 2) / $iUnit, $iH, $title, $fontTitleAlias, $bigSize, $PDF_ALIGN_CENTER, 0) EndIf EndIf $iH = $iH - $iHR ; Create an additional space after big title. EndIf ;Special handling of first text line If ($repeatFirstLine == 1) Then If ($i == 0) Then $firstLine = $iRanduri[0] Else ;Again write the very first text line on top of new page $iH = $iH - $iHR ; We need to first correct the position because the text is written from lower to higher value (from down to up) If $debug >= 3 Then ConsoleWrite("### First line " & $i & " @ " & $iH & ":" & @CRLF & $firstLine & @CRLF) $sLength = Round(_GetTextLength($firstLine, $fontAlias, $fontSize)) If ($sLength > ($iWidth - 1)) Then $lScale = Ceiling($iWidth * 100 / $sLength) _SetTextHorizontalScaling($lScale) _DrawText($iX, $iH, $firstLine, $fontAlias, $fontSize, $PDF_ALIGN_LEFT, 0) _SetTextHorizontalScaling(100) Else _DrawText($iX, $iH, $firstLine, $fontAlias, $fontSize, $PDF_ALIGN_LEFT, 0) EndIf EndIf EndIf $insertPageBreak = 0 EndIf ;insert page break ;Normal line handling $textLine = $iRanduri[$i] $iH = $iH - $iHR ; We need to first correct the position because the text is written from lower to higher value (from down to up) If $debug >= 3 Then ConsoleWrite(">>> Line " & $i & " @ " & $iH & ":" & @CRLF & $textLine & @CRLF) $sLength = Round(_GetTextLength($textLine, $fontAlias, $fontSize)) If ($sLength > ($iWidth - 1)) Then $lScale = Ceiling($iWidth * 100 / $sLength) _SetTextHorizontalScaling($lScale) _DrawText($iX, $iH, $textLine, $fontAlias, $fontSize, $PDF_ALIGN_LEFT, 0) _SetTextHorizontalScaling(100) Else _DrawText($iX, $iH, $textLine, $fontAlias, $fontSize, $PDF_ALIGN_LEFT, 0) EndIf If $iH < $lowerBorder Then $insertPageBreak = 1 ; 2.5 is our border for normal text (footers are written at 1). $i = $i + 1 Wend _EndPage() ;write the buffer to disk ;Very strange - the _ClosePDFFile() may not be called within _Txt2PDFEx - then it does not work _ClosePDFFile() If $debug >= 1 Then ConsoleWrite("Finished " & $outputFile & @CRLF) If $debug >= 1 Then ConsoleWrite("========================================" & @CRLF) EndFunc ;==>_Txt2PDFEx