[PHP] odtPHP - Uncaught exception 'OdfException' with message 'Error during file export'

Dieses Thema im Forum "Webentwicklung" wurde erstellt von StrikeFreedom, 27. Mai 2010 .

Status des Themas:
Es sind keine weiteren Antworten möglich.
  1. 27. Mai 2010
    odtPHP - Uncaught exception 'OdfException' with message 'Error during file export'

    Ich versuche die odtPHP Bibilothek von http://www.odtphp.com/ einzubinden um Open Office Datein zu erstellen/bearbeiten.

    Leider erscheint immer diese Fehlermeldung:

    Fehlermeldung
    Ich weiß einfach nicht mehr weiter, vieleicht habt ihr eine Idee.

    Schon einmal Danke :]

    index.php
    PHP:
    <? php
    require_once( 'library/odf.php' );

    $odf  = new  odf ( "muster.odt" );


    $odf -> setVars ( 'BETREFF' 'Mit PHP erstellte ODF Datei' );

    $odf -> exportAsAttachedFile ();
    ?>
    odf.php
    Spoiler
    PHP:
    <? php
    require  'zip/PclZipProxy.php' ;
    require 
    'zip/PhpZipProxy.php' ;
    require 
    'Segment.php' ;
    class 
    OdfException  extends  Exception
    {}

    class  Odf
    {
        protected 
    $config  = array(
            
    'ZIP_PROXY'  =>  'PclZipProxy' ,
            
    'DELIMITER_LEFT'  =>  '{' ,
            
    'DELIMITER_RIGHT'  =>  '}' ,
            
    'PATH_TO_TMP'  =>  null
           
    );
        protected 
    $file ;
        protected 
    $contentXml ;
        protected 
    $tmpfile ;
        protected 
    $images  = array();
        protected 
    $vars  = array();
        protected 
    $segments  = array();
        const 
    PIXEL_TO_CM  0.026458333 ;
        

        
    public function  __construct ( $filename $config  = array())
        {
            if (! 
    is_array ( $config )) {
                throw new 
    OdfException ( 'Configuration data must be provided as array' );
            }
            foreach (
    $config  as  $configKey  =>  $configValue ) {
                if (
    array_key_exists ( $configKey $this -> config )) {
                    
    $this -> config [ $configKey ] =  $configValue ;
                }
            }
            if (! 
    class_exists ( $this -> config [ 'ZIP_PROXY' ])) {
                throw new 
    OdfException ( $this -> config [ 'ZIP_PROXY' ] .  ' class not found - check your php settings' );
            }
            
    $zipHandler  $this -> config [ 'ZIP_PROXY' ];
            
    $this -> file  = new  $zipHandler ();
            if (
    $this -> file -> open ( $filename ) !==  true ) {
                throw new 
    OdfException ( "Error while Opening the file ' $filename ' - Check your odt file" );
            }
            if ((
    $this -> contentXml  $this -> file -> getFromName ( 'content.xml' )) ===  false ) {
                throw new 
    OdfException ( "Nothing to parse - check that the content.xml file is correctly formed" );
            }

            
    $this -> file -> close ();
            
            
    $tmp  tempnam ( $this -> config [ 'PATH_TO_TMP' ],  md5 ( uniqid ()));
            
    copy ( $filename $tmp );
            
    $this -> tmpfile  $tmp ;
            
    $this -> _moveRowSegments ();
        }
        

        
    public function  setVars ( $key $value $encode  true $charset  'ISO-8859' )
        {
            if (
    strpos ( $this -> contentXml $this -> config [ 'DELIMITER_LEFT' ] .  $key  $this -> config [ 'DELIMITER_RIGHT' ]) ===  false ) {
                throw new 
    OdfException ( "var  $key  not found in the document" );
            }
            
    $value  $encode  htmlspecialchars ( $value ) :  $value ;
            
    $value  = ( $charset  ==  'ISO-8859' ) ?  utf8_encode ( $value ) :  $value ;
            
    $this -> vars [ $this -> config [ 'DELIMITER_LEFT' ] .  $key  $this -> config [ 'DELIMITER_RIGHT' ]] =  str_replace ( "\n" "<text:line-break/>" $value );
            return 
    $this ;
        }
        

        
    public function  setImage ( $key $value )
        {
            
    $filename  strtok ( strrchr ( $value '/' ),  '/.' );
            
    $file  substr ( strrchr ( $value '/' ),  1 );
            
    $size  = @ getimagesize ( $value );
            if (
    $size  ===  false ) {
                throw new 
    OdfException ( "Invalid image" );
            }
            list (
    $width $height ) =  $size ;
            
    $width  *=  self :: PIXEL_TO_CM ;
            
    $height  *=  self :: PIXEL_TO_CM ;
            
    $xml  = <<<IMG
    <draw:frame draw:style-name="fr1" draw:name=" $filename " text:anchor-type="char" svg:width=" { $width } cm" svg:height=" { $height } cm" draw:z-index="3"><draw:image xlink:href="Pictures/ $file " xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/></draw:frame>
    IMG;
            
    $this -> images [ $value ] =  $file ;
            
    $this -> setVars ( $key $xml false );
            return 
    $this ;
        }
        

        
    private function  _parse ()
        {
            
    $this -> contentXml  str_replace ( array_keys ( $this -> vars ),  array_values ( $this -> vars ),  $this -> contentXml );
        }
        

        
    public function  mergeSegment ( Segment $segment )
        {
            if (! 
    array_key_exists ( $segment -> getName (),  $this -> segments )) {
                throw new 
    OdfException ( $segment -> getName () .  'cannot be parsed, has it been set yet ?' );
            }
            
    $string  $segment -> getName ();
            
    // $reg = '@<text:p[^>]*>\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]<\/text:p>@smU';
            
    $reg  '@\[!--\sBEGIN\s'  $string  '\s--\](.*)\[!--.+END\s'  $string  '\s--\]@smU' ;
            
    $this -> contentXml  preg_replace ( $reg $segment -> getXmlParsed (),  $this -> contentXml );
            return 
    $this ;
        }
        

        
    public function  printVars ()
        {
            return 
    print_r ( '<pre>'  print_r ( $this -> vars true ) .  '</pre>' true );
        }
        

        
    public function  __toString ()
        {
            return 
    $this -> contentXml ;
        }
        

        
    public function  printDeclaredSegments ()
        {
            return 
    '<pre>'  print_r ( implode ( ' ' array_keys ( $this -> segments )),  true ) .  '</pre>' ;
        }
        

        
    public function  setSegment ( $segment )
        {
            if (
    array_key_exists ( $segment $this -> segments )) {
                return 
    $this -> segments [ $segment ];
            }
            
    // $reg = "#\[!--\sBEGIN\s$segment\s--\]<\/text:p>(.*)<text:p\s.*>\[!--\sEND\s$segment\s--\]#sm";
            
    $reg  "#\[!--\sBEGIN\s $segment \s--\](.*)\[!--\sEND\s $segment \s--\]#sm" ;
            if (
    preg_match ( $reg html_entity_decode ( $this -> contentXml ),  $m ) ==  0 ) {
                throw new 
    OdfException ( "' $segment ' segment not found in the document" );
            }
            
    $this -> segments [ $segment ] = new  Segment ( $segment $m [ 1 ],  $this );
            return 
    $this -> segments [ $segment ];
        }
        

        
    public function  saveToDisk ( $file  null )
        {
            if (
    $file  !==  null  &&  is_string ( $file )) {
                if (
    file_exists ( $file ) && !( is_file ( $file ) &&  is_writable ( $file ))) {
                    throw new 
    OdfException ( 'Permission denied : can\'t create '  $file );
                }
                
    $this -> _save ();
                
    copy ( $this -> tmpfile $file );     
            } else {
                
    $this -> _save ();
            }
        }
        

        
    private function  _save ()
        {
            
    $this -> file -> open ( $this -> tmpfile );
            
    $this -> _parse ();
            if (! 
    $this -> file -> addFromString ( 'content.xml' $this -> contentXml )) {
               throw new 
    OdfException ( 'Error during file export' );
            }
            foreach (
    $this -> images  as  $imageKey  =>  $imageValue ) {
                
    $this -> file -> addFile ( $imageKey 'Pictures/'  $imageValue );
            }
            
    $this -> file -> close ();  // seems to bug on windows CLI sometimes
        
    }
        

        
    public function  exportAsAttachedFile ( $name = "" )
        {
            
    $this -> _save ();
            if (
    headers_sent ( $filename $linenum )) {
                throw new 
    OdfException ( "headers already sent ( $filename  at  $linenum )" );
            }
            
            if( 
    $name  ==  ""  )
            {
                    
    $name  md5 ( uniqid ()) .  ".odt" ;
            }
            
            
    header ( 'Content-type: application/vnd.oasis.opendocument.text' );
            
    header ( 'Content-Disposition: attachment; filename="' . $name . '"' );
            
    readfile ( $this -> tmpfile );
        }
        

        
    public function  getConfig ( $configKey )
        {
            if (
    array_key_exists ( $configKey $this -> config )) {
                return 
    $this -> config [ $configKey ];
            }
            return 
    false ;
        }
        

        
    public function  getTmpfile ()
        {
            return 
    $this -> tmpfile ;
        }
        
    /**
         * Delete the temporary file when the object is destroyed
         */    
        
    public function  __destruct () {
              if (
    file_exists ( $this -> tmpfile )) {
                
    unlink ( $this -> tmpfile );
            }
        }
    }

    ?>


    edit://

    PhpZipProxy.php
    Spoiler
    PHP:
    <? php
    require_once  'ZipInterface.php' ;
    class 
    PhpZipProxyException  extends  Exception
    { }


    class  PhpZipProxy  implements  ZipInterface
    {
        protected 
    $zipArchive ;
        protected 
    $filename ;
        

        
    public function  addFile ( $filename $localname  null )
        {
            if ((
    file_exists ( $this -> filename ) && ! is_writable ( $this -> filename ))
                || !
    file_exists ( $filename )) {
                return 
    false ;
            }
            return 
    $this -> zipArchive -> addFile ( $filename $localname );
        }
        
    /**
         * Close the Zip archive
         * @return true
         */    
        
    public function  close ()
        {
            return 
    $this -> zipArchive -> close ();
        }
    }
    ?>
     
  2. 27. Mai 2010
    AW: odtPHP - Uncaught exception 'OdfException' with message 'Error during file export'

    der fehler passiert in der datei "zip/PhpZipProxy.php"
     
  3. 27. Mai 2010
    AW: odtPHP - Uncaught exception 'OdfException' with message 'Error during file export'

    Ich bin begeistert, ich probier 3 Tage lang rum bis ich ein Thread auf mach und dann find ich den Fehler nach 5 Minuten.

    meine neue index.php mit der es geht:

    PHP:

    <?php

    require_once( 'library/odf.php' );

    $config  = array(
            
    'ZIP_PROXY'  =>  'PclZipProxy' ,
            
    'DELIMITER_LEFT'  =>  '{' ,
            
    'DELIMITER_RIGHT'  =>  '}' ,
            
    'PATH_TO_TMP'  =>  '/temp'
           
    );

    $odf  = new  odf ( "muster.odt" , $config );


    $odf -> setVars ( 'BETREFF' 'Mit PHP erstellte ODF Datei' );

    $odf -> exportAsAttachedFile ();
    ?> 

     
  4. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.