开发者

How to center image in header (TCPDF)

开发者 https://www.devze.com 2023-03-03 11:12 出处:网络
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?

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


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


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号