<?php
if (!isset($filename)){
    $filename = "Download";
}


$content_for_layout = $this->fetch('content'); //echo $content_for_layout;exit;


header ("Content-Disposition: attachment; filename=\"$filename.pdf" );
require_once(ROOT .DS. 'vendor' . DS . 'dompdf' . DS . 'autoload.inc.php');
//spl_autoload_register('DOMPDF_autoload');
use Cake\Core\Configure;
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->set_option('isRemoteEnabled', TRUE);
//$dompdf->setPaper("a4", "portrait");

// Use the passed 'orientation' variable (default to 'portrait')
$orientation = isset($orientation) ? $orientation : 'portrait';
$dompdf->setPaper("a4", $orientation); // Set to the passed orientation


$dompdf->loadHtml(utf8_decode($content_for_layout), Configure::read('App.encoding'));
$dompdf->render();
//echo 1;exit;
if(!empty($this->request->query('preview'))){
    $dompdf->stream($filename.'.pdf',array('Attachment'=>0));
    echo $dompdf->output();
    exit;
}else{
    ob_end_clean();
    echo $dompdf->output();
    echo $content_for_layout;
}

?>