Skip to content

Commit

Permalink
feat: add manipulations-fichiers-pdf.md
Browse files Browse the repository at this point in the history
  • Loading branch information
BoboTiG committed Feb 7, 2024
1 parent 987170d commit 7513744
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 1 deletion.
15 changes: 14 additions & 1 deletion .aspell.fr.pws
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
personal_ws-1.1 fr 241
personal_ws-1.1 fr 254
AVAX
Aborted
Add
Android
Author
BL
BMS
BoboTiG
Expand All @@ -13,6 +14,8 @@ ChainID
Circle
Contabo
Cores
CreationDate
Creator
Crontab.guru
Cygwin
D-Link
Expand All @@ -34,6 +37,7 @@ GJ
GPU
Gandi
Geth
GhostScript
Gio
Git
GitHub
Expand All @@ -50,6 +54,7 @@ Imports
JPEG
Joe
Keep
Keywords
L'impl�mentation
Left.svg
Lighttpd
Expand All @@ -61,6 +66,7 @@ MPEG
Mainnet
MesloLGS
Metamask
ModDate
Monaspace
My
NF
Expand All @@ -74,12 +80,14 @@ Online
OpenClipart
PC
PDF
PDFtk
PHP
PNG
PS
Pi-hole
Powerlevel
Private
Producer
Pr�-requis
Pr�requis
Pylontech
Expand All @@ -91,6 +99,8 @@ Rabby
Raspberry
Raspbian
ReactOS
Redimensionner
Redimensionnons
Remove
Rpi
SFTP
Expand All @@ -100,12 +110,15 @@ Server
Setup
Shaarli
Shibuya
Subject
Symbol
System
SystemRescueCD
TIA
TTY
Title
Trader
Trapped
T�l�chargement
URL
USB
Expand Down
133 changes: 133 additions & 0 deletions sources/linux/manipulations-fichiers-pdf.md
Original file line number Diff line number Diff line change
@@ -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.
40 changes: 40 additions & 0 deletions sources/linux/snippets/manipulations-fichiers-pdf.sh
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 7513744

Please sign in to comment.