0

I am upload one file which is PDF file and I want to search particular word contains in uploaded

File using only grep command in php.

Thanks in advance.

Nisarg Patel
  • 260
  • 1
  • 8
Chirag Shah
  • 1,463
  • 2
  • 18
  • 40
  • 1
    PDF is a binary format and `grep` won't work. You'll have to convert the PDF to text first. – Amal Murali Oct 12 '13 at 15:56
  • @Amal , Thanks .. but how to convert it in txt file and then find word using grep command. – Chirag Shah Oct 12 '13 at 16:10
  • Did you try [searching](https://www.google.com/search?q=php+pdf+to+text+site%3Astackoverflow.com)? This has been answered many times before. Try one of those solutions and if you can't get it to work, update this question (or post a new question) describing the errors / issues. – Amal Murali Oct 12 '13 at 16:13

2 Answers2

2

You can use pdfgrep see this link How to search contents of multiple pdf files? I have also used it few times and it also supports recursive search.

Thanks & Regards,
Alok Thaker

Community
  • 1
  • 1
linux_fanatic
  • 4,767
  • 3
  • 19
  • 20
2

As Amal Murali wrote in comment, you have to convert PDF content to text. This has been already solved at StackOverflow, ie How to extract text from the PDF document?. Then you can use preg_match_all, or if you really want to use grep, you can use something as proc_open and pass the text through pipe.

So, using class from http://pastebin.com/hRviHKp1 (link from article), here is an example with preg_match_all:

include('class.pdf2text.php');
$a = new PDF2Text();
$a->setFilename('Videographer_RFP.pdf');
$a->decodePDF();
preg_match_all ('some pattern', $a->output(), $matches);
print_r($matches);

Note that I have not tested the code.

Community
  • 1
  • 1
Firielentul
  • 193
  • 1
  • 8