From 7513744bf1160400f6077c4da499df747a458641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Wed, 7 Feb 2024 20:13:04 +0100 Subject: [PATCH] feat: add manipulations-fichiers-pdf.md --- .aspell.fr.pws | 15 +- sources/linux/manipulations-fichiers-pdf.md | 133 ++++++++++++++++++ .../snippets/manipulations-fichiers-pdf.sh | 40 ++++++ 3 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 sources/linux/manipulations-fichiers-pdf.md create mode 100644 sources/linux/snippets/manipulations-fichiers-pdf.sh diff --git a/.aspell.fr.pws b/.aspell.fr.pws index 004fc20..8ba2a2f 100644 --- a/.aspell.fr.pws +++ b/.aspell.fr.pws @@ -1,8 +1,9 @@ -personal_ws-1.1 fr 241 +personal_ws-1.1 fr 254 AVAX Aborted Add Android +Author BL BMS BoboTiG @@ -13,6 +14,8 @@ ChainID Circle Contabo Cores +CreationDate +Creator Crontab.guru Cygwin D-Link @@ -34,6 +37,7 @@ GJ GPU Gandi Geth +GhostScript Gio Git GitHub @@ -50,6 +54,7 @@ Imports JPEG Joe Keep +Keywords L'implémentation Left.svg Lighttpd @@ -61,6 +66,7 @@ MPEG Mainnet MesloLGS Metamask +ModDate Monaspace My NF @@ -74,12 +80,14 @@ Online OpenClipart PC PDF +PDFtk PHP PNG PS Pi-hole Powerlevel Private +Producer Pré-requis Prérequis Pylontech @@ -91,6 +99,8 @@ Rabby Raspberry Raspbian ReactOS +Redimensionner +Redimensionnons Remove Rpi SFTP @@ -100,12 +110,15 @@ Server Setup Shaarli Shibuya +Subject Symbol System SystemRescueCD TIA TTY +Title Trader +Trapped Téléchargement URL USB diff --git a/sources/linux/manipulations-fichiers-pdf.md b/sources/linux/manipulations-fichiers-pdf.md new file mode 100644 index 0000000..925ad47 --- /dev/null +++ b/sources/linux/manipulations-fichiers-pdf.md @@ -0,0 +1,133 @@ +# Manipulation de fichiers PDF + +[PDFtk](https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit) est un outil puissant pour la manipulation de fichiers PDF. + +## Orienter + +Pour l'exemple, disons que les pages 301, 302 et 303 du fichier source sont en mode paysage alors que toutes les autres sont en mode portrait. Voici comment réorienter ces 3 pages : + +```{literalinclude} snippets/manipulations-fichiers-pdf.sh + :lines: 3-10 + :language: shell +``` + +## Redimensionner + +Pour vérifier que toutes les pages ont les même marges : + +```{literalinclude} snippets/manipulations-fichiers-pdf.sh + :lines: 12 + :language: shell +``` + +Exemple de sortie : + +```{code-block} +/MediaBox [0 0 595 842] +/MediaBox [0 0 612 792] +``` + +Redimensionnons toutes les pages : + +```{literalinclude} snippets/manipulations-fichiers-pdf.sh + :lines: 13-15 + :language: shell +``` + +## Fusionner + +Pour fusionner plusieurs fichiers PDF en un seul : + +```{literalinclude} snippets/manipulations-fichiers-pdf.sh + :lines: 17-22 + :language: shell +``` + +## Optimiser + +GhostScript permet de réduire grandement le poids du fichier final : + +```{literalinclude} snippets/manipulations-fichiers-pdf.sh + :lines: 32-37 + :language: shell +``` + +## Personnaliser les Méta-données + +Exporter les méta-données actuelles dans le fichier `metadata.txt` : + +```{literalinclude} snippets/manipulations-fichiers-pdf.sh + :lines: 39 + :language: shell +``` + +Le contenu du fichier ressemble à ça : + +```{code-block} +InfoBegin +InfoKey: ModDate +InfoValue: D:20191114220102Z +InfoBegin +InfoKey: Producer +InfoValue: PRODUCTER +PdfID0: d8ec8095099bebfa395c663ac4e4a26e +PdfID1: 6f977277cf1bfe9a5b8c1c60f2ba175d +NumberOfPages: 158 +PageMediaBegin +PageMediaNumber: 1 +PageMediaRotation: 0 +PageMediaRect: 0 8.58 1,057.5 1,696.08 +PageMediaDimensions: 1,057.5 1,687.5 +... +``` + +Modifier les première lignes ou ajouter les méta-données manquantes (parmi : *Title*, *Author*, *Subject*, *Keywords*, *Creator*, *Producer*, *CreationDate*, *ModDate* et *Trapped*): + +```{code-block} + :emphasize-lines: 1-9 + +InfoBegin +InfoKey: Title +InfoValue: TITLE +InfoBegin +InfoKey: Author +InfoValue: AUTHOR +InfoBegin +InfoKey: CreationDate +InfoValue: D:20191114220102Z +InfoBegin +InfoKey: ModDate +InfoValue: D:20200409101248Z +InfoBegin +InfoKey: Producer +InfoValue: PRODUCTER +PdfID0: d8ec8095099bebfa395c663ac4e4a26e +PdfID1: 6f977277cf1bfe9a5b8c1c60f2ba175d +NumberOfPages: 158 +PageMediaBegin +PageMediaNumber: 1 +PageMediaRotation: 0 +PageMediaRect: 0 8.58 1,057.5 1,696.08 +PageMediaDimensions: 1,057.5 1,687.5 +... +``` + +Générer un nouveau PDF comportant les nouvelles méta-données : + +```{literalinclude} snippets/manipulations-fichiers-pdf.sh + :lines: 40 + :language: shell +``` + +--- + +## 📜 Historique + +2024-02-07 +: Déplacement de l'article depuis le [blog](https://www.tiger-222.fr/?d=2019/12/07/14/59/33-manipulation-de-fichiers-pdf). + +2020-04-09 +: Ajout de la section [Personnaliser les Méta-données](#personnaliser-les-meta-donnees). + +2019-12-17 +: Premier jet. diff --git a/sources/linux/snippets/manipulations-fichiers-pdf.sh b/sources/linux/snippets/manipulations-fichiers-pdf.sh new file mode 100644 index 0000000..e8b54f1 --- /dev/null +++ b/sources/linux/snippets/manipulations-fichiers-pdf.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +pdftk \ + 'FILE_SOURCE.pdf' \ + cat \ + 1-300 \ + 301-303left \ + 304-end \ + output \ + 'NEW_FILE.pdf' + +grep --text MediaBox 'FILE_SOURCE.pdf' | sort -u +sed 's/MediaBox \[0 0 595.*/MediaBox \[0 0 612 792\]/g' \ + 'FILE_SOURCE.pdf' \ + > 'NEW_FILE.pdf' + +pdftk \ + 'FILE_SOURCE_1.pdf' \ + 'FILE_SOURCE_2.pdf' \ + cat \ + output \ + 'NEW_FILE.pdf' + +gs \ + -o 'FILE_SOURCE.pdf' \ + -sDEVICE=pdfwrite \ + -dPDFSETTINGS=/prepress \ + -dEmbedAllFonts=true \ + -sFONTPATH='./pdf-fonts' \ + 'NEW_FILE.pdf' + +gs \ + -o 'FILE_SOURCE.pdf' \ + -sDEVICE=pdfwrite \ + -dPDFSETTINGS=/prepress \ + -dEmbedAllFonts=true \ + 'NEW_FILE.pdf' + +pdftk 'FILE_SOURCE.pdf' dump_data_utf8 output 'metadata.txt' +pdftk 'FILE_SOURCE.pdf' update_info_utf8 'metadata.txt' output 'NEW_FILE.pdf'