Ajout des sous-commandes extract & list, correction du formatage des octets, correction, ajout de fonctions pour rechercher en profondeur dans l’arborescence des objets pdf, intégration de starship comme prompt + ajout des modules souvent utilisés
This commit is contained in:
parent
40c07d15d8
commit
39beac61a9
4 changed files with 386 additions and 22 deletions
193
aliases/pdf.elv
193
aliases/pdf.elv
|
|
@ -343,6 +343,195 @@ fn info {|@args|
|
|||
} $part
|
||||
}
|
||||
|
||||
fn -list-attachments {|in|
|
||||
all (pdf:attachments $in)
|
||||
}
|
||||
|
||||
fn -display-attachments {|in|
|
||||
var @data = (-list-attachments $in)
|
||||
printf "%d pièce(s)-jointe(s) trouvée(s):\n\n" (count $data)
|
||||
all $data | each $echo~
|
||||
echo
|
||||
}
|
||||
|
||||
fn -extract-attachments {|in out|
|
||||
-list-attachments $in | each {|f|
|
||||
pdf:attachment $in $out/$f $f
|
||||
}
|
||||
}
|
||||
|
||||
fn -list-fonts {|in|
|
||||
pdf:fonts $in | each {|f|
|
||||
if (has-key $f /FontDescriptor) {
|
||||
put [&font=$f &fd=$f[/FontDescriptor]]
|
||||
}
|
||||
} | each {|f|
|
||||
if (has-key $f[fd] /FontName) {
|
||||
assoc $f name $f[fd][/FontName]
|
||||
}
|
||||
} | each {|f|
|
||||
var @_ n = (str:split '+' $f[name])
|
||||
var font = $f[font]
|
||||
var fd = $f[fd]
|
||||
var embed = (has-key $fd /FontFile2)
|
||||
var file = ''
|
||||
set n = (str:trim-prefix $n '/')
|
||||
if $embed {
|
||||
set file = $fd[/FontFile2]
|
||||
}
|
||||
put [
|
||||
&id=$font[id]
|
||||
&name=$n
|
||||
&embed=$embed
|
||||
&file=$file
|
||||
&type=(str:trim-prefix $font[/Subtype] '/')
|
||||
&encoding=(str:trim-prefix $font[/Encoding] '/')
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn -display-fonts {|in|
|
||||
var @data = (-list-fonts $in)
|
||||
printf "%d police(s) trouvée(s):\n\n" (count $data)
|
||||
var props = [
|
||||
(format:column &align=center id ID)
|
||||
(format:column name Nom)
|
||||
(format:column type Type)
|
||||
(format:column encoding Encodage)
|
||||
(format:column &align=center embed Téléchargeable)
|
||||
(format:column &align=center file Fichier)
|
||||
]
|
||||
format:list $props $data
|
||||
echo
|
||||
}
|
||||
|
||||
fn -extract-fonts {|in out|
|
||||
-list-fonts $in | each {|f|
|
||||
if $f[embed] {
|
||||
pdf:filtered-stream $in (printf '%s/%s.ttf' $out $f[name]) $f[file]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn -list-images {|in|
|
||||
pdf:images $in | each {|i|
|
||||
var c = ''
|
||||
var w = (format:int $i[/Width])
|
||||
var h = (format:int $i[/Height])
|
||||
if (has-key $i /ColorSpace) {
|
||||
set c = $i[/ColorSpace][0]
|
||||
}
|
||||
put [
|
||||
&id=$i[id]
|
||||
&page=(format:int $i[page])
|
||||
&name=(str:trim-prefix $i[name] '/')
|
||||
&width=$w
|
||||
&height=$h
|
||||
&dim=(printf '%dx%d' $w $h)
|
||||
&filter=(str:trim-prefix $i[/Filter] '/')
|
||||
&size=(format:size $i[/Length])
|
||||
&bpc=(format:int $i[/BitsPerComponent])
|
||||
&color=(str:trim-prefix $c '/')
|
||||
]
|
||||
}
|
||||
var objects = (pdf:objects $in)
|
||||
all (pdf:pages $in) | each {|p|
|
||||
var pn = (format:int $p[pageposfrom1])
|
||||
all $p[images] | each {|img|
|
||||
var id = $img[object]
|
||||
var o = (pdf:-object &extend=$true $objects $id)
|
||||
var s = $o[/Length]
|
||||
var c = ''
|
||||
var w = (format:int $o[/Width])
|
||||
var h = (format:int $o[/Height])
|
||||
if (has-key $o /ColorSpace) {
|
||||
set c = $o[/ColorSpace][0]
|
||||
}
|
||||
put [
|
||||
&id=$id
|
||||
&page=$pn
|
||||
&name=(str:trim-prefix $img[name] '/')
|
||||
&width=$w
|
||||
&height=$h
|
||||
&dim=(printf '%dx%d' $w $h)
|
||||
&filter=(str:trim-prefix $o[/Filter] '/')
|
||||
&size=(format:size $s)
|
||||
&bpc=(format:int $o[/BitsPerComponent])
|
||||
&color=(str:trim-prefix $c '/')
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn -display-images {|in|
|
||||
var @data = (-list-images $in)
|
||||
printf "%d image(s) trouvée(s):\n\n" (count $data)
|
||||
var props = [
|
||||
(format:column &align=right page Page)
|
||||
(format:column &align=center id ID)
|
||||
(format:column name Nom)
|
||||
(format:column &align=right size Taille)
|
||||
(format:column &align=right dim Dimensions)
|
||||
(format:column color Couleur)
|
||||
(format:column &align=right bpc BPC)
|
||||
(format:column &align=center filter Filtre)
|
||||
]
|
||||
format:list $props $data
|
||||
echo
|
||||
}
|
||||
|
||||
fn -extract-images {|in out|
|
||||
# En attendant de savoir décoder correctement les images, on utilise poppler
|
||||
pdfimages -all $in $out/Im
|
||||
}
|
||||
|
||||
fn list {|@args|
|
||||
if (!= (count $args) 2) {
|
||||
fail 'list doit comporter 2 paramètres'
|
||||
}
|
||||
var types in = $@args
|
||||
-must-pdf-exist $in
|
||||
var display = [
|
||||
&a=$-display-attachments~
|
||||
&f=$-display-fonts~
|
||||
&i=$-display-images~
|
||||
]
|
||||
str:split ',' $types | each {|t|
|
||||
set t = (str:to-lower $t)
|
||||
-must-valid $t {|e|
|
||||
and (> (count $e) 0) (has-key $display $e[0])
|
||||
}
|
||||
$display[$t[0]] $in
|
||||
}
|
||||
}
|
||||
|
||||
fn extract {|@args|
|
||||
if (!= (count $args) 3) {
|
||||
fail 'extract doit comporter 3 paramètres'
|
||||
}
|
||||
var types in out = $@args
|
||||
-must-pdf-exist $in
|
||||
-must-not-exist $out
|
||||
var extr = [
|
||||
&a=$-extract-attachments~
|
||||
&f=$-extract-fonts~
|
||||
&i=$-extract-images~
|
||||
]
|
||||
try {
|
||||
mkdir $out
|
||||
str:split ',' $types | each {|t|
|
||||
set t = (str:to-lower $t)
|
||||
-must-valid $t {|e|
|
||||
and (> (count $e) 0) (has-key $extr $e[0])
|
||||
}
|
||||
$extr[$t[0]] $in $out
|
||||
}
|
||||
} except e {
|
||||
rm -rf $out
|
||||
fail $e
|
||||
}
|
||||
}
|
||||
|
||||
edit:add-var pdf~ {|action @args|
|
||||
var actions = [
|
||||
&help=$help~
|
||||
|
|
@ -358,8 +547,8 @@ edit:add-var pdf~ {|action @args|
|
|||
&zip=$zip~
|
||||
&unzip=$unzip~
|
||||
&info=$info~
|
||||
#&list=$list~
|
||||
#&extract=$extract~
|
||||
&list=$list~
|
||||
&extract=$extract~
|
||||
]
|
||||
if (not (has-key $actions $action)) {
|
||||
fail (printf '%s: action inconnue' $action)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue