How to Export PDF's



# 1 > Adding composer file in app

->in app directory, create new file 'composer.json'
-> In composer.json file, add the following


# 2 > Update bootstrap file

->In app->Config->bootstrap, add the following


# 3 > Update appController

->In AppController add the following function
function viewPdf($view, $viewVars, $displayPDF = false) {

$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => 'Invoice'
);
$CakePdf = new CakePdf();
$CakePdf->viewVars($viewVars);
if ($displayPDF) {
$CakePdf->template(DS.$this->params->params['controller'].DS.$view, 'default');
//get the pdf string returned
$pdf = $CakePdf->output();
//If you want to write to the file system
//$pdf = $CakePdf->write(APP . 'files' . DS . 'newsletter.pdf');
// echo 'test123';
echo $pdf;
exit;
} else {
$this->render(DS.$this->params->params['controller'].DS.$view);
}
}


# 4 > Update AppController cont'd

-> On top of 'AppController' add the following
<?php
Configure::write('CakePdf'array(
'engine' => 'CakePdf.DomPdf',
'options' => array(
'print-media-type' => false,
'outline' => true,
'dpi' => 96
),
'margin' => array(
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
),
'orientation' => 'landscape',
'download' => true
));
App::uses('CakePdf''CakePdf.Pdf');
?>


# 5 > Create new layout

-> in app->View->layout, create new directory 'pdf' and create default file in pdf directory
-> in pdf->default, add the following
<?php
header("Content-type:application/pdf");

header("Content-Disposition:attachment;filename=\"Sales.pdf\"");
$this->Html->charset(); ?>
<body>
<?php echo $this->fetch('content'); ?>
</body>


# 6 > Add action in Controller

->In PagesController add the following (can be any controller you need to use to  export)

function view(){
$array1 = array(1 => 1, 2 => 2, 3 => 3);
$array2 = array('test1', 'test2');
$vars = array($array1, $array2);
$this->set(compact($vars));
$this->viewPdf('anotherview', $vars, true);
}
function anotherview() {}


# 7 > Add action in View

->In View->Pages,  create the action 'view' and 'anotherview' we created in controller


# 8 > Selecting the file

In the browser select the export file you created


Other Instructions

Below are many other instructions that show you how to use your UpdateCase application