2

I'm trying to convert HTML file into RTF file using unoconv in php.

From php I'm calling :


    ......
    file_put_contents("/tmp/unoconv55b2862fea753.html", $content);
    $command = "unoconv -f rtf -o /tmp/unoconv55b2862fea753.rtf /tmp/unoconv55b2862fea753.html";
    exec($command, $output);
    $converted = file_get_contents("/tmp/unoconv55b2862fea753.rtf");

the problem is, that file_put content will save .html, but unoconv for some reason doesn't save converted file into /tmp directory.

When I run that $command directly on server in console, converted file was created.

Do you have any idea where could be problem?

bytecode77
  • 14,163
  • 30
  • 110
  • 141
jalco
  • 49
  • 2
  • 6
  • use exec()'s 3rd arg to capture unoconv's exit value, and see what happened... you're just assuming it's actually working. – Marc B Jul 24 '15 at 19:34
  • What is in `$output` after that call? – ThatOneDude Jul 24 '15 at 19:40
  • output is: array(0) { }, and the return_var is: 251 – jalco Jul 24 '15 at 19:52
  • Use the full path to unoconv – ThatOneDude Jul 24 '15 at 20:03
  • 1
    after setting fullpath to unoconv result is the same :/ from cmd its working, from php exec() not – jalco Jul 24 '15 at 20:15
  • I fear we are getting into chat territory with an extended discussion but I might prefix "echo" to `$command` and verify I get what I'm expecting in `$output`. – ThatOneDude Jul 24 '15 at 20:40
  • I don't know if I understand your request :) but if you asking me if the command is correct, than, I think yes, as when I echo it, I'll get command, which when I run in cmd at server, converted file is created – jalco Jul 24 '15 at 20:55

2 Answers2

0

The problem could lie with PHP not having the right privileges for the tmp directory. Try saving the converted file to the root of your site (or a subfolder within it) and see if it converts successfully.

  • I don't think that could be problem as that /tmp folder i got using sys_get_temp_dir(); second reason why i don't think that is problem is, that file_put_contents() works fine, and file is saved there. just that execution of command using exec() function doesn't work :/ – jalco Jul 24 '15 at 19:39
  • Believe me, just because you don't think it could be a problem doesn't mean it won't be. I've spent far too many hours thinking "It couldn't be that...". Remember that PHP is its own user on the server, and that user is running the unoconv command. All else fails, I agree with Marc B (above) –  Jul 24 '15 at 19:42
  • got same result. I created rwxrwxrwx folder directly in root folder of site. tryed to save that generated file to that folder using exec(). folder was empty. After that I executed command directly from console and generated file was created there – jalco Jul 24 '15 at 20:05
-1

When configuring unoconv on ubuntu server to be run under apache (www-data) user This solution is based on:
https://github.com/dagwieers/unoconv/issues/87#issuecomment-16563550
and
https://github.com/dagwieers/unoconv/issues/87#issuecomment-32084464

Step-by-step guide
Follow these steps:
1. Create new file which will contain sudoers entry for user www-data: sudo vi /etc/sudoers.d/www-data
2. Add following content to newly created file and save: www-data ALL=NOPASSWD: /usr/bin/unoconv
3. In PHP exec() use "sudo unoconv"

jalco
  • 49
  • 2
  • 6
  • This is one of the most crazy answers I have seen on SO. Have you lost your mind? Telling people that their www-data user should become sudo and not require a password to do admin stuff? – Richard Mar 02 '17 at 12:19