№5 Dars - Yiida PDF bilan ishlash.

Biror bir qilayotgan proyektimizda hisobotlarni pdf formatiga o`tkazish kabi ishlarga duch kelamiz. Bunda Yii ning maxsus kengaytmalaridan foydalanish maqsadga muvofiqdir. Shulardan biri tcpdf kengaytmasidir. Bu haqida ushbuhttp://www.yiiframework.com/extension/tcpdf giper murojaat orqali batafsil tanishishingiz mumkin. Biz faqatgina misol bilan sizda Yii da qilingan proyektlaringizni hisobotlarini pdf ga o`tkazish masalasini ko`rib chiqamiz.

Masalani qo`yilishi:

Ma`lumotlar omboridagi employee jadvalini barcha ma`lumotlarini olib uni tartib bo`yicha jadval ko`rinishida pdf formatiga o`tkazish:


Yechimi:

1. http://www.yiiframework.com/extension/tcpdf - giper murojaat orqali TCPDF faylini o`zimizga ko`chirib olamiz va /protected/extensions/tcpdf ga joylaymiz

2. http://www.tcpdf.org/ saytiga kiramiz va ushbu "tcpdf" faylni o`zimizga ko`chiramiz. Keyin/protected/extensions/tcpdf/tcpdf ga joylaymiz.

3. Biz fayllarimizni ushbu ko`rinishda joylayshtirgan bo`lamiz:


4.Endi jadval yaratish kerak bo`ladi. Buning uchun quyidagicha kod orqali ma`lumotlar omboringizda jadval hosil qiling va uni to`ldiring

CREATE TABLE `employee` (
`ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID сотрудника',
`firstName` VARCHAR(30) NOT NULL,
`lastName` VARCHAR(40) NOT NULL,
`address` TEXT NULL DEFAULT NULL,
`phoneNumber` TEXT NULL DEFAULT NULL,
  PRIMARY KEY (`ID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

INSERT INTO `employee` (`firstName`, `lastName`, `address`, `phoneNumber`) VALUES ('Obidjon', 'Sobirjonov', 'Yakkasaroy tumani', '123-65-32');

INSERT INTO `employee` (`firstName`, `lastName`, `address`, `phoneNumber`) VALUES ('Karimbek', 'Sharipov', 'Shayhontohur tumani', '994-44-31');

INSERT INTO `employee` (`firstName`, `lastName`, `address`, `phoneNumber`) VALUES ('Foziljon', 'Hakimov', 'Yunusobod tumani', '333-44-55');

Employee jadvali yaratilib ma`lumotlar qo`shilgandan keyin shu jadval uchun Yii ning Gii generatori orqali model yaratamiz.

5.Endi controllerimizda action hosil qilamiz va quyidagicha kod kiritamiz:

public function actionPrint()
            {
        $pdf = Yii::createComponent(
                 'application.extensions.tcpdf.ETcPdf', 'P', 'cm', 'A4', true, 'UTF-8'
         );
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor("FirstName");
        $pdf->SetTitle("Orders");
        $pdf->SetSubject("Orders");
        $pdf->SetKeywords("Orders");
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        $pdf->AddPage();
        $pdf->SetFont('freeserif', '', 14);
        $tbl = "" . date('d.m.Y', time()) . "
";
        $pdf->SetFont('freeserif', '', 10);
        $tbl .= '';
        $printOrders = null;
        $employee = Employee::model()->findAll();
        $tbl .= "<table style=\"padding: 5px;\" border=\"1\">
               <tbody>
                    <tr bgcolor=\"#ccc\">
                         <th width=\"30%\">
                             <strong>Employee</strong>
                         </th>
                         <th width=\"70%\">
                              <strong>Address</strong>
                         </th>
                   </tr>";
        foreach ($employee as $value)
        {
            $tbl .= "<tr><td><strong>FIO:</strong>
                     {$value->firstName} - {$value->lastName}

                        <strong>Phone Number:</strong> 
                     {$value->phoneNumber}
            </td>
            <td>
                     {$value->address}
            </td></tr>";
        }
        $tbl .= "</tbody></table>";
        $pdf->writeHTML($tbl, true, false, false, false, '');
        $pdf->Output('filename.pdf', 'I');
            }

Natija:
14.05.2014

Albatta bu boshlang`ich kichik bir misol edi, har xil turdagi pdf lar bilan ishlash kerak bo`lsa www.tcpdf.org saytidan batafsil ma`lumot olishingiz mumkin.

Ushbu ma`lumot http://belyakov.su/content/yii-sozdanie-pdf dan olindi.

Manba:


JONNY

Muallif haqida

JONNY Arduino, Java, C#, Android, Windows, Linux, Debian, Javascript. O'zbekistonni rivojlantiramiz! Dasturlash orqali vatanimizni yangi marralarga olib chiqamiz.


Blogdagi so‘nggi maqolalar:


Birinchi bo‘ling!

Iltimos, fikr bildirish uchun saytga kiring yoki ro‘yxatdan o‘ting!