Eskil

pdf.tcl at trunk
Login

File plugins/pdf.tcl artifact cf6c73175a on branch trunk


##Eskil Plugin : Compare text from PDF files. (needs pdftotext 4+)
## Option -marginl : Left margin to pass to pdftotext
## Option -marginr : Right margin to pass to pdftotext
## Option -margint : Top margin to pass to pdftotext
## Option -marginb : Bottom margin to pass to pdftotext
## Option -pdftotext : Extra options to pass to pdftotext
#
# This plugin runs input through the external tool pdftotext.
# Thus it must be run together with the -pluginallow flag.

# Example file for a plugin.
# A plugin's first line must start exactly like this one.
# The text after : is the summary you can get at the command line

# A plugin must define this procedure to do the job.
# side: left or right
# chi:  An input channel for reading the original file.
# cho:  An output channel for writing the processed file.
proc PreProcess {side chi cho} {
    if {[info commands exec] eq ""} {
        puts $cho "PDF plugin must be run with privilege to be able\
                   to execute pdftotext"
        return 1
    }
    set opts(-marginl) ""
    set opts(-marginr) ""
    set opts(-margint) ""
    set opts(-marginb) ""
    set opts(-pdftotext) ""
    foreach opt {-marginl -marginr -margint -marginb -pdftotext} {
        set i [lsearch -exact $::argv $opt]
        if {$i >= 0} {
            incr i
            set opts($opt) [lindex $::argv $i]
        }
    }
     set cands [auto_execok pdftotext]
    lappend cands [file join $::WhoAmIFull pdftotext]
    lappend cands [file join $::WhoAmIFull .. pdftotext]
    lappend cands [file join $::WhoAmIFull .. .. pdftotext]
    set found 0
    foreach cand $cands {
        if {[file exists $cand]} {
            set found 1
            break
        }
        if {[file exists $cand.exe]} {
            set cand $cand.exe
            set found 1
            break
        }
    }

    if { ! $found} {
        puts $cho "PDF plugin needs external tool 'pdftotext' to run"
        return 1
    }
    if {[catch {llength $::Info}]} {
        puts $cho "PDF plugin needs -plugininfo parameter to be a list"
        return 1
    }
    if {[catch {llength $opts(-pdftotext)}]} {
        puts $cho "PDF plugin needs -pdftotext parameter to be a list"
        return 1
    }
    # Pass options from -plugininfo as well.
    set options [concat $::Info $opts(-pdftotext)]
    foreach opt {-marginl -marginr -margint -marginb} {
        if {$opts($opt) ne ""} {
            lappend options $opt $opts($opt)
        }
    }
    # Use source file with pdftotext since stdin is not reliable on e.g Windows
    if {[catch {exec $cand {*}$options $::File($side) - >&@ $cho}]} {
        puts $cho "**************************************"
        puts $cho "PDF plugin got an error from pdftotext"
    }

    # Signal that the file after processing should be used both
    # for comparison and for displaying.
    return 1
}