= TCPDF =
* Get component from [[https://github.com/kitavrus/yii2-tcpdf]]
== Automatic Installation ==
Automatic installation is NOT recommended (see NOTES below), but these are the steps:
1. Run the following command:
$ composer require cinghie/yii2-tcpdf "dev-master"
2. Add reference in ''[app]/composer.json'' file, in ''require'' section:
"require": {
...
"cinghie/yii2-tcpdf": "dev-master"
},
NOTE: This installation method tends to remove the ''[app]/vendor/cinghie/yii2-tcpdf'' and fails to update correctly, leaving a non-working installation. Follow the manual installation if this happens.
== Manual Installation ==
Manual Installation is the recommended installation method:
1. Remove the "require" reference (''"cinghie/yii2-tcpdf": "dev-master"'') in the ''[app]/composer.json'' file.
2. Add reference in your configuration ''[app]/config/web.php'' (or ''config.php'') file, in component section:
'component' => [
...
// Yii2 TCPDF
'tcpdf' => [
'class' => 'cinghie\tcpdf\TCPDF',
],
...
]
3. Add reference in ''[app]/vendor/yiisoft/extensions.php'' the following:
return array (
...
'cinghie/yii2-tcpdf' =>
array (
'name' => 'cinghie/yii2-tcpdf',
//'name' => 'tcpdf',
'version' => '1.0.0',
//'bootstrap' => '\cinghie\tcpdf\MyBootstrap',
'alias' =>
array (
'@cinghie/tcpdf' => $vendorDir . '/cinghie/yii2-tcpdf',
),
),
...
);
4. Create controller action. Eg:
use yii\web\Response;
...
class SiteController extends Controller
{
public function actionPdfReport()
{
Yii::$app->response->format = Response::FORMAT_RAW; // Raw for PDF output
return $this->render('pdf-report');
}
}
5. Create view. Eg: ''[app]/views/site/pdf-report.php''
get('tcpdf');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Gogogital.it');
$pdf->SetTitle('Yii2 TCPDF Example');
$pdf->SetSubject('Yii2 TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'Yii2 TCPDF Example', 'Gogodigital - Wide ICT Solutions | gogodigital.it', array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// ---------------------------------------------------------
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();
// set text shadow effect
$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));
// Set some content to print
$html = "Yii2 TCPDF Works Fine!
";
// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
// ---------------------------------------------------------
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('yii2_tcpdf_example.pdf', 'I');
//============================================================+
// END OF FILE
//============================================================+
// Close Yii2
\Yii::$app->end();
?>
6. Test the PDF view:
* Simple URL: http://localhost:8080/[app]/web/index.php?r=site/pdf-report
* Pretty URL: http://localhost:8080/[app]/web/site/pdf-report