Thursday, May 25, 2017

Basic layout of Dompdf

<html>
<head>
  <style>
  @page { margin: 50px; }
.header,
.footer {
    width: 100%;
    position: fixed;
}
.header {
    top: -50px;
    height:50px;
}
.footer {
    bottom: -50px;
    height:50px;
}
.pagenum:before {
    content: counter(page);
}
  </style>
  </head>
<body>
<div class="header">
    <div style="border-bottom:1px solid #ccc000;">Ubed Khan Web Developer&#0169;</div>
</div>
<div class="footer">
    <div style="border-bottom:1px solid #ccc000;">Copyright &#0169; 2017 ukphpdeveloper.blobspot.in. All Right Reserved.</div>
</div>
<div class="content">
//here your pdf page content will placed
you can use page break style as below when you want your content on next page of pdf.
 <!--<p style="page-break-before: always;"><a href="ukphpdeveloper.blogspot.in">ukphpdeveloper.blogspot.in</a></p>-->
</div>
</body>
</html>

Dompdf with cakephp

Read More »

Wednesday, May 24, 2017

Cakephp and Dompdf

Here we go

Download dompdf library
https://github.com/dompdf/dompdf/releases/tag/v0.6.1
Extract zip file and rename dompdf and keep dompdf folder in app/vendor directory

open app/config/routes.php and place following line in it

Router::parseExtensions('pdf');

Now create pdf directory in app/view/layouts
then create default.ctp file in app/view/layouts/pdf/ and paste following code in it

<?php
require_once(APP . 'Vendor' . DS . 'dompdf' . DS . 'dompdf_config.inc.php');

// instantiate and use the dompdf class
$dompdf = new Dompdf();

$dompdf->load_html($content_for_layout);

// (Optional) Setup the paper size and orientation
$dompdf->set_paper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();
exit(0);

Now open any controller , assume here PostController.php

Create function as below for pdf link
function pdflink(){
}

Create view file in app/view/posts directory and name it pdflink.ctp and paste following code
<?php
echo $this->Html->link(__('PDF'), array('action' => 'view_pdf', 'ext' => 'pdf', 1));
?>

Create following function to view pdf data
function view_pdf(){
}
Now create "pdf" directory in app/view/posts directory.
Then Create view file in app/view/posts/pdf/ directory and name it "view_pdf.ctp" and paste following html which will display in your pdf.
<h1>Ubed Khan Web Developer</h1>
<p>Thanks to visit ukphpdeveloper.blogspot.in</p>

Now Run
http://localhost/project-name/posts/pdflink
you will see the link on this page and just click on that your pdf will download. It's simple as that.





Note: Keep your debug mode on production level in app/config/core.php
Configure::write('debug',0);

if you will get the error that font.php file not found then you have to download php font from below link
https://github.com/PhenX/php-font-lib/releases
or
https://github.com/PhenX/php-font-lib/tree/master/src/FontLib

And create "classes" directory in app/Vendor/dompdf/lib/php-font-lib/
Then paste downloaded php font library in app/Vendor/dompdf/lib/php-font-lib/classes/

if you large amount of data then it will need execution time more than 30sec which is by default in php.ini file. so you can try by doing following changes.

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

Enjoy coding ;)

Basic layout of Dompdf

Read More »