Commit 32912f15 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Autoconvertion des fichiers si on lit du word ou du Excell

parent b01d8da7
Loading
Loading
Loading
Loading
Loading
+33 −9
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ class Document
    /**
     * @var string
     */
    private $convCommand = 'unoconv --server=:host -f pdf -o :outputFile :inputFile';
    private $convCommand = 'unoconv --server=:host -f :format -o :outputFile :inputFile';

    /**
     * @var string
@@ -608,6 +608,17 @@ class Document
            throw new Exception('OpenDocument file "' . $fileName . '" doesn\'t exists.');
        }

        $mimetype = mime_content_type($fileName);
        /* autoconvertion en OpenDocument */
        if (str_contains($mimetype, 'openxmlformats') && str_contains($mimetype, 'spreadsheetml')){
            $fileName = $this->convert($fileName, 'ods');
            $duplicate = false;
        }
        if (str_contains($mimetype, 'openxmlformats') && str_contains($mimetype, 'wordprocessingml')){
            $fileName = $this->convert($fileName, 'odt');
            $duplicate = false;
        }

        if ($duplicate) {
            $odtFile = $this->tempFileName('odtFile_', 'odt');
            copy($fileName, $odtFile);
@@ -666,7 +677,7 @@ class Document
            if (!$filename) {
                $filename = $this->tempFileName('odt2pdf_', 'pdf');
            }
            $this->odtToPdf($actualFile, $filename);
            $this->toPdf($actualFile, $filename);
            $actualFile = $filename;
        }else{
            if ($filename && $filename != $actualFile){
@@ -686,30 +697,43 @@ class Document
     * @param $origine
     * @param $destination
     *
     * @return Document
     * @return string
     * @throws Exception
     */
    public function odtToPdf(string $origine, string $destination): Document
    public function toPdf(string $origine, string $destination=null): string
    {
        return $this->convert($origine, 'pdf', $destination);
    }



    public function convert(string $origine, string $format, ?string $destination=null): string
    {
        if (!file_exists($origine)){
            throw new Exception("File \"$origine\" not reachable or writable.");
        }

        if (!$destination){
            $destination = $this->tempFileName(null, $format);
        }

        if (!is_writable(dirname($destination))) {
            throw new Exception('Impossible to create or update the document. Directory "'.dirname($destination).'" not reachable or writable');
        }

        $command = $this->getConvCommand();

        $command = str_replace(':host', $this->getHost(), $this->getConvCommand());
        $command = str_replace(':host', $this->getHost(), $command);
        $command = str_replace(':format', $format, $command);
        $command = str_replace(':inputFile', $origine, $command);
        $command = str_replace(':outputFile', $destination, $command);
        exec($command, $output, $return);

        if (0 != $return) {
            throw new \Exception('La conversion de document en PDF a échoué');
            throw new \Exception('La conversion de document en '.$format.' a échoué');
        }

        return $this;
        return $destination;
    }


@@ -731,7 +755,7 @@ class Document

    public function saveToPdf(string $filename)
    {
        $this->odtToPdf($this->getArchive()->filename, $filename);
        $this->toPdf($this->getArchive()->filename, $filename);

        return $this;
    }
@@ -811,7 +835,7 @@ class Document
     *
     * @return string
     */
    private function tempFileName($prefix = null, $ext = 'odt'): string
    private function tempFileName(?string $prefix = null, string $ext = 'odt'): string
    {
        $tmpDir = $this->getTmpDir();
        if ('/' != substr($tmpDir, -1)) {