How to add an image in TCPDF
Asked Answered
C

3

5

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)?

Choosy answered 15/5, 2016 at 10:36 Comment(0)
L
6

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.

Ledbetter answered 11/6, 2018 at 10:6 Comment(0)
M
3

TCPDF is tricky about inserting images as HTML. It implements few hacks to tell what is being loaded:

  1. inserting image with src attribute as absolute path - must have star * prefix:

    <img src="*/var/www/my-image.png">

  2. inserting image with src attribute as relative path - both examples are treated as relative paths:

    <img src="/var/www/my-image.png">

    <img src="var/www/my-image.png">

Note, that relative paths are calculated differently on linux and windows - what works correctly on windows may not work well on linux. That is caused by checking first character in a path string as a forward slash /, which is considered a linux root and the path will be recalculated - relative path will append to a global variable DOCUMENT_ROOT.

  1. Loading base-64 encoded string - must have @ prefix in src attribute:

    <img src="@iVBORw0KGgoAAggfd0000555....">

    <img src="@'.base64_encode(file_get_contents($path)).'" width=50 height=35>

This is safe bet if you want to avoid issues with calculating correct path, but adds extra I/O overhead, because TCPDF will attempt to store supplied data as temporary image file in order to determine image width & height.

UPDATED:

Since TCPDF 6.4 (2020-07-23) there is new setting allowLocalFiles which is by default false. Set it to true and load local file with protocol file:// prefix:

$tcpdf->allowLocalFiles = true;
<img src="file:///var/www/my-image.png">   # linux
<img src="file://c:\\images\my-image.png"> # windows
Mincey answered 25/2, 2021 at 11:14 Comment(2)
@Kamlesh Updated answer, maybe will help - enable allowLocalFiles.Mincey
Absolute and Relative both paths not worked for me. I used K_PATH_IMAGES constant of tcpdf for setting images path in img html tag and it worked for me. <img src="'.K_PATH_IMAGES.'logo-english.jpg" alt="logo-english.jpg" width="220" />; Thanks.Wellington
S
2

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

Synchronous answered 15/5, 2016 at 17:52 Comment(3)
Do you have an idea where to extend this class in magento ?Choosy
I recommend using lib, you probably use it for TCPDF anyway. Create folder there, and put your extended class in some file. In it also include TCPDF, so one library includes the other. Then in the createPdf (or any) method in whatever controller/observer/model you are using just include/require it like this: require(Mage::getBaseDir('lib') . '/mypdf/mypdf.php');Synchronous
Deus Ex Machina. Gratias multas!Unqualified

© 2022 - 2024 — McMap. All rights reserved.