Repo for the search and displace core module including the interface to select files and search and displace operations to run on them.
https://searchanddisplace.com
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<?php
namespace App\SearchDisplace\Convertor;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Exception\ProcessFailedException; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage;
/** * Convert documents from formats supported by Libre Office */
class Convertor { /** * @param $to desired file format * @param $document absolute file path * @param $tmp - if true file will be saved to tmp directory for download * * @return string $path to converted file */ public static function convert($to, $document, $tmp = false) { $path = pathinfo($document); $extension = $path['extension']; $dir = $path['dirname']; $original = $dir . '/' . $path['basename']; if(!$tmp) { $folder = $dir . '/'; } else { $folder = storage_path('app/tmp/'); }
$env = [ 'HOME' => storage_path('app/' . env('LIBREOFFICE_CONFIG_PATH', 'tmp/libreoffice')), ];
if ($extension == 'odt') { $to = $to . ':"${:FILTER}"'; $env['FILTER'] = 'OpenDocument Text Flat XML'; }
Log::info('Running `soffice` to convert "' . $original . '" to "' . $to . '". Output folder: "' . $folder . '"'); Log::info( 'COMMAND: ' . 'soffice ' . '--convert-to ' . $to . ' ' . $original . ' ' . '--outdir ' . $folder );
# We will run the process from a shell command line, which allows us to add parameters
# The "OpenDocument Text Flat XML" parameter contains whitespaces, so we will need to add that as
# a env variable parameter, otherwise the Process class will escape it and it will not work properly.
$process = Process::fromShellCommandline( "soffice --convert-to $to $original --outdir $folder", null, //base_path(),
$env ); $process->run(function ($type, $buffer) { if (Process::ERR === $type) { Log::info("CONVERT ERROR: " . $buffer); } }, $env);
if (!$process->isSuccessful()) { throw new ProcessFailedException($process); }
Storage::deleteDirectory(env('LIBREOFFICE_CONFIG_PATH', 'app/tmp/libreoffice'));
return $path['filename'] . '.' . $to; } }
|