Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an image in TCPDF

I want to add an image in header using TCPDF in my Magento store.

I am doing this:

$tcpdf = new TCPDF_TCPDF();
$img = file_get_contents(Mage::getBaseDir('media') . '/dhl/logo.jpg');

$PDF_HEADER_LOGO = $tcpdf->Image('@' . $img);//any image file. check correct path.
$PDF_HEADER_LOGO_WIDTH = "20";
$PDF_HEADER_TITLE = "This is my Title";
$PDF_HEADER_STRING = "This is Header Part";

$tcpdf->SetHeaderData($PDF_HEADER_LOGO, $PDF_HEADER_LOGO_WIDTH, $PDF_HEADER_TITLE, $PDF_HEADER_STRING);

$tcpdf->Output('report_per_route_'.time().'.pdf', 'I'); 

What steps I have to follow if I want to add my store name (left corner) and logo (right corner)?

like image 861
Aliana Avatar asked May 15 '16 10:05

Aliana


2 Answers

If you are trying to generate the pdf using the WriteHTML() here is a little trick to add image without use of image() function.

Simply use the HTML <img> as below,

$image_path = 'path/to/image'; $print = '<p>some text here...</p>'; $print .= '<img src=" '. $image_path .' ">';

and you can use inline css to apply height, width etc.

like image 125
mapmalith Avatar answered Sep 22 '22 17:09

mapmalith


Ok. First of all $PDF_HEADER_LOGO is suppose to be an image file name, not image data - as in default implementation of Header() function. There is, however, one important thing to remember, exact location depends on K_PATH_IMAGES constant, which should contain path to images folder. If its defined before including TCPDF library its ok, if not TCPDF checks some default paths and first existing is used as images directory. Those directories are:

  • ./examples/images/
  • ./images/
  • /usr/share/doc/php-tcpdf/examples/images/
  • /usr/share/doc/tcpdf/examples/images/
  • /usr/share/doc/php/tcpdf/examples/images/
  • /var/www/tcpdf/images/
  • /var/www/html/tcpdf/images/
  • /usr/local/apache2/htdocs/tcpdf/images/
  • K_PATH_MAIN (which is root tcpdf folder)

So either define constant before, or put your file to one of above directories, and then pass only file name as first argument to SetHeaderData and it should work.

To have something similar for Footer you need to extend base TCPDF_TCPDF class and overwrite its Footer method.

Example:

class MYPDF extends TCPDF_TCPDF {

    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'COMPANY NAME', 0, false, 'C', 0, '', 0, false, 'T', 'M');
        $this->Image('/path/to/image.jpg', 500)
    }
}

You'll probably need to work out exact coordinates. Especially in Image it depends on your dimensions, you can add another parameter to Image function being y coordinate, and two others - width and height of image.

And most importantly I recommend checking great examples section on TCPDF page:

http://www.tcpdf.org/examples.php

like image 37
Deus777 Avatar answered Sep 21 '22 17:09

Deus777