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.
64 lines
1.6 KiB
64 lines
1.6 KiB
<?php
|
|
|
|
namespace App\Ingest;
|
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class DocxConvertor extends AbstractConvertor
|
|
{
|
|
public function execute()
|
|
{
|
|
$this->convertToPdfWithLibreOffice();
|
|
|
|
$pdfFilePath = "$this->directoryPath/document.pdf";
|
|
|
|
if ( ! $this->storage->exists($pdfFilePath)) {
|
|
throw new \Exception('Failed to convert to PDF: ' . $pdfFilePath);
|
|
}
|
|
|
|
$convertor = new PDFConvertor($this->storage, $pdfFilePath);
|
|
|
|
$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();
|
|
}
|
|
|
|
protected function convertToPdfWithLibreOffice()
|
|
{
|
|
$office = new Office();
|
|
|
|
$success = $office->run(
|
|
'pdf',
|
|
$this->storage->path($this->path),
|
|
$this->storage->path($this->directoryPath)
|
|
);
|
|
|
|
if (! $success) {
|
|
throw new \Exception('Failed when converting from DOCX to PDF for file: ' . $this->path);
|
|
}
|
|
|
|
$this->deleteOriginalDocument();
|
|
}
|
|
}
|