To PDF or not to PDF

I’ve looked in to a few interfaces to PDF generation. None of them seem as great as I was hoping for. So after doing all of this research, I am starting to think that there is little to no benefit of a PDF file.

It was a required from my current client, I think it breaks down to they want to generate filled in forms based on the data in the database and print them. Which can be done with XHTML.

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

I just did a lot of work with pdf generation and I haven’t found anything good that’s free… I’m using TCPDF but it kinda sucks. I also considered generating Excel files and converting them to PDFs with PHPExcel but not much formatting converts.

I imagine your client wanted to generate forms that had "lines" instead of "text-boxes". He may have made the conclusion from that that pdf files were needed. Or maybe he wanted the ability to email them, or use some other sort of pdf features.

I am using wkhtmltopdf at the moment. Needs X and uses some resources, but PDF creation couldn’t be simpler and the result is very beautiful.

http://code.google.com/p/wkhtmltopdf/

It basically is the same as having a webkit browser doing ‘print to pdf’.

Bas

wkhtmltopdf looks really good but I’m not really sure how I can get it running through php on both my local windows server and my shared linux server

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 <_<