Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter3 and tcpdf
#5

Here is my source code:

Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

if (!defined('K_PATH_IMAGES')) {
    define ('K_PATH_IMAGES', DEFAULTFCPATH.'assets/img/');
}

class Print_controller extends Portfolio_controller {

    public function __construct() {

        parent::__construct();

        $this->load->helper('file');
    }

    public function index() {

        $id = $this->id;

        $model_name = $this->model['name'];
        $keywords = '';

        $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

        // Set document information.
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('Ivet Fashion Model Agency');
        $pdf->SetTitle($model_name);
        $pdf->SetSubject($this->is_polaroid ? '"Polaroid" Photos' : 'Portfolio');
        $pdf->SetKeywords($keywords);

        // Set default header data.
        $pdf->SetHeaderData('logo-ivet-fashion-pdf.png', 15, $model_name, '');

        // 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 monospaced font.
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // Set margins.
        $pdf->SetMargins(25, 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);

        // -------------------------------------------------------------------

        foreach ($this->images as $image) {

            $image_file = DEFAULTFCPATH.'images/gallery/'.$image['image'];

            if (!file_exists($image_file)) {
                continue;
            }

            // Add a page.
            $pdf->AddPage();

            // Set JPEG quality.
            $pdf->setJPEGQuality(100);

            // Image with resizing.
            $pdf->Image($image_file, '', '', 160, 240, '', '', '', true, 600, 'C', false, false, 0, 'CM', false, false);

        }

        // -------------------------------------------------------------------

        $filename = $model_name.' '.($this->is_polaroid ? 'Polaroid' : 'Portfolio').' IvetFashion';
        $filename = clean_file_name($filename, '-', true);
        $filename .= '.pdf';

        //Close and output PDF document
        @ $pdf->Output($filename, 'I');
    }

}

class MYPDF extends TCPDF {

    public function Header() {
        if ($this->header_xobjid === false) {
            // start a new XObject Template
            $this->header_xobjid = $this->startTemplate($this->w, $this->tMargin);
            $headerfont = $this->getHeaderFont();
            $headerdata = $this->getHeaderData();
            $this->y = $this->header_margin;
            if ($this->rtl) {
                $this->x = $this->w - $this->original_rMargin - 8;
            } else {
                $this->x = $this->original_lMargin + 8;
            }
            if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
                $imgtype = TCPDF_IMAGES::getImageFileType(K_PATH_IMAGES.$headerdata['logo']);
                if (($imgtype == 'eps') OR ($imgtype == 'ai')) {
                    $this->ImageEps(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
                } elseif ($imgtype == 'svg') {
                    $this->ImageSVG(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
                } else {
                    $this->Image(K_PATH_IMAGES.$headerdata['logo'], '', '', $headerdata['logo_width']);
                }
                $imgy = $this->getImageRBY();
            } else {
                $imgy = $this->y;
            }
            $cell_height = $this->getCellHeight($headerfont[2] / $this->k);
            // set starting margin for text data cell
            if ($this->getRTL()) {
                $header_x = $this->original_rMargin + ($headerdata['logo_width'] * 1.1);
            } else {
                $header_x = $this->original_lMargin + ($headerdata['logo_width'] * 1.1);
            }

            $header_x += 100;

            $cw = $this->w - $this->original_lMargin - $this->original_rMargin - ($headerdata['logo_width'] * 1.1);
            $this->SetTextColorArray($this->header_text_color);
            // header title
            $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
            $this->SetX($header_x);
            $this->Cell($cw, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
            // header string
            $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
            $this->SetX($header_x);
            $this->MultiCell($cw, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false, true, 0, 'T', false);
            // print an ending header line
            $this->SetLineStyle(array('width' => 0.85 / $this->k, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $headerdata['line_color']));
            $this->SetY((2.835 / $this->k) + max($imgy, $this->y));
            if ($this->rtl) {
                $this->SetX($this->original_rMargin);
            } else {
                $this->SetX($this->original_lMargin);
            }
            $this->Cell(($this->w - $this->original_lMargin - $this->original_rMargin), 0, '', '', 0, 'C');
            $this->endTemplate();
        }
        // print header template
        $x = 0;
        $dx = 0;
        if (!$this->header_xobj_autoreset AND $this->booklet AND (($this->page % 2) == 0)) {
            // adjust margins for booklet mode
            $dx = ($this->original_lMargin - $this->original_rMargin);
        }
        if ($this->rtl) {
            $x = $this->w + $dx;
        } else {
            $x = 0 + $dx;
        }
        $this->printTemplate($this->header_xobjid, $x, 0, 0, 0, '', '', false);
        if ($this->header_xobj_autoreset) {
            // reset header xobject template at each page
            $this->header_xobjid = false;
        }
    }

    public function Footer() {
        $cur_y = $this->y;
        $this->SetTextColorArray($this->footer_text_color);
        //set style for cell border
        $line_width = (0.85 / $this->k);
        $this->SetLineStyle(array('width' => $line_width, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => $this->footer_line_color));
        //print document barcode
        $barcode = $this->getBarcode();
        if (!empty($barcode)) {
            $this->Ln($line_width);
            $barcode_width = round(($this->w - $this->original_lMargin - $this->original_rMargin) / 3);
            $style = array(
                'position' => $this->rtl?'R':'L',
                'align' => $this->rtl?'R':'L',
                'stretch' => false,
                'fitwidth' => true,
                'cellfitalign' => '',
                'border' => false,
                'padding' => 0,
                'fgcolor' => array(0,0,0),
                'bgcolor' => false,
                'text' => false
            );
            $this->write1DBarcode($barcode, 'C128', '', $cur_y + $line_width, '', (($this->footer_margin / 3) - $line_width), 0.3, $style, '');
        }
        $w_page = isset($this->l['w_page']) ? $this->l['w_page'].' ' : '';
        if (empty($this->pagegroups)) {
            $pagenumtxt = $w_page.$this->getAliasNumPage().' / '.$this->getAliasNbPages();
        } else {
            $pagenumtxt = $w_page.$this->getPageNumGroupAlias().' / '.$this->getPageGroupAlias();
        }
        $this->SetY($cur_y);
        //Print page number
        if ($this->getRTL()) {
            $this->SetX($this->original_rMargin);
            $this->Cell(0, 0, $pagenumtxt, '', 0, 'L');
        } else {
            $this->SetX($this->original_lMargin);
            $this->Cell(0, 0, $this->getAliasRightShift().$pagenumtxt, '', 0, 'R');
        }
    }

}

And it works: http://ivetfashion.com/diana-yakubovska/print

Additional remarks, options: You can activate Composer (see the manual) and then install TCPDF by using it.

composer.json
Code:
{
    "config": {
        "preferred-install": "dist"
    },
    "require": {
        "tecnickcom/tcpdf": "^6.2.12"
    }
}

Since Composer provides it own autoloader, your library Pdf and the line $this->load->library('Pdf'); would not be needed anymore.

After that test the pdf generation with some simple static data (from the given examples), without using a form and form validation. Then modify and test your application specific code.
Reply


Messages In This Thread
CodeIgniter3 and tcpdf - by june123 - 06-12-2017, 10:01 AM
RE: CodeIgniter3 and tcpdf - by ivantcholakov - 06-12-2017, 11:12 AM
RE: CodeIgniter3 and tcpdf - by june123 - 06-12-2017, 07:08 PM
RE: CodeIgniter3 and tcpdf - by ivantcholakov - 06-12-2017, 07:39 PM
RE: CodeIgniter3 and tcpdf - by ivantcholakov - 06-12-2017, 08:26 PM



Theme © iAndrew 2016 - Forum software by © MyBB