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

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

我使用 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

中是相同的代码

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

考虑到上述情况...您的图像是使用图像函数包含的(例如 $this->Html->image('logo.png'); ?> 输出类似于 /images/logo.png[=21= 的路径].dompdf 看到此路径并从文件系统根目录(而不是 /inet/www/siteroot/images/logo.png)查找文件。

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