№5 Dars — Yiida PDF bilan ishlash.

№5 Dars — Yiida PDF bilan ishlash.

№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:

e284965 dars yiida pdf bilan ishlash 65e61d2809629

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 .= "

«;
foreach ($employee as $value)
{
$tbl .= «

«;
}
$tbl .= «

Employee Address
FIO:
{$value->firstName} — {$value->lastName}

Phone Number:
{$value->phoneNumber}

{$value->address}

«;
$pdf->writeHTML($tbl, true, false, false, false, »);
$pdf->Output(‘filename.pdf’, ‘I’);
}

Natija:
14.05.2014

e284965 dars yiida pdf bilan ishlash 65e61d285e4b8

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:

Web Dasturlash
№5 Dars — Yiida PDF bilan ishlash.