2014年10月31日 星期五

yii 產生pdf 大破解 (史上最清楚的TCPDF教學)

前言 

要在yii使用tcpdf根本不用去找別人寫的套件, 只要載入官方的檔案就可以了! 
ps: 我會將tcpdf檔案放在extensions純粹是讓整個架構合理, 日後管理起來比較好找而已.

前置作業

1. 下載TCPDF (按我)

2. 解壓縮後將examples資料夾刪掉 (不需要), 這時候全部的檔案會在一個tcpdf的資料夾裡

3. 將tcpdf資料夾傳到yii 的第三方套件資料夾 (protected/extensions)

4. 到yii 的設定檔 (protected/config/main.php), 將tcpdf路徑加進去

'import' => array(
'application.models.*',
'application.components.*',
'ext.tcpdf.*',
),

5. 進到tcpdf的設定檔 (extensions/tcpdf/config/tcpdf_config.php) *重要
-如果你產生的pdf裡面要有logo的話, 先取消掉 K_PATH_IMAGES的註解, 然後設定你的logo路徑:

define ('K_PATH_IMAGES', '你的logo路徑');

如果我們的路徑是在專案下的img資料夾, 那路徑就是 var/www/ProjectName/img/member/
但是, 更活一點的寫法可以這樣:

$imgPath = dirname(Yii::app()->BasePath) . '/img/member/';
define ('K_PATH_IMAGES', $imgPath);

-設定可以顯示中文字
將PDF_FONT_NAME_MAIN的helvetica改成msungstdlight

開發

Contrller

1. 在你的controller裡設定空白的layout, 設定的方式參考下面兩篇
所謂空白layout就是裡面只有這行:
<?php echo $content; ?>

ps: 要用空白layout的原因是, 在tcpdf產生出pdf前, 如果有任何輸出的話就會出錯!

2
public function actionPrint()
{
$this->layout = '//layouts/nothing';
$data = array(
'name' => '中二不解釋',
'age' => '18',
'tel' => '0910855555',
'note' => '沒事就打lol',
);
$html = Yii::app()->controller->renderPartial('print_templet', array('data'=>$data), true, true);
$this->render('print',array(
'html'=>$html,
));
}

View

1. print_templet.php

<table>
<tr>
<td>
姓名
</td>
<td>
<?php echo data['name']; ?>
</td>
</tr>
<tr>
<td>
年紀
</td>
<td>
<?php echo data['age']; ?>
</td>
</tr>
<tr>
<td>
電話
</td>
<td>
<?php echo data['tel']; ?>
</td>
</tr>
<tr>
<td>
備註
</td>
<td>
<?php echo data['note']; ?>
</td>
</tr>
</table>

2. print.php

$pdf = new tcpdf('P', 'mm', 'A4', true, 'UTF-8', false);

$pdf->SetCreator('Joker love you');
$pdf->SetAuthor('joker');
$pdf->SetTitle('Welcome to joker.tw!');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, PHP');

$pdf->setHeaderData('logo.png', 30, 'joker.tw', '反攻大陸就靠promo!', array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));

$pdf->setHeaderFont(Array('msungstdlight', '', '10'));
$pdf->setFooterFont(Array('helvetica', '', '8'));

$pdf->SetDefaultMonospacedFont('courier');

$pdf->SetMargins(15, 27, 15);
$pdf->SetHeaderMargin(5);
$pdf->SetFooterMargin(10);

$pdf->SetAutoPageBreak(TRUE, 25);
$pdf->setImageScale(1.25);
$pdf->setFontSubsetting(true);
$pdf->SetFont('msungstdlight', '', 14, '', true);

$pdf->AddPage();

$pdf->writeHTML($html, true, false, true, true, '');

$style = array(
    'border' => 2,
    'vpadding' => 'auto',
    'hpadding' => 'auto',
    'fgcolor' => array(0,0,0),
    'bgcolor' => false, //array(255,255,255)
    'module_width' => 1, // width of a single module in points
    'module_height' => 1 // height of a single module in points
);

$pdf->write2DBarcode('www.google.com.tw', 'QRCODE,H', 20, 210, 40, 40, $style, 'N');
$pdf->Text(20, 200, '掃我者得永生');

$pdf->Output('penisIsGood.pdf', 'I');


技術說明

1. renderPartial這個很屌, 它跟render的差異在於:

-renderPartial本質上就是include一個php文件。它和render的不同點在於後者在include完一個php文件後還會把顯示的內容安插到一個layout中。

以上說明引用自 renderPartial 的用法

-renderPartial的參數可以看官方說明
-參數的說明可以看 Yii的中渲染和的RenderPartial的區別

沒有留言:

張貼留言