Example #1
0
 /**
  * Method: convertDoc
  * =========================================================================
  * This is where we actually do some converting of docx to pdf.
  * This converter uses the OpenOffice/LibreOffice Headless capabilities.
  *
  * Parameters:
  * -------------------------------------------------------------------------
  *  - $docx: This must be an instance of ```SplFileInfo```
  *           pointing to the document to convert.
  *
  * Returns:
  * -------------------------------------------------------------------------
  * void
  */
 public function convertDoc(TempFile $docx)
 {
     if (!is_executable($this->binary)) {
         throw new RuntimeException('The libreoffice command ("' . $this->binary . '") ' . 'was not found or is not executable by the current user! ');
     }
     // Check to see if the profile dir exists and is writeable
     if (is_dir($this->profile) && !is_writable($this->profile)) {
         throw new RuntimeException('If libreoffice does not have permissions to the User ' . 'Profile directory ("' . $this->profile . '") the conversion ' . 'will fail!');
     }
     // Build the cmd to run
     $cmd = $this->binary . ' ' . '--headless ' . '-env:UserInstallation=file://' . $this->profile . ' ' . '--convert-to pdf:writer_pdf_Export ' . '--outdir "' . $this->output . '" ' . '"' . $docx->getPathname() . '"';
     // Run the command
     $process = $this->process($cmd);
     $process->run();
     // Check for errors
     if (!$process->isSuccessful()) {
         throw new RuntimeException($process->getErrorOutput());
     }
     // Grab the generated pdf
     $pdf = file_get_contents($this->output . '/' . $docx->getBasename('.docx') . '.pdf');
     // Clean up after ourselves
     exec('rm -rf ' . $this->profile);
     // Finally return the generated pdf
     return $pdf;
 }