• # Essais & gros mémo

    Posté par . En réponse au message Une commande pour filigraner des documents en série.. Évalué à 3.

    Après quelques essais je me suis souvenu pourquoi j'avais écarté pdftk pour les filigranes.
    Les PDF gardent en qualité et les composantes restent sélectionnables, mais le filigrane est un calque apposé, aisément supprimable.
    Trouzemilles options sur 3 logiciels, je pose mes quelques notes ici.

    pdftk & imagemagick

    # empty A4 pdf
    # https://unix.stackexchange.com/questions/277892/how-do-i-create-a-blank-pdf-from-the-command-line
    convert xc:none -page A4 A4.pdf
    SOURCE=A4.pdf
    DESTINATION=watermark.pdf
    WATERMARK="mon beau watermark pour pdftk"
    # create A4 watermark
    convert \
    -density 150 \
    -trim \
    -quality 100 \
    -flatten \
    -sharpen 0x1.0 \
    $SOURCE \
    \( \
    -size 300x \
    -background none \
    -fill "RGBA(255,0,0,0.15)" \
    -gravity center "label:$WATERMARK" \
    -trim \
    -rotate -42 \
    -bordercolor none \
    -border 10 \
    -write mpr:wm \
    +delete \
    +clone \
    -fill mpr:wm \
    -draw "color 0,0 reset" \
     \) \
    -compose over \
    -composite \
    $DESTINATION
    # pdftk watermark single & multipages
    pdftk source.pdf stamp watermark.pdf output final.pdf
    

    imagemagick & ghostscript

    Cas concret avec une attestation EDF, PDF comportant des erreurs et 1,5Mo

    gs -dQUIET -dBATCH -dNODISPLAY -dNOPAUSE -dPDFINFO Attestation.pdf
    **** Error: An error occurred while reading an XREF table.
    **** The file has been damaged. This may have been caused
    **** by a problem while converting or transferring the file.
    **** Ghostscript will attempt to recover the data.
    **** However, the output may be incorrect.

    Réduction à 50Ko et base vierge d'erreurs
    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=$CLEAN $SOURCE

    #######
    SOURCE=Attestation.pdf
    CLEAN=clean.pdf
    DESTINATION=final.pdf
    COMPRESS=light.pdf
    WATERMARK="2024/09/17_NUMDOSSIER_DESTINATAIRE"
    # cas EDF
    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=$CLEAN $SOURCE
    # fili
    convert \
    -density 150 \
    -trim \
    $CLEAN \
    -quality 100 \
    -flatten \
    -sharpen 0x1.0 \
    \( \
    -size 300x \
    -background none \
    -fill "RGBA(255,0,0,0.15)" \
    -gravity center "label:$WATERMARK" \
    -trim \
    -rotate -42 \
    -bordercolor none \
    -border 10 \
    -write mpr:wm \
    +delete \
    +clone \
    -fill mpr:wm \
    -draw "color 0,0 reset" \
     \) \
    -compose over \
    -composite \
    $DESTINATION
    ######
    

    compression avec ghostscript

    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile=$COMPRESS $DESTINATION 
    

    Pour résumer source 1,5Mo > 50Ko> watermark 2,1Mo > 165Ko
    Le QRCode du cachet électronique reste scannable à 150%

    Note que dans tous les cas convert fait appel à ghostscript
    Pour augmenter qualité du filigrane avec imagemagick:
    https://stackoverflow.com/questions/6605006/convert-pdf-to-image-with-high-resolution#6605085
    https://stackoverflow.com/questions/15769623/imagemagick-convert-pdf-to-jpeg-has-poor-text-quality-after-upgrading-imagemagic
    Agir avec options density & quality & sharpen

    TODO

    Imagemagick ne sait pas traiter plusieurs pages de pdf séparément, mais peut y accéder via file.pdf[0]
    L'option border pour gérer bordure au-dessus, en-dessous, pas compris...

    Ressources

    imagemagick
    https://imagemagick.org/script/command-line-options.php

    ghostscript
    https://ghostscript.readthedocs.io/en/latest/Use.html#
    https://github.com/gilles83100/ghostscript?tab=readme-ov-file#reduction-de-la-taille-des-documents

    ghostscript - compression pdf
    https://gist.github.com/ahmed-musallam/27de7d7c5ac68ecbd1ed65b6b48416f9
    On peut aussi attaquer sur les images et niveaux de gris
    L'option PDFSETTINGS
    https://stackoverflow.com/questions/46195795/ghostscript-pdf-batch-compression

    Other options for PDFSETTINGS:
    screen selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting.
    ebook selects medium-resolution output similar to the Acrobat Distiller "eBook" setting.
    printer selects output similar to the Acrobat Distiller "Print Optimized" setting.
    prepress selects output similar to Acrobat Distiller "Prepress Optimized" setting.
    default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.

    ingénierie inverse de pdf
    https://stackoverflow.com/questions/3549541/how-can-i-visually-inspect-the-structure-of-a-pdf-to-reverse-engineer-it

    pour les couleurs
    https://rgbacolorpicker.com/