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.
117 lines
2.4 KiB
117 lines
2.4 KiB
<?php
|
|
|
|
namespace App\Ingest;
|
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class DocxConvertor extends AbstractConvertor
|
|
{
|
|
/**
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function execute()
|
|
{
|
|
$this->convertToPdfWithLibreOffice();
|
|
|
|
if ( ! $this->storage->exists($this->path)) {
|
|
throw new \Exception('Failed to convert to PDF: ' . $this->path);
|
|
}
|
|
|
|
$convertor = new PDFConvertor($this->storage, $this->path);
|
|
|
|
$convertor->execute();
|
|
}
|
|
|
|
protected function convertToPDF()
|
|
{
|
|
(new Process(['export HOME=' . env('USER_HOME_PATH')]))->run();
|
|
|
|
$process = new Process([
|
|
'unoconv',
|
|
'-f',
|
|
'pdf',
|
|
// '-c=socket,host=localhost,port=' . (2000 + rand(2, 7)) . ';urp;StarOffice.ComponentContext',
|
|
$this->storage->path($this->path),
|
|
]);
|
|
|
|
$process->setTimeout(10);
|
|
|
|
$process->run();
|
|
|
|
if (!$process->isSuccessful()) {
|
|
throw new ProcessFailedException($process);
|
|
}
|
|
|
|
$this->deleteOriginalDocument();
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function convertToPdfWithLibreOffice()
|
|
{
|
|
$this->convertToFormat('pdf');
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function convertToODT()
|
|
{
|
|
$this->convertToFormat('odt');
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function convertToRTF()
|
|
{
|
|
$this->convertToFormat('rtf');
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function convertToDOC()
|
|
{
|
|
$this->convertToFormat('doc');
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function convertToTXT()
|
|
{
|
|
$this->convertToFormat('txt');
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
protected function convertToFormat($format)
|
|
{
|
|
$office = new Office();
|
|
|
|
$success = $office->run(
|
|
$format,
|
|
$this->storage->path($this->path),
|
|
$this->storage->path($this->directoryPath)
|
|
);
|
|
|
|
if (! $success) {
|
|
throw new \Exception('Failed when converting from DOCX to ' . strtoupper($format) . ' for file: ' . $this->path);
|
|
}
|
|
|
|
$this->deleteOriginalDocument();
|
|
|
|
$this->path = "$this->directoryPath/document.$format";
|
|
}
|
|
}
|