Como posso extrair páginas contendo uma determinada string de um arquivo PDF?

3

Eu tenho um arquivo PDF contendo 100 páginas. Gostaria de extrair essas páginas contendo uma string específica.

Como posso conseguir isso? Talvez usando ghostscript na linha de comando?

Por que vale a pena: Estou usando o Edubuntu 12.04 LTS.

    
por user273938 25.04.2014 / 05:23

1 resposta

3

Visão geral

Aqui está um script que eu juntei rapidamente e que deveria fazer o trabalho. Certifique-se de ler o preâmbulo e os comentários para obter informações sobre como usar o script e como ele funciona.

Script

#!/bin/bash

# NAME:         extract_pdf_results
# VERSION:      0.1
# AUTHOR:       (c) 2014 Glutanimate
# DESCRIPTION:  Extracts PDF pages that contain supplied string and concatenates them to a new file.
# FEATURES:     
# DEPENDENCIES: pdfgrep pdftk
#               ➥install on Ubuntu/Debian with sudo apt-get install pdfgrep pdftk
#
# LICENSE:      GNU GPLv3 (http://www.gnu.de/documents/gpl-3.0.en.html)
#
# NOTICE:       THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 
#               EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 
#               PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR 
#               IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
#               AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND 
#               PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
#               YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
#
#               IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY 
#               COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS 
#               PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, 
#               INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE 
#               THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED 
#               INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE 
#               PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER 
#               PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
#
# USAGE:        extract_pdf_results <string> <pdffile>

STRING=""
FILE=""
FILENAME="${FILE##*/})"
BASENAME="${FILENAME%.*}"
DIRNAME="${FILE%/*}"

echo "Processing $FILE..."

## find pages that contain string, remove duplicates, convert newlines to spaces

echo "Looking for $STRING..."

PAGES="$(pdfgrep -n "$STRING" "$FILE" | cut -f1 -d ":" | uniq | tr '\n' ' ')"

echo "Matching pages:
$PAGES"

## extract pages to new file in original directory

echo "Extracting result pages..."

pdftk "$FILE" cat $PAGES output "${DIRNAME}/${BASENAME}_pages_with_${STRING}.pdf"

echo "Done."

Exemplo

./extract_pdf_results.sh Lagrange ./test.pdf
Processing ./test.pdf...
Looking for Lagrange...
Matching pages:
3 
Extracting result pages...
Done.
    
por Glutanimate 25.04.2014 / 13:29