Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
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.
 
 
 
 

72 lines
1.7 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();
$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();
}
/**
*
* @throws \Exception
*/
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();
}
}