-->

Including style sheets in a PDF in cakePHP

2020-04-27 07:24发布

问题:

I am using dompdf to generate PDFs for some of the views and that is working just fine. The problem is that I cannot include the css files anywhere, and only css included in the <style> tags inside the view itself is taken into consideration.

Here is the controller action:

public function view_pdf($id=null){

    ini_set('memory_limit','512M');

    $event = $this->Event->findById($id);

        $evt_data=new \DateTime($event['Event']['date']);
        $this->set('event', $event);
        $this->layout='event';
}

Here is the layout:

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->stream('Event.pdf');

And here is the view itself (shortened version):

<style>
div.content-event{
    background: gray;
    color: black;
    padding: 20px;
}
</style>
<div class="content-event" style="margin-bottom: 80px">
<div class="heading">
    <div class="row">
        <div class="col-md-12">
            <h3><?php echo $event['Event']['name']; ?></h3>
        </div>
    </div>
</div>

I have tried to include the css files in the usual way, like: echo $this->Html->css('event');, in both the view and the layout, but this is just not taking any action.

Any help or guidance is much appreciated.

回答1:

Archana's answer appears to be valid, but I wanted to provide a bit more context. When you load a document using $dompdf->load_html() dompdf has no information about the source of the file. When you provide links to external resources dompdf sets the currently executing file on the local file system as the base path.

Essentially URLs will be parsed in the following manner:

  • relative URLs (e.g. css/styles.css) will be evaluated relative to the currently executing file
  • absolute URLs (e.g. /css/styles.css) will be evaluated relative to the file system root
  • URLs with a domain (e.g. http://example.com/css/styles.css) will be evaluated as written

When you make the following call to HTMLHelper::css:

$this->Html->css('styles')

CakePHP will produce a style reference similar to the following:

<link rel="stylesheet" type="text/css" href="/css/styles.css" />

Based on the parsing rules I outlined above that reference will be read from the filesystem root. And unless you have a root folder called "css" with your stylesheet in it that reference will be invalid. The reason Archana's answer works is that you're providing a path from the root of the file system to the file (e.g. /inet/www/cakesite/app/webroot/css/styles.css).

It's a bit easier to get a handle on external resource references if you specify a domain. In that manner dompdf will access the resource via your web server as would any web browser. This type of reference does require a bit more attention to your dompdf and server configuration.

You might find the following questions helpful on how to construct a URL:

  • Q: How to get the base Url in cakephp?
  • Q: base_url in CakePHP


回答2:

You can include CSS file like following:-

<link rel="stylesheet" type="text/css" href="<?php echo APP.'webroot'.DS.'css'.DS.'event.css'; ?>" media="all" /> 

Put your css file into webroot/css folder.