Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- filename=$1
- if [[ -z $1 ]]; then
- echo "USAGE: ./pdf-to-csv.sh compiledform.pdf outputtable.csv H"
- echo "The last argument can be both V or H. If V (or not specified) the table is going to be vertical, if H the table is going to be horizontal. The letter O is an alias for H, they both mean horizontal."
- echo "If the second argument is not specified, the program just prints on screen a vertical tables."
- echo "Please notice that this script works if you build your PDF form with LibreOffice Writer, and if the user compiles it with LibreOffice Writer. Did not try other tools, they might works as well but I can't guarantee."
- echo "Script by Luca Tringali, tringalinvent [at] libero.it"
- else
- pdftk $filename dump_data_fields output filled_form.txt
- names=$(less filled_form.txt | grep "FieldName:" | cut -d ":" -f 2 | cut -c 2-)
- values=$(less filled_form.txt | grep "FieldValue:" | cut -d ":" -f 2 | cut -c 2-)
- if [[ -z $3 ]] || [[ $3 == "V" ]]; then
- header="FieldName;FieldValue"
- data=$(paste <(echo "$names") <(echo "$values") --delimiters ';')
- else
- if [[ $3 == "H" ]] || [[ $3 == "O" ]]; then
- header=""
- while read -r line
- do
- header="$header$line;"
- done <<<"$names"
- data=""
- while read -r line
- do
- data="$data$line;"
- done <<<"$values"
- fi
- fi
- csvtext="${header}\n${data}"
- if [[ -z $2 ]]; then
- echo -e "${csvtext}"
- else
- echo -e "${csvtext}" > $2
- fi
- rm filled_form.txt
- fi
Add Comment
Please, Sign In to add comment