JohnWilks
JohnWilks

Reputation: 81

How to center image in header (TCPDF)

I have the code below and I am guessing what is the center of the page by eye. How would I center the image the proper way?

class MYPDF extends TCPDF {

        //Page header
        public function Header() {
                // Logo
                $image_file = K_PATH_IMAGES.'logo.png';
                $this->Image($image_file, 90, 5, 40, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);

                $style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(39, 137, 199));

                $this->Line($this->getPageWidth()-PDF_MARGIN_RIGHT, 25, PDF_MARGIN_LEFT, 25, $style);


        }
}

Thanks

Upvotes: 8

Views: 28595

Answers (4)

MudMan23
MudMan23

Reputation: 821

You can try to use the HTML function in TCPDF. It’s very simple: you create HTML like usual, and convert it into PDF.

<?php
$html = '<h2>List of Expediton</h2>    <!-- Title -->
<table border="1" cellspacing="3" cellpadding="4">
    <tr>
        <th align="left">Title</th>    <!-- head of column name -->
        <th align="center">Title</th>  <!-- head of column name -->
        <th align="right">Title</th>   <!-- head of column name -->
    </tr>
    <tr>
        <td>Example</td>
        <td>Example</td>
        <td>Example</td>
    </tr>

</table>
';

$pdf->writeHTML($html, true, false, true, false, ''); //function to convert from HTML
?>

Upvotes: 0

fdehanne
fdehanne

Reputation: 1718

You have a param $palign in Image()

(string) Allows to center or align the image on the current line. Possible values are:

  • L : left align
  • C : center
  • R : right align
  • empty string : left for LTR or right for RTL

With your image :

$this->Image($image_file, 90, 5, 40, '', 'PNG', '', 'T', false, 300, 'C', false, false, 0, false, false, false);

Upvotes: 16

benske
benske

Reputation: 4050

This should work:

// Image($file, $x='', $y='', $w=0, $h=0, $type='', $link='', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false, $fitonpage=false)

$this->Image($image_file, 'C', 6, '', '', 'JPG', false, 'C', false, 300, 'C', false, false, 0, false, false, false);

You can find more information in the API: http://www.tcpdf.org/doc/code/classTCPDF.html#a714c2bee7d6b39d4d6d304540c761352

Upvotes: 12

user757547
user757547

Reputation: 1

In TCPDF, you can use a HTML tag + Style in order to center a text/image.

Upvotes: 0

Related Questions