Created
January 22, 2013 15:13
-
-
Save mmenozzi/4595355 to your computer and use it in GitHub Desktop.
OpenOffice (unoconv) Word to PDF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param $sourceFile | |
* @param $destinationFilePath | |
* @throws \RuntimeException | |
* @throws \Exception | |
*/ | |
private function doConvert($sourceFile, $destinationFilePath) | |
{ | |
$output = array(); | |
exec("unoconv --output=\"" . $destinationFilePath . "\" \"" . $sourceFile . "\"", $output); | |
if (!file_exists($destinationFilePath)) { | |
$manualInvestigationFile = $this->backupDir . DIRECTORY_SEPARATOR . basename($destinationFilePath); | |
copy($sourceFile, $manualInvestigationFile); | |
@unlink($sourceFile); | |
throw new \RuntimeException( | |
sprintf( | |
"OpenDocument to PDF conversion failed. Maybe wrong file type? " . | |
"Source file: %s. Destination file: %s Unoconv output: %s. " . | |
"Source file has been saved for manual investigation in: %s", | |
$sourceFile, | |
$destinationFilePath, | |
implode(" / ", $output), | |
$manualInvestigationFile | |
) | |
); | |
} | |
@unlink($sourceFile); | |
$this->fixDestinationPath($destinationFilePath); | |
} | |
/** | |
* Corregge il percorso di destinazione del PDF generato | |
* | |
* Potrebbe essere che venga creata una cartella invece che un file | |
* singolo. In questo caso rinomina il file e lo sostituisce alla cartella | |
* | |
* @param $destinationFilePath | |
* @throws \Exception | |
*/ | |
private function fixDestinationPath($destinationFilePath) | |
{ | |
if (is_dir($destinationFilePath)) { | |
$destinationFilePath = realpath($destinationFilePath); | |
$uniqueTempPath = $destinationFilePath . uniqid(); | |
rename($destinationFilePath, $uniqueTempPath); | |
$this->move($uniqueTempPath, $destinationFilePath); | |
rmdir($uniqueTempPath); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment