CAO : KICAD PDF Schematic Add Bookmarks

De Wiki_du_Réseau_des_Electroniciens_du_CNRS
Aller à la navigationAller à la recherche

Bookmarks dans un schéma PDF

Imprimer un schéma vers un fichier génère un PDF, mais sans bookmarks, ce qui rend la navigation fastidieuse.

Script

Le script Bash ci-dessous permet d'y remédier. Il peut s'exécuter nativement sur Linux et dans le terminal Git Bash sur Windows.

Script pdf-add-bkmk.sh :

#!/usr/bin/bash

function usage()
{
    echo "Usage: $(basename $0) INPUT_PDF PAGES OUTPUT_PDF"
    echo "Add bookmarks to a PDF document"
    echo -e "  INPUT_PDF\tInput PDF file without bookmarks"
    echo -e "  PAGES\t\tBookmarks definition in a CSV file (see below)"
    echo -e "  OUTPUT_PDF\tOutput PDF file with bookmarks"
    echo -ne "\n  PAGES syntax:
    2-column CSV file, no header, separator = ',': page title, bookmark level\n
    The page number depends on the order of the lines in the file.
    Example:
      Root,1                --> line 1 will be page 1
      A subsheet,2          --> line 2 will be page 2
      Another subsheet,2    --> line 3 will be page 3\n"
}

if [ $# -lt 3 ]; then
    echo "ERROR: missing arguments"
    usage
    exit 1
fi

if [ ! -f $1 ]; then
    echo "ERROR: file $1 does not exist"
    exit 1
fi

if [ ! -f $2 ]; then
    echo "ERROR: file $2 does not exist"
    exit 1
fi

INPUT_PDF=$1
PAGES=$2
OUTPUT_PDF=$3
TMP_BOOKMARKS=/tmp/bookmarks.txt
TMP_DATA_ORIG=/tmp/data.txt
TMP_DATA_UPDATED=/tmp/data_updated.txt

cat /dev/null > $TMP_BOOKMARKS

# Parse CSV file and generate bookmark file:
pgNum=1
while IFS=, read -r pgTitle bmLevel
do
    echo -e "BookmarkBegin\nBookmarkTitle: $pgTitle\nBookmarkLevel: $bmLevel\nBookmarkPageNumber: $pgNum" \
        >> $TMP_BOOKMARKS
    ((pgNum++))
done < $PAGES
sed -i 's/\"//g' $TMP_BOOKMARKS

# Generate PDF with bookmarks:
pdftk $INPUT_PDF dump_data output $TMP_DATA_ORIG
cat $TMP_DATA_ORIG $TMP_BOOKMARKS > $TMP_DATA_UPDATED
pdftk $INPUT_PDF update_info $TMP_DATA_UPDATED output $OUTPUT_PDF

Installation

  1. Copier/coller le code ci-dessus dans un fichier texte nommé pdf-add-bkmk.sh.

Enregistrer le fichier sous $HOME/bin sur Linux et dans un répertoire qui est dans la variable d'environnement PATH sur Windows.

  1. Sur Linux, rendre le script exécutable : $ chmod a+x pdf-add-bkmk.sh
  2. Installer PDFtk. Sur Windows, ajouter son répertoire à la variable d'environnement PATH. Sur Debian-like : $ sudo apt install pdftk-java.

Exemple

Fichiers :

  • schema.pdf : schéma imprimé au format PDF par KiCad
  • pages.csv : noms et niveaux des bookmarks
  • schema_bm.pdf : schéma PDF, les bookmarks en plus !
$ pdf-add-bkmk.sh schema.pdf pages.csv schema_bm.pdf

Le fichier pages.csv a 2 colonnes. La première est le titre du bookmark et la seconde est le niveau des pages (subsheets) dans la hiérarchie du schéma. Par exemple, dans le cas d'un schéma racine avec 2 sous-schémas :

Root,1
Microcontrôleur,2
Capteurs,2
Alimentation,2