First commit

master
Fabrice Mouhartem 4 years ago
commit 0769e1dfc4

@ -0,0 +1,21 @@
The MIT License
Copyright (c) 2019 Fabrice Mouhartem
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

@ -0,0 +1,12 @@
all: hidepdfads
hidepdfads: src/script.sh presets.txt
cat $^ > $@
presets.txt: data/presets.json
jq --raw-output '.[] | (.name + " " + .X + " " + .Y + " " + .L + " " + .H + " " + .color + " " + .unit + " " + .format)' < "$^" > "$@"
clean:
rm presets.txt
.PHONY: clean all

@ -0,0 +1,32 @@
[
{
"name" : "rhonexpress",
"X" : "15",
"Y" : "146.5",
"L" : "180",
"H" : "90",
"unit": "mm",
"format" : "a4",
"color" : "white"
},
{
"name" : "AF",
"X" : "11",
"Y" : "18",
"L" : "9",
"H" : "9",
"unit": "cm",
"format" : "a4",
"color" : "white"
},
{
"name" : "klm",
"X" : "11",
"Y" : "18",
"L" : "9",
"H" : "9",
"unit": "cm",
"format" : "a4",
"color" : "white"
}
]

@ -0,0 +1,105 @@
#!/bin/env sh
:${LATEXCC:=pdflatex}
print_help() {
>&2 echo "Usage: $0 <command> [<args>]"
>&2 echo -e "\nThis tool uses latex to cover a specific region of a PDF file on every page.\n"
>&2 echo "<command> can be:"
>&2 echo -e "\thelp:\tdisplay this help message"
>&2 echo -e "\tlist:\tlist the different presets"
>&2 echo -e "\tpreset:\tuse an existing preset\n\t\targuments: <preset> <infile> <outfile>"
>&2 echo -e "\tmanual:\tset coordinates and dimension of the rectangle\n\t\targuments: <infile> <outfile> X Y L H [color=white] [unit=cm] [format=a4]"
}
gen_pdf() { # arguments: infile outfile X Y L H color unit format
if [ $# -ne 9 ]
then
>&2 echo -e "Function gen_pdf: wrong number of arguments\n"
exit 1
fi
infile="$1"
outfile="$2"
X="$3"
Y="$4"
L="$5"
H="$6"
color="$7"
unit="$8"
format="$9"
tmp_dir=`mktemp -d`
act_dir=$(pwd)
cd "$tmp_dir"
echo "\\documentclass[$format""paper]{article}" > main.tex
cat <<EOB >>main.tex
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pdfpages}
\pagestyle{empty}
\begin{document}
\includepdf[pages={-},% include all pages
pagecommand={% is called at the beginning of each inclusion
\begin{tikzpicture}[remember picture,overlay]
EOB
echo "\\draw[color=$color,fill=$color] (\$(current page.north west) + ($X$unit, -$Y$unit)\$) rectangle ++ ($L$unit, -$H$unit);\\end{tikzpicture}}]{$infile}\\end{document}" >>main.tex
cat main.tex
$LATEXCC -interaction=nonstopmode main.tex || { >&2 echo -e "\n\n\nLaTeX error during compilation"; rm -r "$tmp_dir"; exit 1; }
$LATEXCC -interaction=nonstopmode main.tex
cp -i main.pdf "$act_dir/$outfile"
rm -r "$tmp_dir"
}
case "$1" in
"list")
echo -n "Available presets: "
preset_list=$(sed '0,/^__DATA__$/d' "$0" | cut -d' ' -f1)
echo $preset_list
;;
"help")
print_help
;;
"")
print_help
;;
"preset")
if [ $# -ne 4 ]
then
>&2 echo -e "preset: wrong number of arguments\n"
print_help
else
read_presets="$(sed '0,/^__DATA__$/d' "$0")"
i=0
while read -r line
do
presets[$i]="$line"
i=`echo $i + 1 | bc`
done <<< "$read_presets"
for elts in "${presets[@]}"
do
x=($elts)
[[ "$2" == ${x[0]} ]] && gen_pdf "$3" "$4" "${x[1]}" "${x[2]}" "${x[3]}" "${x[4]}" "${x[5]}" "${x[6]}" "${x[7]}" && exit 0
done
>&2 echo -e "preset: preset \"$2\" not found\n"
print_help
fi
;;
"manual")
if [ $# -lt 7 ]
then
>&2 echo -e "manual: wrong number of arguments\n"
print_help
else
color=${8:-white}
unit=${9:-cm}
format=${10:-a4}
gen_pdf "$2" "$3" "$4" "$5" "$6" "$7" "$color" "$unit" "$format" && exit 0
fi
esac
exit
# data format: X Y L H color unit format
__DATA__
rhonexpress 15 146.5 180 90 white mm a4
AF 11 18 9 9 white cm a4
klm 11 18 9 9 white cm a4

@ -0,0 +1,73 @@
# hidepdfads
A small tool to hide a recurrent part of a PDF file such as ads on plane tickets for instance.
It is a scripted version of [this blog post](https://blog.epheme.re/latex-ad-block.html).
## Files
The project comprises two main files: scr/script.sh and data/presets.json
These two files are used to generate the `pdf-adblocker` standalone file.
## Build and run
To build, simply run the `make` command in this directory.
The following commands are available:
```txt
Usage: hidepdfads <command> [<args>]
This tool uses latex to cover a specific region of a PDF file on every page.
<command> can be:
help: display this help message
list: list the different presets
preset: use an existing preset
arguments: <preset> <infile> <outfile>
manual: set coordinates and dimension of the rectangle
arguments: <infile> <outfile> X Y L H [color=white] [unit=cm] [format=a4paper]
```
Example:
```sh
$ hidepdfads preset AF plane-ticket.pdf plane-ticket-adfree.pdf
```
### Prerequisites
To build the PDF, you may need [jq](https://stedolan.github.io/jq/) to parse the json database.
The script relies on latex to run. You can specify your favorite latex compiler in the environment variable `LATEXCC`.
## Add presets
Presets are compiled in a json file with the following signature:
```json
{
"name" : "preset-name",
"X" : "0",
"Y" : "0",
"L" : "0",
"H" : "0",
"unit" : "mm/cm/in/pt or any LaTeX-compatible unit",
"format" : "a4/letter",
"color" : "white or any LaTeX-compatible color"
},
```
This data can be filled using for instance `inkscape` to find the location and size of the area to hide.
![inkscape dimensions](https://blog.epheme.re/examples/inkscape-adblock.png)
**Instructions:** After opening your pdf file, start by selecting the ad (purple), you may have to ungroup elements (ctrl+shift+g), then set the dimensions in cm or your favourite length unit (blue) and finally note the dimensions of the ad (red).
Feel free to contribute to this (so far small) database of PDF cleaner presets 😉
## Acknowledgements
The basecode and principles of the code are highly inspired from [dmenu-emoji](https://github.com/porras/dmenu-emoji).
## TODO
* Simplify the way to fill the database

@ -0,0 +1,102 @@
#!/bin/env sh
:${LATEXCC:=pdflatex}
print_help() {
>&2 echo "Usage: $0 <command> [<args>]"
>&2 echo -e "\nThis tool uses latex to cover a specific region of a PDF file on every page.\n"
>&2 echo "<command> can be:"
>&2 echo -e "\thelp:\tdisplay this help message"
>&2 echo -e "\tlist:\tlist the different presets"
>&2 echo -e "\tpreset:\tuse an existing preset\n\t\targuments: <preset> <infile> <outfile>"
>&2 echo -e "\tmanual:\tset coordinates and dimension of the rectangle\n\t\targuments: <infile> <outfile> X Y L H [color=white] [unit=cm] [format=a4]"
}
gen_pdf() { # arguments: infile outfile X Y L H color unit format
if [ $# -ne 9 ]
then
>&2 echo -e "Function gen_pdf: wrong number of arguments\n"
exit 1
fi
infile="$1"
outfile="$2"
X="$3"
Y="$4"
L="$5"
H="$6"
color="$7"
unit="$8"
format="$9"
tmp_dir=`mktemp -d`
act_dir=$(pwd)
cd "$tmp_dir"
echo "\\documentclass[$format""paper]{article}" > main.tex
cat <<EOB >>main.tex
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pdfpages}
\pagestyle{empty}
\begin{document}
\includepdf[pages={-},% include all pages
pagecommand={% is called at the beginning of each inclusion
\begin{tikzpicture}[remember picture,overlay]
EOB
echo "\\draw[color=$color,fill=$color] (\$(current page.north west) + ($X$unit, -$Y$unit)\$) rectangle ++ ($L$unit, -$H$unit);\\end{tikzpicture}}]{$infile}\\end{document}" >>main.tex
cat main.tex
$LATEXCC -interaction=nonstopmode main.tex || { >&2 echo -e "\n\n\nLaTeX error during compilation"; rm -r "$tmp_dir"; exit 1; }
$LATEXCC -interaction=nonstopmode main.tex
cp -i main.pdf "$act_dir/$outfile"
rm -r "$tmp_dir"
}
case "$1" in
"list")
echo -n "Available presets: "
preset_list=$(sed '0,/^__DATA__$/d' "$0" | cut -d' ' -f1)
echo $preset_list
;;
"help")
print_help
;;
"")
print_help
;;
"preset")
if [ $# -ne 4 ]
then
>&2 echo -e "preset: wrong number of arguments\n"
print_help
else
read_presets="$(sed '0,/^__DATA__$/d' "$0")"
i=0
while read -r line
do
presets[$i]="$line"
i=`echo $i + 1 | bc`
done <<< "$read_presets"
for elts in "${presets[@]}"
do
x=($elts)
[[ "$2" == ${x[0]} ]] && gen_pdf "$3" "$4" "${x[1]}" "${x[2]}" "${x[3]}" "${x[4]}" "${x[5]}" "${x[6]}" "${x[7]}" && exit 0
done
>&2 echo -e "preset: preset \"$2\" not found\n"
print_help
fi
;;
"manual")
if [ $# -lt 7 ]
then
>&2 echo -e "manual: wrong number of arguments\n"
print_help
else
color=${8:-white}
unit=${9:-cm}
format=${10:-a4}
gen_pdf "$2" "$3" "$4" "$5" "$6" "$7" "$color" "$unit" "$format" && exit 0
fi
esac
exit
# data format: X Y L H color unit format
__DATA__
Loading…
Cancel
Save