我如何使用cakephp 2中的dompdf在pdf文件中显示图像

时间:2015-03-18 19:33:13

标签: cakephp pdf dompdf

我使用dompdf for cakephp将html转换为pdf,但问题是所有图像都不显示,我如何显示图像?

Router::parseExtensions('pdf');

这是AppController

public $components = array('RequestHandler');

这是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();

和这个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));
}

这是app/View/Locations/admin_view.ctp

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

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>
有些人可以帮助我!

PS:Locations/admin_view.ctp中的所有工作都与admin_view_pdf.ctp

中的代码相同

1 个答案:

答案 0 :(得分:1)

您正在使用$dompdf->load_html()加载文档。因此,dompdf不知道文档的位置,并假定所有不包含域的路径都引用本地文件系统上的文件。绝对路径来自文件系统的根目录;相对路径是使用dompdf相对于PHP脚本的位置。

考虑到上述情况......使用图像功能包含您的图像(例如$this->Html->image('logo.png'); ?>,它输出类似于 /images/logo.png 的路径.dompdf看到此路径并从文件系统根目录中查找该文件(例如, /inet/www/siteroot/images/logo.png )。

解决问题的最简单方法是告诉CakePHP包含域名,例如$this->Html->image("logo.png", array('fullBase' => true));