Salaktarus
Salaktarus

Reputation: 167

How i display image in pdf file using dompdf in cakephp 2

i using dompdf for cakephp to convert html to pdf, but the problem all image dosen't display, how i display image? this is the

Router::parseExtensions('pdf');

and this is the AppController

public $components = array('RequestHandler');

this is app/View/Layouts/pdf/admin.ctp :

<?php  
    require_once(APP . 'Vendor' . DS . 'dompdf' . DS . 'dompdf_config.inc.php'); 
    spl_autoload_register('DOMPDF_autoload'); 
    $dompdf = new DOMPDF(); 
    $dompdf->set_paper = 'A4';
    $dompdf->load_html(utf8_decode($content_for_layout), Configure::read('App.encoding'));
    $dompdf->render();
    echo $dompdf->output();

and this app/Controller/LocationsController :

public function admin_view_pdf($id = null) {
    $this->Location->id = $id;
    if (!$this->Location->exists()) {
        throw new NotFoundException(__('Invalid post'));
    }
    // increase memory limit in PHP 
    ini_set('memory_limit', '512M');
    $this->set('post', $this->Location->read(null, $id));

    $options = array('conditions' => array('Location.' . $this->Location->primaryKey => $id));
    $location=$this->Location->find('first', $options);
    $this->set('location', $location);

    $country=new Country();
    $options2 = array('conditions' => array('Country.' . $country->primaryKey => $location['User']['country_id']));
    $this->set('country',$country->find('first',$options2));

    $city=new City();
    $options3 = array('conditions' => array('City.' . $city->primaryKey => $location['User']['city_id']));
    $this->set('city',$city->find('first',$options3));

    $permit=new Permit();
    $options4 = array('conditions' => array('Permit.' . $permit->primaryKey => $location['User']['permit_id']));
    $this->set('permit',$permit->find('first',$options4));

    $category=new Category();
    $options5 = array('conditions' => array('Category.' . $category->primaryKey => $location['Car']['category_id']));
    $this->set('category',$category->find('first',$options5));

    $subcategory=new Subcategory();
    $options6 = array('conditions' => array('Subcategory.' . $subcategory->primaryKey => $location['Car']['subcategory_id']));
    $this->set('subcategory',$subcategory->find('first',$options6));


    $country2=new Country();
    $options7 = array('conditions' => array('Country.' . $country2->primaryKey => $location['Agency']['country_id']));
    $this->set('country2',$country2->find('first',$options7));

    $city2=new City();
    $options8 = array('conditions' => array('City.' . $city2->primaryKey => $location['Agency']['city_id']));
    $this->set('city2',$city2->find('first',$options8));
}

and this is app/View/Locations/admin_view.ctp :

<?php echo $this->Html->link(__('PDF'), array('action' => 'admin_view_pdf', 'ext' => 'pdf', $location['Location']['id'])); ?>

and this app/View/Locations/pdf/admin_view_pdf.ctp :

<div class="row" style="margin-left: 8px;">
<div class="col-lg-10 col-md-10" style="margin-left: 8px;" >


<div class="span12 columns">

        <ol class="breadcrumb">
            <li><?php echo $this->Html->link('Location','/');?>
            </li>
            <li class="active"><?php echo $location['User']['first_name'];?>
            </li>
        </ol>

<div class="panel panel-info">
    <div class="panel-heading">
        Primary Panel
    </div>
   <div class="panel-body">

    <div class="col-lg-4 col-md-4">
    <?php echo $this->Html->image($location['Car']['avatar'], array('alt'=>$location['Car']['id'],'width'=>'250'));?>
    <p>
     <h4><?php echo $category['Category']['name'];?></h4>
      <p>
      <h4><?php echo $subcategory['Subcategory']['name'];?></h4>

       <h5><small style="font-family: 'Roboto Slab';"> Car pseudo  </small><?php echo $location['Car']['title'];?></h5>
       <h5><small style="font-family: 'Roboto Slab';"> Color  </small><?php echo $location['Car']['color'];?></h5>
       <h5><small style="font-family: 'Roboto Slab';"> Serial  </small><?php echo $location['Car']['serial'];?></h5>
       <h5><small style="font-family: 'Roboto Slab';"> Model  </small><?php echo $location['Car']['model'];?></h5>
       <h4><small style="font-family: 'Roboto Slab';"> Price  </small><?php echo $location['Car']['price'];?></h4>
     </div>
    <div class="col-lg-4 col-md-4">
    <div><?php echo $this->Html->image($location['User']['picture'], array('alt'=>$location['User']['first_name'],'width'=>'150'));?></div>

        <h4><?php echo $location['User']['last_name'];?></h4>
        <h5><small style="font-family: 'Roboto Slab';"> First name </small><?php echo $location['User']['first_name'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Id </small><?php echo $location['User']['id'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Username </small><?php echo $location['User']['username'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Birthday </small><?php echo $location['User']['birthday'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Created </small> <?php echo $location['User']['created'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Modified </small> <?php echo $location['User']['modified'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Email</small> <?php echo $location['User']['email'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">CIN </small> <?php echo $location['User']['cin'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Role </small> <?php echo $location['User']['role'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Status </small> <?php echo $location['User']['status'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Permit </small> <?php echo $location['User']['permit'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Phone </small> <?php echo $location['User']['phone'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Country </small> <?php echo $country['Country']['country'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">City </small> <?php echo $city['City']['city'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Permit Type </small> <?php echo $permit['Permit']['category'];?></h5>

    </div>
    <div class="col-lg-4 col-md-4">
    <div><?php echo $this->Html->image('logo.png', array('width' => '100', 'height'=>'60')); ?></div>
        <h5><small style="font-family: 'Roboto Slab';"> First name </small><?php echo $location['Agency']['id'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';"> First name </small><?php echo $location['Agency']['name'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">Country </small> <?php echo $country2['Country']['country'];?></h5>
        <h5><small style="font-family: 'Roboto Slab';">City </small> <?php echo $city2['City']['city'];?></h5>
    </div>
    <h5><small style="font-family: 'Roboto Slab';"> </small></h5>


    </div>
    <div class="panel-footer">Panel Footer</div>
 </div>
 </div>
 </div>

</div>

some one can help me!

PS : all work in Locations/admin_view.ctp and is the same code in admin_view_pdf.ctp

Upvotes: 0

Views: 1943

Answers (1)

BrianS
BrianS

Reputation: 13914

You are loading your document with $dompdf->load_html(). Because of this dompdf does not know where your document lives and assumes all paths that do not include a domain are referencing files on the local file system. Absolute paths are from the root of the file system; relative paths are relative to the location of the PHP script using dompdf.

With the above in mind ... your images are included using the image function (e.g. $this->Html->image('logo.png'); ?> which outputs a path similar to /images/logo.png. dompdf sees this path and looks for the file from the file system root (instead of, for example, /inet/www/siteroot/images/logo.png).

The easiest method of resolving the issue would be to tell CakePHP to include the domain, e.g. $this->Html->image("logo.png", array('fullBase' => true));.

Upvotes: 1

Related Questions