Jump to content
BrianTheLibrarian

Creating multiple pdfs fails after first pdf

Recommended Posts

BrianTheLibrarian

Hello, I am trying to create multiple PDFs using the following code.  It makes the first pdf fine, but all subsequent pdfs (while they have the correct number of pages) are blank pages and give an error in acrobat that says "There is an error processing a page. There was a problem reading this document (18)"

 

#include <MPDF_UDF.au3>
#include <String.au3>
#include <Array.au3>
#include <GDIPlus.au3>

Global $iX
Global $iY

_SetTitle("Image2PDF")
_SetSubject("Convert image(s) to pdf")
_SetKeywords("pdf, AutoIt")
_OpenAfter(False);open after generation
_SetUnit($PDF_UNIT_CM)
_SetPaperSize("CUSTOM", 595.276, 595.276); 8.5x8.5
_SetOrientation($PDF_ORIENTATION_PORTRAIT)
_SetZoomMode($PDF_ZOOM_CUSTOM,90)
_SetLayoutMode($PDF_LAYOUT_CONTINOUS)


$folder = "E:\Google Drive\Indesign PNG Export watermark\"
global $name[4][1] = [["Bundle_1"],["Bundle_2"],["Bundle_3"],["Bundle_4"]]
global $images[4][24] = [["1099","1100","1101","1006","1007","1008","1014","1015","1016","935","950","951","1090","1091","1092","1059","1060","1061","977","978","979","","",""],["1108","1109","1110","1122","1123","1124","1134","1135","1136","1148","1150","1149","1162","1163","1164","","","","","","","","",""],["770","771","772","807","808","809","835","848","849","813","814","815","","","","","","","","","","","",""],["2231","2243","2232","2218","2219","2220","2248","2250","2251","1808","1809","1816","1888","1889","1890","2048","2049","2050","","","","","",""]]
global $aImgs[0]
global $w
$x = UBound($images)
$nameLen = UBound($name)
$y = 0
$w = 0


while $w < $nameLen
    global $aImgs[0]
    Global $_Pages = 0 ;
    _ArrayAdd ($aImgs, $folder)

    while $y < $x
        If $images[$w][$y] = "" Then
            ExitLoop

        Else
            _ArrayAdd ($aImgs, "preview" & $images[$w][$y]&".png")
            $y = $y+1
        EndIf
    WEnd

    ;_ArrayDisplay($aImgs)

    _SelectImages()
    $y = 0
    $w = $w+1
WEnd

Exit

Func _SelectImages()

        ;initialize the pdf
        _InitPDF(@ScriptDir & "\"& $name[$w][0] &".pdf")

        If UBound($aImgs)<>1 Then
            ;=== load resources used in pdf ===
            For $i=1 To UBound($aImgs)-1
                _LoadResImage("img"&$i, $aImgs[0] & "\" & $aImgs[$i])
            Next

            For $i = 1 To UBound($aImgs)-1

                $z = $i-1
                _getImageSize ($folder&"\preview"&$images[$w][$z]&".png")

                if ($iY > $iX) Then
                    $imgH = 17
                    $imgW = 21
                    $xCord = 2
                    $yCord = .5

                EndIf

                if ($iY < $iX) Then
                    $imgH = 21
                    $imgW = 17
                    $xCord = 0
                    $yCord = 1.3

                EndIf

                if ($iX = $iY ) Then
                    $imgH = 21
                    $imgW = 21
                    $xCord = 0
                    $yCord = 0

                EndIf


                _BeginPage()
                    _InsertImage("img"&$i, $xCord, $yCord ,$imgH, $imgW)
                _EndPage()
            Next
        EndIf
        ;then, finally, write the buffer to disk
        _ClosePDFFile()
        $_Buffer = ""
EndFunc


Func _getImageSize($sImageName)
    _GDIPlus_Startup()
    $hImage = _GDIPlus_ImageLoadFromFile($sImageName)
    $iX = _GDIPlus_ImageGetWidth($hImage)
    $iY = _GDIPlus_ImageGetHeight($hImage)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc

 

Share this post


Link to post
Share on other sites
Subz

You will need add $Pages = 0 to get it to restart the page count for example:

#include <MPDF_UDF.au3>
#include <String.au3>
#include <Array.au3>
#include <GDIPlus.au3>
#include <ScreenCapture.au3>

Global $_iImgWidth
Global $_iImgHeight

Global $sImagePath = "E:\Google Drive\Indesign PNG Export Watermark"
Global $aPDFNames[4] = ["Bundle_1", "Bundle_2", "Bundle_3", "Bundle_4"]
Global $aPDFImages[4][24] = [["1099","1100","1101","1006","1007","1008","1014","1015","1016","935","950","951","1090","1091","1092","1059","1060","1061","977","978","979","","",""],["1108","1109","1110","1122","1123","1124","1134","1135","1136","1148","1150","1149","1162","1163","1164","","","","","","","","",""],["770","771","772","807","808","809","835","848","849","813","814","815","","","","","","","","","","","",""],["2231","2243","2232","2218","2219","2220","2248","2250","2251","1808","1809","1816","1888","1889","1890","2048","2049","2050","","","","","",""]]

For $i = 0 To UBound($aPDFNames) - 1
    $_Buffer = "" ;~ Reset Buffer
    $_Images = "" ;~ Reset Resource Images
    $_Pages = 0 ;~ Reset Pages to 0
    _SetTitle("Image2PDF")
    _SetSubject("Convert image(s) to pdf")
    _SetKeywords("pdf, AutoIt")
    _OpenAfter(False);open after generation
    _SetUnit($PDF_UNIT_CM)
    _SetPaperSize("CUSTOM", 595.276, 595.276); 8.5x8.5
    _SetZoomMode($PDF_ZOOM_CUSTOM,90)
    _SetOrientation($PDF_ORIENTATION_PORTRAIT)
    _SetLayoutMode($PDF_LAYOUT_CONTINOUS)
    ConsoleWrite("Compiling - " & $aPDFNames[$i] & @CRLF)
    ;~ Initialize the PDF
    _InitPDF(@ScriptDir & "\"& $aPDFNames[$i] & ".pdf")
    _SelectImages($i)
    ;~ then, finally, write the buffer to disk
    _ClosePDFFile()
Next

Func _SelectImages($_iPDFImageIndex)
    Local $aImgSize, $iImgHeight, $iImgWidth
    ;~ === Load resources used in pdf ===
    For $j = 0 To UBound($aPDFImages, 2) - 1
        ConsoleWrite("Adding - img" & $j & ": " & $sImagePath & "\preview" & $aPDFImages[$_iPDFImageIndex][$j] & ".png" & @CRLF)
        _LoadResImage("img" & $j, $sImagePath & "\preview" & $aPDFImages[$_iPDFImageIndex][$j] & ".png")
        $aImgSize = _GetImageSize($sImagePath & "\preview" & $aPDFImages[$_iPDFImageIndex][$j] & ".png")
        Select
            Case $aImgSize[0] > $aImgSize[1]
                $iImgWidth = 17
                $iImgHeight = 21
                $xImgCoord = 2
                $yImgCoord = .5
            Case $aImgSize[0] < $aImgSize[1]
                $iImgWidth = 21
                $iImgHeight = 17
                $xImgCoord = 0
                $yImgCoord = 1.3
            Case $aImgSize[0] = $aImgSize[1]
                $iImgHeight = 21
                $iImgWidth = 21
                $xImgCoord = 0
                $yImgCoord = 0
        EndSelect
        _BeginPage()
            _InsertImage("img"& $j, $xImgCoord, $yImgCoord ,$iImgWidth, $iImgHeight)
        _EndPage()
    Next
EndFunc

Func _GetImageSize($sImageName)
    Local $_aImgSize[2]
    _GDIPlus_Startup()
    $hImage = _GDIPlus_ImageLoadFromFile($sImageName)
    $_aImgSize[0] = _GDIPlus_ImageGetWidth($hImage)
    $_aImgSize[1] = _GDIPlus_ImageGetHeight($hImage)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
    Return $_aImgSize
EndFunc

 

  • Thanks 1

Share this post


Link to post
Share on other sites
BrianTheLibrarian

Wow, thank you! I really appreciate your help and thanks for cleaning up the code a bit - I know I still have a ways to go on that.

Share this post


Link to post
Share on other sites
taurus905

Hello Subz,

I tried your example script above and received the following error:

---------------------------
AutoIt Error
---------------------------
Line 250  (File "C:\Program Files (x86)\AutoIt3\Include\MPDF_UDF.au3"):

__ToBuffer("<<" & _Iif($_sFONT <> "", "/Font<<" & $_sFONT & ">>", "") & "/ProcSet [/PDF/Text" & _Iif($_Image <> "", "/ImageB/ImageC/ImageI", "") & "]" & _Iif(($_Image <> "") Or ($_sObject <> ""), "/XObject <<" & $_Image & $_sObject & ">>", "") & ">>")
__ToBuffer("<<" & ^ ERROR

Error: Unknown function name.
---------------------------

Maybe I have an old version of "MPDF_UDF.au3" (MPDFv103)

Do you know where the current version is?

Thank you.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites
Subz

Hi taurus

Had the same issue, it appears the _lif function which was a part of an older AutoIt version was removed, found from some posts that you just have to  add the following to the end of your MPDF .au3 file.

Cheers

Func _Iif($vExpression, $vTrue, $vFalse)
    Return $vExpression ? $vTrue : $vFalse
EndFunc   ;==>_Iif

 

Share this post


Link to post
Share on other sites
taurus905

Excellent, Subz.

Thank you for the quick and accurate response.

taurus905


"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

Share this post


Link to post
Share on other sites

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

×