views:

3062

answers:

2

There's this program, pdftotext, that can convert a pdf file to a text file. To use it directly on the linux console:

pdftotext file.pdf

and it will generate a file.txt on the same directory as the pdf file. I was looking for a way to do it from inside a php program, and after some googling i ended with two commands that should work for me: system() and exec(). So i made a php file with this program:

<?php
    system('pdftotext file.pdf');
?>

But when i run it code, it doesn't work. No txt file is created. So i tried to create a test file with another command:

<?php
    system('touch test.txt');
?>

This worked fine. I've also used exec() and the results were the same. Why doesn't it work?

EDIT: following RoBorg advice, i added the 2>&1 argument to the command, so:

<?php
    system('pdftotext file.pdf 2>&1');
?>

it printed a error message:

pdftotext: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory

seems like something is missing on the server.

A: 

PHP has a build in PDF function library, that should be able to give you what you need:
http://nl3.php.net/pdf

Pim Jager
Seems like that library is mainly for outputting pdf. What i need is the other way around
David McDavidson
+3  A: 

It's probably a permissions issue, but try this instead:

<?php
    system('pdftotext file.pdf 2>&1');
?>

The 2>&1 redirects stderr to stdout, so any error messages will be printed. It should be pretty easy to fix from then on.

Greg
it printed a error message"pdftotext: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory "since i dont have root access to the server, i guess there's nothing i can do.
David McDavidson