Jump to content

Create PDF from your application


taietel
 Share

Recommended Posts

Here is a reproducer with some 3 files generated one after each other:

  • has 4 pages
  • has 3 pages
  • has 4 pages
#include "MPDF_UDF.au3"
MsgBox(1, "test", "Hit ok to run the first report (4 pages)")
FirstPDF()

MsgBox(1, "test", "Hit ok to run the second report (3 pages)")
SecondPDF()

MsgBox(1, "test", "Hit ok to run the third report (4 pages)")


ThirdPDF()

Func FirstPDF()
    ;set the properties for the pdf
    _SetTitle("Demo Table PDF in AutoIt")
    _SetSubject("Demo Table PDF in AutoIt, with formating")
    _SetKeywords("pdf, demo, table, AutoIt")
    _OpenAfter(True);open after generation
    _SetUnit($PDF_UNIT_CM)
    _SetPaperSize("CUSTOM", 841.890, 595.276); A4 landscape
    _SetZoomMode($PDF_ZOOM_FULLPAGE)
    _SetOrientation($PDF_ORIENTATION_PORTRAIT)
    _SetLayoutMode($PDF_LAYOUT_CONTINOUS)

    ;initialize the pdf
    _InitPDF(@ScriptDir & "\table.pdf")

    ;=== load used font(s) ===
    ;fonts: Garamond
    _LoadFontTT("_CalibriB", $PDF_FONT_CALIBRI, $PDF_FONT_BOLD)
    _LoadFontTT("_CalibriI", $PDF_FONT_CALIBRI, $PDF_FONT_ITALIC)
    _LoadFontTT("_Calibri", $PDF_FONT_CALIBRI)

    ;begin page
    _BeginPage()
    _InsertTable(2, 2, 0, 0, 10, 10)
    $sTit = "Sample pdf table generated with AutoIt"
    _SetTextRenderingMode(1)
    _InsertRenderedText((_GetPageWidth() / _GetUnit()) / 2, _GetPageHeight() / _GetUnit() - 1.5, $sTit, "_CalibriI", 32, 100, $PDF_ALIGN_CENTER, 0xbbbbbb, 0x202020)
    _SetTextRenderingMode(0)
    _EndPage()
    _BeginPage()
    _InsertTable(2, 8, 0, 0, 8, 5)
    $sTit = "Sample pdf table generated with AutoIt"
    _SetTextRenderingMode(2)
    _InsertRenderedText((_GetPageWidth() / _GetUnit()) / 2, _GetPageHeight() / _GetUnit() - 1.5, $sTit, "_CalibriB", 32, 100, $PDF_ALIGN_CENTER, 0xbbbbbb, 0x202020)
    _SetTextRenderingMode(0)
    _EndPage()
    _BeginPage()
    _InsertTable(2, 8, 20, 0, 8, 5)
    $sTit = "Sample pdf table generated with AutoIt"
    _SetTextRenderingMode(3)
    _InsertRenderedText((_GetPageWidth() / _GetUnit()) / 2, _GetPageHeight() / _GetUnit() - 1.5, $sTit, "_CalibriI", 32, 100, $PDF_ALIGN_CENTER, 0xbbbbbb, 0x202020)
    _SetTextRenderingMode(0)
    _EndPage()
    _BeginPage()
    _InsertTable(8, 2, 0, 15, 8, 15)
    $sTit = "Sample pdf table generated with AutoIt"
    _SetTextRenderingMode(5)
    _InsertRenderedText((_GetPageWidth() / _GetUnit()) / 2, _GetPageHeight() / _GetUnit() - 1.5, $sTit, "_CalibriB", 32, 100, $PDF_ALIGN_CENTER, 0xbbbbbb, 0x202020)
    _SetTextRenderingMode(0)
    _EndPage()

    _ClosePDFFile()
EndFunc   ;==>FirstPDF

Func SecondPDF()
    ;set the properties for the pdf
    _SetTitle("Demo Table PDF in AutoIt")
    _SetSubject("Demo Table PDF in AutoIt, with formating")
    _SetKeywords("pdf, demo, table, AutoIt")
    _OpenAfter(True);open after generation
    _SetUnit($PDF_UNIT_CM)
    _SetPaperSize("CUSTOM", 841.890, 595.276); A4 landscape
    _SetZoomMode($PDF_ZOOM_FULLPAGE)
    _SetOrientation($PDF_ORIENTATION_PORTRAIT)
    _SetLayoutMode($PDF_LAYOUT_CONTINOUS)

    ;initialize the pdf
    _InitPDF(@ScriptDir & "\table.pdf")

    ;=== load used font(s) ===
    ;fonts: Garamond
    _LoadFontTT("_CalibriB", $PDF_FONT_CALIBRI, $PDF_FONT_BOLD)
    _LoadFontTT("_CalibriI", $PDF_FONT_CALIBRI, $PDF_FONT_ITALIC)
    _LoadFontTT("_Calibri", $PDF_FONT_CALIBRI)

    ;begin page
    _BeginPage()
    _InsertTable(2, 2, 0, 0, 10, 10)
    $sTit = "Sample pdf table generated with AutoIt"
    _SetTextRenderingMode(1)
    _InsertRenderedText((_GetPageWidth() / _GetUnit()) / 2, _GetPageHeight() / _GetUnit() - 1.5, $sTit, "_CalibriI", 32, 100, $PDF_ALIGN_CENTER, 0xbbbbbb, 0x202020)
    _SetTextRenderingMode(0)
    _EndPage()
    _BeginPage()
    _InsertTable(2, 8, 0, 0, 8, 5)
    $sTit = "Sample pdf table generated with AutoIt"
    _SetTextRenderingMode(2)
    _InsertRenderedText((_GetPageWidth() / _GetUnit()) / 2, _GetPageHeight() / _GetUnit() - 1.5, $sTit, "_CalibriB", 32, 100, $PDF_ALIGN_CENTER, 0xbbbbbb, 0x202020)
    _SetTextRenderingMode(0)
    _EndPage()
    _BeginPage()
    _InsertTable(2, 8, 20, 0, 8, 5)
    $sTit = "Sample pdf table generated with AutoIt"
    _SetTextRenderingMode(3)
    _InsertRenderedText((_GetPageWidth() / _GetUnit()) / 2, _GetPageHeight() / _GetUnit() - 1.5, $sTit, "_CalibriI", 32, 100, $PDF_ALIGN_CENTER, 0xbbbbbb, 0x202020)
    _SetTextRenderingMode(0)
    _EndPage()

    _ClosePDFFile()
EndFunc   ;==>SecondPDF

Func ThirdPDF()
    ;set the properties for the pdf
    _SetTitle("Demo Table PDF in AutoIt")
    _SetSubject("Demo Table PDF in AutoIt, with formating")
    _SetKeywords("pdf, demo, table, AutoIt")
    _OpenAfter(True);open after generation
    _SetUnit($PDF_UNIT_CM)
    _SetPaperSize("CUSTOM", 841.890, 595.276); A4 landscape
    _SetZoomMode($PDF_ZOOM_FULLPAGE)
    _SetOrientation($PDF_ORIENTATION_PORTRAIT)
    _SetLayoutMode($PDF_LAYOUT_CONTINOUS)

    ;initialize the pdf
    _InitPDF(@ScriptDir & "\table.pdf")

    ;=== load used font(s) ===
    ;fonts: Garamond
    _LoadFontTT("_CalibriB", $PDF_FONT_CALIBRI, $PDF_FONT_BOLD)
    _LoadFontTT("_CalibriI", $PDF_FONT_CALIBRI, $PDF_FONT_ITALIC)
    _LoadFontTT("_Calibri", $PDF_FONT_CALIBRI)

    ;begin page
    _BeginPage()
    _InsertTable(2, 2, 0, 0, 10, 10)
    $sTit = "Sample pdf table generated with AutoIt"
    _SetTextRenderingMode(1)
    _InsertRenderedText((_GetPageWidth() / _GetUnit()) / 2, _GetPageHeight() / _GetUnit() - 1.5, $sTit, "_CalibriI", 32, 100, $PDF_ALIGN_CENTER, 0xbbbbbb, 0x202020)
    _SetTextRenderingMode(0)
    _EndPage()
    _BeginPage()
    _InsertTable(2, 8, 0, 0, 8, 5)
    $sTit = "Sample pdf table generated with AutoIt"
    _SetTextRenderingMode(2)
    _InsertRenderedText((_GetPageWidth() / _GetUnit()) / 2, _GetPageHeight() / _GetUnit() - 1.5, $sTit, "_CalibriB", 32, 100, $PDF_ALIGN_CENTER, 0xbbbbbb, 0x202020)
    _SetTextRenderingMode(0)
    _EndPage()
    _BeginPage()
    _InsertTable(2, 8, 20, 0, 8, 5)
    $sTit = "Sample pdf table generated with AutoIt"
    _SetTextRenderingMode(3)
    _InsertRenderedText((_GetPageWidth() / _GetUnit()) / 2, _GetPageHeight() / _GetUnit() - 1.5, $sTit, "_CalibriI", 32, 100, $PDF_ALIGN_CENTER, 0xbbbbbb, 0x202020)
    _SetTextRenderingMode(0)
    _EndPage()
    _BeginPage()
    _InsertTable(8, 2, 0, 15, 8, 15)
    $sTit = "Sample pdf table generated with AutoIt"
    _SetTextRenderingMode(5)
    _InsertRenderedText((_GetPageWidth() / _GetUnit()) / 2, _GetPageHeight() / _GetUnit() - 1.5, $sTit, "_CalibriB", 32, 100, $PDF_ALIGN_CENTER, 0xbbbbbb, 0x202020)
    _SetTextRenderingMode(0)
    _EndPage()
    _ClosePDFFile()
EndFunc   ;==>ThirdPDF

Func _InsertTable($iX, $iY, $iW = 0, $iH = 0, $iCols = 0, $iRows = 0, $lTxtColor = 0x000000, $lBorderColor = 0xdddddd)
    Local $iPgW = Round(_GetPageWidth() / _GetUnit(), 1)
    Local $iPgH = Round(_GetPageHeight() / _GetUnit(), 1)
    If $iW = 0 Then $iW = $iPgW - $iX - 2
    If $iH = 0 Then $iH = $iPgH - $iY - 2
    _SetColourStroke($lBorderColor)
    _Draw_Rectangle($iX, $iY, $iW, $iH, $PDF_STYLE_STROKED, 0, 0xfefefe, 0.01)
    _SetColourStroke(0)
    Local $iColW = $iW / $iCols
    Local $iRowH = $iH / $iRows
    Local $lRGB
    For $i = 0 To $iRows - 1
        For $j = 0 To $iCols - 1
            If $i = 0 Then
                $lRGB = 0xefefef
            Else
                $lRGB = 0xfefefe
            EndIf
            _SetColourStroke($lBorderColor)
            _Draw_Rectangle($iX + $j * $iColW, $iY + $iH - ($i + 1) * $iRowH, $iColW, $iRowH, $PDF_STYLE_STROKED, 0, $lRGB, 0.01)
            _SetColourStroke(0)
            Local $sText = "Row " & $i & ": Col " & $j
            Local $sLength = Round(_GetTextLength($sText, "_Calibri", 10), 1)
            $lScale = Ceiling(0.75 * $iColW * 100 / $sLength)
            _SetColourFill($lTxtColor)
            _SetTextHorizontalScaling($lScale)
            _DrawText($iX + $j * $iColW + $iColW / 10, $iY + $iH - ($i + 1) * $iRowH + ($iRowH - 10 / _GetUnit()) / 2, $sText, "_Calibri", 10, $PDF_ALIGN_LEFT, 0)
            _SetTextHorizontalScaling(100)
            _SetColourFill(0)
        Next
    Next
EndFunc   ;==>_InsertTable

Check your script for $a variable. :mellow: This part is not related with the UDF.

Regards,

taietel

[EDIT] Just to be safe, modify the end of the _ClosePDFFile function to look like this (to clean ALL the resources):

Func _ClosePDFFile()

.........................

$_Pages = ""
    $_sPage = ""
    $_sFONT = ""
    $_Image = ""
    $_sObject = ""
    $_iResource = ""
    $_Buffer = ""
    If $_bOpen Then ShellExecute($PDF_NAME)
EndFunc   ;==>_ClosePDFFile
Edited by taietel
Link to comment
Share on other sites

Thanks, this seems to have worked.

Func _ClosePDFFile()

.........................

$_Pages = ""
    $_sPage = ""
    $_sFONT = ""
    $_Image = ""
    $_sObject = ""
    $_iResource = ""
    $_Buffer = ""
    If $_bOpen Then ShellExecute($PDF_NAME)
EndFunc   ;==>_ClosePDFFile

I am going to try to play with it a bit, and see if it still does this, but it looks like it is resolved.

BTW, $a is not part of the UDF, but it is part of the loop I was using. If you see, it goes through an array, and exports from the array to the PDF file. That is why I used a variable and a loop, because sometimes there could be 1 thing in the array, and the next report can have 20.

Link to comment
Share on other sites

I know $a is not part of the UDF.

If I were you, I'd put the table as an object, once, at the beginning of the pdf, with the flag set to appear on every page, and just fill the needed cells with the data from the array. This way the code is shorter, the pdf is smaller and it generates faster. The table example was just a simple demonstration of how to make one, but not practical for huge documents. You have to modify it to fit your needs.

Usually I generate the pdf in one shot, from several to hundreds of pages, that's why I didn't clear the buffer in the first place.

Link to comment
Share on other sites

  • 3 weeks later...
  • 3 weeks later...
  • 2 weeks later...

I friends

I modified the function _Txt2PDF to split a very long line into multiple lines.

<SPAN class="hps">
   Local $arrayParole = StringSplit($iTotalRows[$counter]," ")
    Local $riga = ""
    for $J = 0 to UBound($arrayParole)-1
      ; calcola lunghezza riga
      Local $iRowLengthParole = Round(_GetTextLength($riga & $arrayParole[$j] & " ", $sFontAlias, $iTextHeight))
      if $iRowLengthParole < $iWidth Then
       $riga &= $arrayParole[$j] & " "
      Else
       _DrawText($iX, $iY - ($i * $iRowHeight)-$ExtraHeight, $riga, $sFontAlias, $iTextHeight, $PDF_ALIGN_LEFT, 0)
       $riga = $arrayParole[$j] & " "
       $ExtraHeight += $iRowHeight
      EndIf
    Next
    _DrawText($iX, $iY - ($i * $iRowHeight)-$ExtraHeight, $riga, $sFontAlias, $iTextHeight, $PDF_ALIGN_LEFT, 0)
   Else
. . . . . . . . 
</SPAN>

you can justify text?

Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...

Thank you for this beautiful UDF. It is very simple to use. But I have a problem when I use the caracters "à" "è" ecc. with font Bold or Italic. The line in the pdf file is printed with not correct space between words. Sorry for english. And Thank you for the help.

:rolleyes:

Link to comment
Share on other sites

GREAT!!!

I appreciate very much rewriting of this UDF to not be dependent on any external DLL :D

Of course five stars from me.

I will use it definitely in my current project. Thanks.

Anyway I probably discovered small bug:

In Example_Mixed.au3 there is _DrawText() with $PDF_ALIGN_CENTER/$PDF_ALIGN_LEFT/$PDF_ALIGN_RIGHT

but in generated opened PDF there is align left/right swapped. So propably $PDF_ALIGN_LEFT/$PDF_ALIGN_RIGHT are swapped?

Also your example for Listview2PDF posted in this topic could be bundled in UDF's ZIP archive.

Link to comment
Share on other sites

@Virgilio1: For justified paragraph, try to use Garamond (I use this mostly). Regarding adding more fonts: of course you can add.

@Efo74: That's because the ANSI encoding of fonts in this UDF.

@Zedna: I'm glad you use it! About the aligning, I think it is OK:

#include "MPDF_UDF.au3"
_SetTitle("Demo PDF in AutoIt")
_SetSubject("Demo PDF in AutoIt, without any ActiveX or DLL...")
_SetKeywords("pdf, demo, AutoIt")
_OpenAfter(True);open after generation
_SetUnit($PDF_UNIT_CM)
_SetPaperSize("A4")
_SetZoomMode($PDF_ZOOM_CUSTOM,90)
_SetOrientation($PDF_ORIENTATION_PORTRAIT)
_SetLayoutMode($PDF_LAYOUT_CONTINOUS)
_InitPDF(@ScriptDir & "Example_aligned.pdf")
_LoadFontTT("_Garamond", $PDF_FONT_GARAMOND)
_BeginPage()
_DrawLine(3, 25, 3, 21, $PDF_STYLE_STROKED, 3, 0.05, 0x990000, 0, 0)
_SetColourFill(0x111111)
_DrawText(3, 24, "AutoIt left", "_Garamond", 22, $PDF_ALIGN_LEFT, 0)
_DrawText(3, 23, "AutoIt center", "_Garamond", 22, $PDF_ALIGN_CENTER, 0)
_DrawText(3, 22, "AutoIt right", "_Garamond", 22, $PDF_ALIGN_RIGHT, 0)
_SetColourFill(0)
_EndPage()
_ClosePDFFile()

Sorry for the delay. Now I have 4 jobs and no time.

Regards,

taietel

Link to comment
Share on other sites

Shouldn't align left have the text on the left side of the page and align right have it on the right side of the page? Having it align to the center of the page makes no sense.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Now I looked into sources to see what's going on there and there is really bug in DrawText()

original

Func _DrawText($iX, $iY, $sText, $sFontAlias, $iFontSize, $iAlign = $PDF_ALIGN_LEFT, $iRotate = 0)
...
    Switch $iAlign
        Case $PDF_ALIGN_LEFT
        Case $PDF_ALIGN_RIGHT
            $l = _GetTextLength($sText, $sFontAlias, $iFontSize)
            $iX -= $l
        Case $PDF_ALIGN_CENTER
            $l = _GetTextLength($sText, $sFontAlias, $iFontSize)
            $iX -= $l / 2
    EndSwitch
...

fixed

Func _DrawText($iX, $iY, $sText, $sFontAlias, $iFontSize, $iAlign = $PDF_ALIGN_LEFT, $iRotate = 0)
...
    Switch $iAlign
        Case $PDF_ALIGN_LEFT
        Case $PDF_ALIGN_RIGHT
            $l = _GetTextLength($sText, $sFontAlias, $iFontSize)
            $iX += $l
        Case $PDF_ALIGN_CENTER
            $l = _GetTextLength($sText, $sFontAlias, $iFontSize)
            $iX += $l / 2
    EndSwitch
...

$iX -= was incorrect there must be $iX +=

Here is screenshot of correct result after that fix

post-6483-0-72391800-1321539904_thumb.pn

Edited by Zedna
Link to comment
Share on other sites

Adition to my previous post:

Now as I more look at the align code/results I see original idea of author :-)

Alignment in original way means that X parameter is related to left/center/right edge of text

so with $PDF_ALIGN_RIGHT X means not left but right position of text!

But that way is uncommon in any other development tools/APIs as far as I know

It is common that X means always left position.

In sense of that enlightment I think it would be better

to add new optional parameter W (width)

and make alignment related to this X and X+W --> right corner of text

implicitly W=_GetTextLength($sText, $sFontAlias, $iFontSize)

Edited by Zedna
Link to comment
Share on other sites

@BrewManNH

Shouldn't align left have the text on the left side of the page and align right have it on the right side of the page?

That's exactly what the alignment in the UDF do, but relative to a imaginary border. If you begin to write a text 2 cm from the left, and choose left alignment, it will flow in the right direction. If you choose right alignment, but start in the same point (2 cm from the left), the text will flow on the left.

Having it align to the center of the page makes no sense.

:D

@Zedna

Alignment in original way means that X parameter is related to left/center/right edge of text

That was the idea.
Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...