Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shell_exec not working not able to convert pdftotext

I am trying to convert a pdf file into text file. When I run the command through terminal its working fine but when try to execute it through PHP it's not working.

I am stuck in this situation from last four hour spend lots of time in google but no solution available. Can any body resolve this issue?

File owner - nobody

shell_exec('/usr/bin/pdftotext /opt/lampp/htdocs/foldername/filename.pdf');

Can anyone provide any helpful solution?

I also tried to change usr folder ownership from root to nobody and provide 777 permission on folder and its context.

like image 583
Ajay Kadyan Avatar asked Dec 27 '22 10:12

Ajay Kadyan


2 Answers

Your command to run pdftotext is not correct.

There should be a second argument telling pdftotext to write to a specific file or just use a dash "-" to write to stdout, unless you actually want the program to create a text-file with the filename as the pdf (this would require write permissions in the /opt/lampp/.../ folder)

This is at least true for pdftotext version 0.12.4

"Pdftotext reads the PDF file, PDF-file, and writes a text file, text-file. If text-file is not specified, pdftotext converts file.pdf to file.txt. If text-file is ´-', the text is sent to stdout."

So, the solution to your question would simply be add a dash after the filename, like so:

<?php
$pdftext = shell_exec('/usr/bin/pdftotext /opt/lampp/htdocs/foldername/filename.pdf -');
echo $pdftext;

Provided that the binary exists and PHP is allowed to use shell_exec and you have permissions and that the pdf-file exists and you have permissions.

like image 101
hank Avatar answered Dec 30 '22 10:12

hank


from how to test if PHP system() function is allowed? and not turned off for security reasons

function isAvailable($func) {
    if (ini_get('safe_mode')) return false;
    $disabled = ini_get('disable_functions');
    if ($disabled) {
        $disabled = explode(',', $disabled);
        $disabled = array_map('trim', $disabled);
        return !in_array($func, $disabled);
    }
    return true;
}

You may need to check if isAvailable('shell_exec') On shared hosting this function might be disabled.

If it's not disabled, check the Apache log, it's all you can do.

like image 33
vectorialpx Avatar answered Dec 30 '22 10:12

vectorialpx