alchemy-fr / PHP-Unoconv

Other
73 stars 36 forks source link

is there a way to use this as a vendor in my cakephp? #1

Closed simkimsia closed 11 years ago

simkimsia commented 11 years ago

Hi there,

I am having some problems using exec to call the unoconv. Even though I have no problems running it as www-data in my terminal.

So I am interested to implement your library.

Can you give some documentations as to how to use this in a cakephp app? Or even a pure php script?

Thank you.

romainneutron commented 11 years ago

Hello,

as mentioned in the README, there is one method to use Unoconv Unoconv\Unoconv::transcode :

// $logger is a Monolog\Logger instance
$unoconv = Unoconv\Unoconv::create($logger);
$unoconv->transcode('document.docx', 'pdf', 'document.pdf');

So, to use unoconv in your project, the easiest is to install it through composer, I've added a doc that was missing about this in the README :

{
    "require": {
        "php-unoconv/php-unoconv": "~0.2"
    }
}

You do not have to use exec as the underlying process is handled in library itself.

If you want to generate jpegs from office document, consider transcode the office doc to PDF first, then render the PDF to jpeg using Ghostscript-PHP.

Hope that helped !

simkimsia commented 11 years ago

Sadly, it did not work for me.

I got the following.

screen shot 2013-05-31 at 12 18 49 pm

Any suggestions?

romainneutron commented 11 years ago

It seems Unoconv is not detected. Can you provide the snippet of code where you invoke unoconv ?

simkimsia commented 11 years ago

It says line 91 of your Unoconv.php was unable to detect unoconv.

My php code to call your Unoconv/Unoconv was

 public function convertExcel() {
    $unoconv = Unoconv\Unoconv::create();
    $folderPath = $this->outputFilesPath . 'Excel' . DS . $this->quotation['id'] . DS;
    $inputFilePath =  $folderPath . $this->quotation['name'] . '.xlsx';
    $outputFilePath = $folderPath . $this->quotation['name'] . '.pdf';
    return $unoconv->transcode($inputFilePath, 'pdf', $outputFilePath);
}
romainneutron commented 11 years ago

Actually, the unoconv detection does not work, you can specify the binary you want to use with this :

$unoconv = Unoconv\Unoconv::load('/path/to/your/unoconv-binary');

instead of

$unoconv = Unoconv\Unoconv::create();
simkimsia commented 11 years ago

I now get a Unoconv failed to transcode file.

How do I troubleshoot this?

By the way, my code now is:

public function convertExcel() {

    $unoconv = Unoconv\Unoconv::load('/usr/bin/unoconv');
    $folderPath = $this->outputFilesPath . 'Excel' . DS . $this->quotation['id'] . DS;
    $inputFilePath =  $folderPath . $this->quotation['name'] . '.xlsx';
    $outputFilePath = $folderPath . $this->quotation['name'] . '.pdf';
    return $unoconv->transcode($inputFilePath, 'pdf', $outputFilePath);
}
romainneutron commented 11 years ago

The best way to debug is to add a logger :

    $logger = new Monolog\Logger('unoconv');
    $logger->pushHandler(new Monolog\Handler\StreamHandler('php://stdout'));
    $unoconv = Unoconv\Unoconv::load('/usr/bin/unoconv');

you will have an output of what's executed.

simkimsia commented 11 years ago

I tried that. I cannot find the output anywhere.

I also changed php://stdout to a log file, but nothing got written into the log file.

romainneutron commented 11 years ago

You have to provide code / stacktrace so I can help you

simkimsia commented 11 years ago

I realized why.

I needed to change your code to

$logger = new Monolog\Logger('unoconv');
$logger->pushHandler(new Monolog\Handler\StreamHandler('php://stdout'));
$unoconv = Unoconv\Unoconv::load('/usr/bin/unoconv', **$logger**);

Anyway, my output is:

[2013-05-31 08:26:52] unoconv.INFO: Unoconv running command '/usr/bin/unoconv' '--format=pdf' '--stdout' '/var/virtual/storyzer.com/cake-json/ltequotationapp/webroot/outputfiles/Excel/1/Quotation 1.xlsx' [] [] [2013-05-31 08:26:52] unoconv.ERROR: Unoconv failed to execute command '/usr/bin/unoconv' '--format=pdf' '--stdout' '/var/virtual/storyzer.com/cake-json/ltequotationapp/webroot/outputfiles/Excel/1/Quotation 1.xlsx' [] []

In case, it has to do with the extra space in the filename. I tried the following file dsadas.xlsx as well. I got the following.

[2013-05-31 08:29:00] unoconv.INFO: Unoconv running command '/usr/bin/unoconv' '--format=pdf' '--stdout' '/var/virtual/storyzer.com/cake-json/ltequotationapp/webroot/outputfiles/Excel/2/dsadas.xlsx' [] [] [2013-05-31 08:29:00] unoconv.ERROR: Unoconv failed to execute command '/usr/bin/unoconv' '--format=pdf' '--stdout' '/var/virtual/storyzer.com/cake-json/ltequotationapp/webroot/outputfiles/Excel/2/dsadas.xlsx' [] []

romainneutron commented 11 years ago

It's either a $_ENV issue (missing paths) or an unoconv issue

romainneutron commented 11 years ago

I'd rather think it's a path issue

simkimsia commented 11 years ago

Hi @romainneutron ,

what do I do to resolve this $_ENV issue?

I have tried to run the same command while in terminal, and it worked.

romainneutron commented 11 years ago

Are you using nginx ?

romainneutron commented 11 years ago

I mean, are you using php-fpm ?

simkimsia commented 11 years ago

Yes I am using php-fpm AND nginx.

How did you know that?

simkimsia commented 11 years ago

Hi @romainneutron

I have logged into terminal as www-data and echo'd the $PATH.

I then subsequently copied the values and wrote a putenv('PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/node/bin'); just before I ran the unoconv->transcode

It works now.

I have no idea why this is the case.

In addition, I am using nginx and php-fpm. I wonder if this has anything to do to affect this behavior?

romainneutron commented 11 years ago

Nginx does not include path environment variable by default. You have to modify your Nginx fastcgi_params config file like :

fastcgi_param    PATH    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/node/bin

then you can remove the PHP putenv call.

romainneutron commented 11 years ago

I've added a binary detection troubleshooting here : https://github.com/alchemy-fr/BinaryDriver/blob/master/README.md#binary-detection-troubleshooting

simkimsia commented 11 years ago

@romainneutron between adding the putenv into my php script and modifying the Nginx config file which is more ideal?

simkimsia commented 11 years ago

By the way, the reason i found out about the PATH issue was because I executed unoconv without using php-unoconv and then i ran this instruction.

https://github.com/dagwieers/unoconv/issues/87#issuecomment-18800070

This allowed me to output to a output.txt file.

This was really helpful as it helped me to realise the issue was the PATH.

Is there a way your code can similarly produce the same debug statement somehow?

Either like a output.txt or as a log using Monolog.

romainneutron commented 11 years ago

There is only one method in PHP-Unoconv : \Unoconv\Unoconv::transcode.

srraul94 commented 12 months ago

Nginx does not include path environment variable by default. You have to modify your Nginx fastcgi_params config file like :

fastcgi_param    PATH    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/node/bin

then you can remove the PHP putenv call.

This fixed the problem with unoconv on Apple Silicon (M1/M2) and Laravel Valet. Is important add the hombrew path (export PATH=/opt/homebrew/bin:$PATH)