www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: [RESOLVED] shell_exec and pdftotext does not work?

  1. #1
    Join Date
    Jan 2003
    Location
    Egypt
    Posts
    185

    resolved [RESOLVED] shell_exec and pdftotext does not work?

    Hi,
    I don't know why the following does not work:
    PHP Code:
    echo shell_exec('/usr/bin/pdftotext'); 
    It does not output anything, where in terminal it outputs the usage of pdftotext. On the other hand, some command such as dir give output correctly.

    The path of pdftotext is correct because I got it by the command "which". The PHP I have is XAMPP on Ubuntu 11.10.

    Another thing, If I inserted wrong command, it does not output any error message too!
    Regards();
    return Said Bakr
    My Blog - Quran

  2. #2
    Join Date
    Mar 2011
    Posts
    1,133
    I believe you need to pass a filename and path to pdftotext. Your code just causes the program to run without any input data to convert. It should probably be something like:
    PHP Code:
    echo shell_exec('/usr/bin/pdftotext path/filename.pdf'); 
    Rick Trethewey
    Rainbo Design

  3. #3
    Join Date
    Jan 2003
    Location
    Egypt
    Posts
    185
    Unfortunately, this does not solve the problem.
    Regards();
    return Said Bakr
    My Blog - Quran

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    I use pdftotext on a local server with this function
    PHP Code:
    function pdfTxt($path){
      
    $o=shell_exec('pdftotext -enc UTF-8 '.$path.' pdf.txt');
        
    $c=file_get_contents('pdf.txt');
    // Some corrections depending on your pdfs
        
    return preg_replace("@\x0D\x0A\x0D\x0A\.\x0D\x0A\x0D\x0A\.\x0D\x0A\x0D\x0A[^\x0D]+\x0D\x0A\x0D\x0A[^\x0D]+\x0D\x0A\x0D\x0A[^\x0D]+\x0D\x0A\x0D\x0A@"," ",$c);

    pdftotext is in same directory as the running php file, $path is the pdf file relative path. Without pdf.txt the new txt file take the name of your file.
    Download it on this page and read the pdftotext.txt file for other options...
    Good luck !
    Last edited by 007Julien; 12-04-2011 at 08:49 AM.

  5. #5
    Join Date
    Jan 2003
    Location
    Egypt
    Posts
    185
    Quote Originally Posted by 007Julien View Post
    I use pdftotext on a local server with this
    pdftotext is in same directory as the running php file, $path is the pdf file relative path. Without pdf.txt the new txt file take the name of your file.
    Download it on this page and read the pdftotext.txt file for other options...

    Good luck !
    I may misunderstand you. I already have on my local Linux machine pdftotext and it run from terminal. As I think it is belong to package called poppler.

    Do you mean that xpdf is another package than poppler and it do its functionality?
    Regards();
    return Said Bakr
    My Blog - Quran

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264

    It is better to talk to the organ-grinder than to his monkey !

    I work on a local server with windows. I have fund pdftotext.exe in the package xpdfbin-win-3.03.zip with other files like pdftoppm.exe, pdfimages.exe, ... and her descriptions, options in pdftotext.txt, pdftoppm.txt, pdfimages.txt ...

    I do not know poppler but read on this page that Poppler is a PDF rendering library based on the xpdf-3.0 code base.

  7. #7
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    In bash/"the terminal" there are things called file pointers. When you run any given command, there are at least THREE file pointers, called 1 2 3.

    1 is standard output "STDOUT"
    2 is error output "STDERR"
    3 is standard input "STDIN"

    When you run this:
    PHP Code:
    $o=shell_exec('pdftotext -enc UTF-8 '.$path.' pdf.txt'); 
    you are getting ONLY standard output. But what if there is no standard output?

    Maybe you have an ERROR. And you really want to be looking at both STDOUT + STDERR.

    You can redirect the pointer for STDERR to STDOUT by adding this to your bash/sh command:
    2>&1
    PHP Code:
    $o=shell_exec('pdftotext -enc UTF-8 '.$path.' pdf.txt 2>&1'); 
    Now when you run this:

    PHP Code:
    $path 'asd9ajs09dj039jadad'//doesn't exist
    echo shell_exec('pdftotext -enc UTF-8 '.$path.' pdf.txt 2>&1'); 
    Code:
    pdf2text: command not found
    But, for you it will probably be something like
    Could not open input file: asd9ajs09dj039jadad

    Taken from my own code:
    PHP Code:
    $cmdStr CONFIG::PATH_TO_PYTHON2.' '.CONFIG::PATH_TO_PYTHON_PARSER.' '.$file->getDiskPath().' 2>&1'//also send stderr to stdout
        
    exec($cmdStr$buffer); //send command to terminal, capture output in a buffer 


    You can also make your own custom file pointers or perhaps you are familiar with "piping" standard output into another programs standard input:


    echo ASDF_ASDF_ASDF_ASDF | tr 'S' "D"
    outputs:
    ADDF_ADDF_ADDF_ADDF

    echo ASDF_ASDF_ASDF_ASDF | tr 'S' "D" | tr 'D' "F"
    outputs:
    AFFF_AFFF_AFFF_AFFF

    hope this helps some
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

  8. #8
    Join Date
    Jan 2003
    Location
    Egypt
    Posts
    185
    Thank you for your valuable hint.
    Now I able to see the error:
    PHP Code:
    /usr/bin/pdftotext: /opt/lampp/lib/libjpeg.so.62no version information available (required by /usr/lib/libpoppler.so.13) /usr/bin/pdftotext: /opt/lampp/lib/libgcc_s.so.1version `GCC_4.2.0' not found (required by /usr/lib/i386-linux-gnu/libstdc++.so.6) 
    I think it's something related to XAMPP for linux, If you know how to solve it, I will be appreciated.
    Regards();
    return Said Bakr
    My Blog - Quran

  9. #9
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    I am not entirely sure what .so is, but I can tell you that the error is complaining about GCC missing- which is REALLY ODD considering you're running a program calld pdf2text and not compiling anything.

    I would send the error to the system admin, it doesn't look like something you can fix without being root.

    GCC is a `C` compiler
    PHP is written in C also (it's a programming language).

    Edit:
    but first, make sure you can't run the actual program pdf2text from a real shell. Use putty if you're on windows, otherwise open a terminal and use ssh user@host.com or just man ssh and it will explain it.
    Last edited by eval(BadCode); 12-05-2011 at 04:04 AM.
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

  10. #10
    Join Date
    Jan 2003
    Location
    Egypt
    Posts
    185
    pdftotext is running well from shell (Terminal).
    Regards();
    return Said Bakr
    My Blog - Quran

  11. #11
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by said_fox View Post
    pdftotext is running well from shell (Terminal).
    Have you tried running it as the Apache/www-data/php user (whichever your system has configured? It is possible that the user running the php doesn't have the same access as your user account.

  12. #12
    Join Date
    Jan 2003
    Location
    Egypt
    Posts
    185
    Quote Originally Posted by criterion9 View Post
    Have you tried running it as the Apache/www-data/php user (whichever your system has configured? It is possible that the user running the php doesn't have the same access as your user account.
    I think that the problem is not related to the access, the error message shows that it is about something missing. However, how could I know the user that running PHP?
    Regards();
    return Said Bakr
    My Blog - Quran

  13. #13
    Join Date
    Jul 2010
    Location
    /ramdisk/
    Posts
    865
    PHP Code:
    exec("whoami",$buffer);
    echo 
    implode($buffer); 
    I use (, ; : -) as I please- instead of learning the English language specification: I decided to learn Scheme and Java;

  14. #14
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by said_fox View Post
    I think that the problem is not related to the access, the error message shows that it is about something missing. However, how could I know the user that running PHP?
    Is is possible that your user account has different classpath (or whatever it is called in your environment) than the account that runs PHP.

  15. #15
    Join Date
    Jan 2003
    Location
    Egypt
    Posts
    185
    Quote Originally Posted by criterion9 View Post
    Is is possible that your user account has different classpath (or whatever it is called in your environment) than the account that runs PHP.
    How could I determine this and how could I fix it?
    Regards();
    return Said Bakr
    My Blog - Quran

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles