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

  1. <?php
  2. namespace App\Ingest;
  3. use Symfony\Component\Process\Exception\ProcessFailedException;
  4. use Symfony\Component\Process\Process;
  5. class DocxConvertor extends AbstractConvertor
  6. {
  7. /**
  8. *
  9. * @throws \Exception
  10. */
  11. public function execute()
  12. {
  13. $this->convertToPdfWithLibreOffice();
  14. $pdfFilePath = "$this->directoryPath/document.pdf";
  15. if ( ! $this->storage->exists($pdfFilePath)) {
  16. throw new \Exception('Failed to convert to PDF: ' . $pdfFilePath);
  17. }
  18. $convertor = new PDFConvertor($this->storage, $pdfFilePath);
  19. $convertor->execute();
  20. }
  21. protected function convertToPDF()
  22. {
  23. (new Process(['export HOME=' . env('USER_HOME_PATH')]))->run();
  24. $process = new Process([
  25. 'unoconv',
  26. '-f',
  27. 'pdf',
  28. // '-c=socket,host=localhost,port=' . (2000 + rand(2, 7)) . ';urp;StarOffice.ComponentContext',
  29. $this->storage->path($this->path),
  30. ]);
  31. $process->setTimeout(10);
  32. $process->run();
  33. if (!$process->isSuccessful()) {
  34. throw new ProcessFailedException($process);
  35. }
  36. $this->deleteOriginalDocument();
  37. }
  38. /**
  39. *
  40. * @throws \Exception
  41. */
  42. protected function convertToPdfWithLibreOffice()
  43. {
  44. $office = new Office();
  45. $success = $office->run(
  46. 'pdf',
  47. $this->storage->path($this->path),
  48. $this->storage->path($this->directoryPath)
  49. );
  50. if (! $success) {
  51. throw new \Exception('Failed when converting from DOCX to PDF for file: ' . $this->path);
  52. }
  53. $this->deleteOriginalDocument();
  54. }
  55. }