【问题标题】:How i display image in pdf file using dompdf in cakephp 2我如何在 cakephp 2 中使用 dompdf 在 pdf 文件中显示图像
【发布时间】:2015-03-18 19:33:12
【问题描述】:

我使用 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 中是相同的代码

【问题讨论】:

  • 这无关,但相当重要。摆脱uff8_decode() ...它会破坏您的内容。

标签: cakephp pdf dompdf


【解决方案1】:

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

考虑到上述内容...您的图像是使用 image 函数包含的(例如,$this-&gt;Html-&gt;image('logo.png'); ?&gt; 输出类似于 /images/logo.png 的路径。dompdf 看到此路径并看起来对于来自文件系统根目录的文件(而不是,例如,/inet/www/siteroot/images/logo.png)。

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

【讨论】:

    猜你喜欢
    • 2013-05-28
    • 2021-02-03
    • 2018-01-24
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 2020-05-04
    • 1970-01-01
    相关资源
    最近更新 更多