To PDF or not to PDF

First, have you tried Quick PDF Library? I use it since late '90 with Delphi (then known as Isedquickpdf), now with C# (and I know it works with PHP, but haven’t tried yet). Good product, covers probably all the features of the current pdf standard.

My answer to your question: check the functions eg. the a/m library covers - above some level of complexity it’s hard to implement all them in XHTML. At least I don’t have time for it.

One major benefit (depending on your target/client) is the fact that when you print a PDF, you do not get the (usually unwanted) ‘header and footer’ appended by your browser by default.

By this I mean for example, the URL printed at the top of the document.

You can’t expect your users to mess about (or even know about) changing print settings.

I ended up using DOMPDF, seems to be the best of the best. Has high activity, so I have hope for it.

I used this one a long time ago almost 2 years I think… it worked pretty good but some of the html/css i needed to hack a little to get it displayed properly.

As to your question…

"I guess my question is… what does generating a PDF for a standard form give you over just generating XHTML?"

Only benefit is that you can download the pdf ;)

Could you please post sample code of dompdf usage inside Yii?

I’ve tried code from http://groups.google.com/group/dompdf/browse_thread/thread/1bb20c85878d89c1 but it’s not very clean with spl_autoload_* stuff and works for me only with version 0.6.0 beta 1 which has some problems rendering my html markup into pdf.

Sure, this is what I use:


<?php


Yii::import('application.extensions.pdf.dompdf.*'); 

require_once(dirname(__FILE__).'/dompdf/dompdf_config.inc.php'); 

spl_autoload_unregister(array('YiiBase','autoload')); 

spl_autoload_register(array('YiiBase','autoload')); 




class Pdf

{

  private $_dompdf;

  

  private $_html;

  

  /**

   * Init

   */

  public function __construct()

  {

    $this->_dompdf = new DOMPDF();

    $this->_dompdf->base_path = Yii::app()->request->baseUrl; 

  }

  

  /**

   * set paper size

   * 

   * @param string $size

   * @param string $orientation

   */

  public function setSize($size, $orientation='portrait')

  {

    $this->_dompdf->set_paper($size, $orientation);

  }

  

  public function renderPartial($view, $params)

  {

    $html = Yii::app()->controller->renderPartial($view, $params, true, true);

    $this->_html .= $html;

  }

  

  public function stream($name)

  {

    $this->_dompdf->load_html($this->_html);

    $this->_dompdf->render();

    $this->_dompdf->stream($name);

  }

}

Thanks for reply!

For now I went with tcpdf and extension for it (had to finish project quickly) but I will try dompdf too.

Sadly there is no cleaner way of using it. Maybe new version will change something in this regard.

Hi as a newbie in Yii. How did you set up the dompdf files through your yii setup. Is that the whole downloaded dompdf folder inside extensions and then a dompdf subfolder in controllers ?

Regards,

xavier

okay, i think I’ve found the way.

How do you cope with images ?

I can’t get Yii to cope with loading DomPdf. I created sample controller action as follows:




public function actionIndexTest()

{

    Yii::import('application.extensions.pdf.dompdf.*');

    require_once('dompdf_config.inc.php');

    spl_autoload_unregister(array('YiiBase', 'autoload'));

    spl_autoload_register(array('YiiBase', 'autoload'));


    $dompdf = new DOMPDF();

}



and I get:

YiiBase::include(DOMPDF.php) [<a href=‘function.YiiBase-include’>function.YiiBase-include</a>]: failed to open stream: No such file or directory

How to fix that ? I’ve tried many include combinations and all of them yield the same include error message.

dompdf_config.inc.php has not been edited.

I got it. I found suitable spl load and unload:




public function actionIndexTest()

{

    Yii::import('application.extensions.pdf.dompdf.*');

    require_once('dompdf_config.inc.php');

    spl_autoload_unregister(array('YiiBase', 'autoload'));

    spl_autoload_register('DOMPDF_autoload');


    $dompdf = new DOMPDF();

}



A bit off-topic or forked question. If you prefer XHTML, how do you force browser to print-out what you just generated? Or maybe you leave clicking on "Print" to the user? I heard that there were a bunch of JS code for forcing printing out of a webpage, without user interaction, but there were not cross-browsers compatibility.

BTW: Any idea for what reason you get -3? For asking a really good question? Hm… seems we have more and more strange people in this forum.

I’m trying to use dompdf but I keep getting the following errors…

[i]Strict Standards: require_once() [function.require-once]: It is not safe to rely on the system’s timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set()function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘America/Chicago’ for ‘CDT/-5.0/DST’ instead in /home/farberam/public_html/BuilderApp/public_html/protected/extensions/pdf/dompdf/dompdf_config.inc.php on line 208

Warning: require_once(/home/farberam/public_html/BuilderApp/public_html/protected/extensions/pdf/dompdf/include/coutputprocessor.cls.php) [function.require-once]: failed to open stream: No such file or directory in /home/farberam/public_html/BuilderApp/public_html/protected/extensions/pdf/dompdf/dompdf_config.inc.php on line 208

Strict Standards: require_once() [function.require-once]: It is not safe to rely on the system’s timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘America/Chicago’ for ‘CDT/-5.0/DST’ instead in /home/farberam/public_html/BuilderApp/public_html/protected/extensions/pdf/dompdf/dompdf_config.inc.php on line 208

Fatal error: require_once() [function.require]: Failed opening required ‘/home/farberam/public_html/BuilderApp/public_html/protected/extensions/pdf/dompdf/include/coutputprocessor.cls.php’ (include_path=’.:/home/farberam/public_html/BuilderApp/public_html/protected/extensions/pdf/dompdf:/home/farberam/public_html/BuilderApp/public_html/protected/extensions:/home/farberam/public_html/BuilderApp/public_html/protected/helpers:/home/farberam/public_html/BuilderApp/public_html/protected/components:/home/farberam/public_html/BuilderApp/public_html/protected/models:/usr/lib/php:/usr/local/lib/php’) in /home/farberam/public_html/BuilderApp/public_html/protected/extensions/pdf/dompdf/dompdf_config.inc.php on line 208

[/i]

I’m using DarkNSF’s example with only a couple of minor changes…




<?php


Yii::import('ext.pdf.dompdf.*'); 

require_once('dompdf_config.inc.php'); 

spl_autoload_unregister(array('YiiBase','autoload')); 

spl_autoload_register('DOMPDF_autoload');




class Pdf

{

  private $_dompdf;

  

  private $_html;

  

  /**

   * Init

   */

  public function __construct()

  {

    $this->_dompdf = new dompdf();

    $this->_dompdf->base_path = Yii::app()->request->baseUrl; 

  }

  

  /**

   * set paper size

   * 

   * @param string $size

   * @param string $orientation

   */

  public function setSize($size, $orientation='portrait')

  {

    $this->_dompdf->set_paper($size, $orientation);

  }

  

  public function renderPartial($view, $params)

  {

    $html = Yii::app()->controller->renderPartial($view, $params, true, true);

    $this->_html .= $html;

  }

  

  public function stream($name)

  {

    $this->_dompdf->load_html($this->_html);

    $this->_dompdf->render();

    $this->_dompdf->stream($name);

  }

}

and I call it from the following action in my controller…




	public function actionPDF2($invID)

	{

		$inventory = $this->loadModel($invID);

		$fileName = $inventory->communityPlan->community->name.'-Lot#'.$inventory->lotNumber;

		$params = array('data'=>$inventory)

	

		$pdf = new Pdf();

		$pdf->renderPartial('pdfview', $params);

		$pdf->stream($fileName);


	}	



Here is the function that contains line 208 that the errors are referring to…




function DOMPDF_autoload($class) {

  $filename = mb_strtolower($class) . ".cls.php";

  require_once(DOMPDF_INC_DIR . "/$filename");

}



Anyone have any ideas of what I’m doing wrong?

Thanks in advance!

So,

When Yiiframework will have a widget to generate the pdf anyway ???

this topics started in 2010, now the framework changed a lot.

and the way to create components too.

I’d guess most people still use tcpdf. Sadly, it’s still the most matured PHP lib for PDF generation <_<

Well mPDF proved reliable to me. There’s an extension for it (can be used for HTML2PDF as well)

Since this topic is still alive, here’s the bare minimum to get DOMPDF working (I use the same technique to temporarily get Smarty working in another project).

I copied the DOMPDF_autoload function and manually registered it as an autoloader. Then I simply include dompdf_config.inc.php and all is well :)

Save the code below as protected/components/DOMPDF.php and Yii will autoload it when you call new DOMPDF().


// DOMPDF_autoload

Yii::registerAutoloader(function($class) {

  $filename = DOMPDF_INC_DIR . "/" . mb_strtolower($class) . ".cls.php";

  

  if ( is_file($filename) )

    require_once($filename);

});




require Yii::getPathOfAlias('application.vendors.dompdf') . '/dompdf_config.inc.php';

Of course a wrapper class for rendering the HTML (as DarkNSF posted) is nice too, so you could add that to the bottom of this code.

I ended up implementing this as Tsunami had mentioned above




<?php


// DOMPDF_autoload

Yii::registerAutoloader(function($class) {

  $filename = DOMPDF_INC_DIR . "/" . mb_strtolower($class) . ".cls.php";

  

  if ( is_file($filename) )

    require_once($filename);

});


require Yii::getPathOfAlias('ext.dompdf').DIRECTORY_SEPARATOR.'dompdf_config.inc.php';


class Pdf

{

  private $_dompdf;

  

  private $_html;

  

  public $output;

  

  /**

   * Init

   */

  public function __construct()

  {

    $this->_dompdf = new DOMPDF();

    $this->_dompdf->base_path = Yii::app()->request->baseUrl; 

  }

  

  /**

   * set paper size

   * 

   * @param string $size

   * @param string $orientation

   */

  public function setSize($size, $orientation='portrait')

  {

    $this->_dompdf->set_paper($size, $orientation);

  }

  

  public function renderPartial($view, $params)

  {

    $html = Yii::app()->controller->renderPartial($view, $params, true, false);

    $this->_html .= $html;

  }

  

  public function stream($name)

  {

    $this->_dompdf->load_html($this->_html);

    $this->_dompdf->render();

    $this->_dompdf->stream($name);

  }

  //Returning pdf output as a stream, e,g when using you want to send pdf as attachment in emails

  public function output()

  {

    $this->_dompdf->load_html($this->_html);

    $this->_dompdf->render();

    $this->output = $this->_dompdf->output();

    return $this->output;

  }

}



Oh my goodness! an amazing article dude. Thank you However My business is experiencing issue with ur rss. Don’t know why Struggling to subscribe to it. Is there anyone having identical rss problem? Anyone who knows kindly respond. Thnkx