AppleScript: PDF Formularfelder befüllen

Folgendes kleines AppleScript wurde inspiriert durch ein PHP Script.

Ein mit Formularfeldern pröpariertes PDF wird als Vorlage benutzt. Die Formularfelder die als Wert bzw. Standardtext einen Platzhalter enthalten werden durch suchen/ersetzen mit Werten befüllt.

Dieses PDF kann einfach mit den Formularfunktionen von OpenOffice erstellt werden. Hier ist die OO Beispieldatei zu finden.

Einfach und genial. Ein Ansatz mit Potenzial ;-)

(*
pdf_form_fill.applescript

This applescript was inspired by the php script pdf_form_fill from @author Lars Wegmann info@wegmann-it.de

The script replaces the values of the form textfield in a pdf file. 

For example:
Make a new OpenOffice Writer document, insert some form textfields. 
The value of the fields represents the placeholders that would be 
replaced later with applescript.

Notice:
The placeholder it self must have the lenght of the maxlength of the form textfield. If the
textfield has 40 characters the placeholder must have also 40 characters.
The replace string can't be longer than the search string (the placeholder).
*)

--search/replace array
set arr to {{"###FELD1###", "X1"}, {"###FELD2###", "X2"}}

--open pdf file
set pdfdata to readFile("/Applications/MAMP/htdocs/test/PDF/pdf-test.pdf")

--process search/replace array
set pdfdata to processArray(pdfdata, arr)

--write pdf file
writeFile("/Applications/MAMP/htdocs/test/PDF/pdf-test-replaced.pdf", pdfdata)

on readFile(unixPath)
    set foo to (open for access (POSIX file unixPath))
    set txt to (read foo for (get eof foo))
    close access foo
    return txt
end readFile

on writeFile(unixPath, theData)
    set f to open for access unixPath with write permission
    write theData to f as string
    close access f
end writeFile

-- process array of search|replace values
on processArray(theData, arr)
    repeat with sr in arr
        --search replace ascii      
        if item 1 of sr is in theData then
            set theData to str_replace(item 1 of sr, str_pad(item 1 of sr, item 2 of sr), theData)
        end if
        --search replace hex
        if string2hex(item 1 of sr) is in theData then
            set theData to str_replace(string2hex(item 1 of sr), str_pad(string2hex(item 1 of sr), string2hex(item 2 of sr)), theData)
        end if
    end repeat
    return theData
end processArray

-- convert string to hex value
on string2hex(this_String)
    set hex to ""
    repeat with i from 1 to count of this_String
        set hex to hex & char2hex(character i of this_String)
    end repeat
    return hex as string
end string2hex

-- convert character to hex value
on char2hex(this_char)
    set the ASCII_num to (the ASCII number this_char)
    set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
    set x to item ((ASCII_num div 16) + 1) of the hex_list
    set y to item ((ASCII_num mod 16) + 1) of the hex_list
    return ("00" & x & y) as string
end char2hex

on str_pad(str1, str2)
    set c1 to count of str1
    set c2 to count of str2
    set pad to ""
    if c2 is less than c1 then
        repeat with i from 1 to c1 - c2
            set pad to pad & " "
        end repeat
    end if
    return str2 & pad
end str_pad

-- find : Text (or list of text) to be found
-- replace : Text (or list of text) to replace with
-- subject : Text (or list of text) to be searched
on str_replace(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set returnList to true

    -- This wouldn't make sense (you could have it raise an error instead)
    if class of find is not list and class of replace is list then return subject

    if class of find is not list then set find to {find}
    if class of subject is not list then ¬
        set {subject, returnList} to {{subject}, false}

    set findCount to count find
    set usingReplaceList to class of replace is list

    try
        repeat with i from 1 to (count subject)
            set thisSubject to item i of subject
            repeat with n from 1 to findCount
                set text item delimiters of AppleScript to item n of find
                set thisSubject to text items of thisSubject
                if usingReplaceList then
                    try
                        item n of replace
                    on error
                        "" -- `replace` ran out of items
                    end try
                else
                    replace
                end if

                set text item delimiters of AppleScript to result
                set thisSubject to "" & thisSubject
            end repeat

            set item i of subject to thisSubject
        end repeat
    end try

    set text item delimiters of AppleScript to prevTIDs
    if not returnList then return beginning of subject
    return subject
end str_replace