How to create a custom header with TCPDF?

0

I am generating a pdf with the tcpdf library but I want to create a header with a different height, title and image than the one that comes in it, I do not really know much about this library and I do not understand some things, I have the following:

// Include the main TCPDF library (search for installation path).
require_once('PDF/tcpdf.php');
// 
class PDF{
    private $pdf;
    function __construct($titulo){

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

        // set document information NOTA: datos solo informativos no muestra 
        $this->pdf->SetCreator(PDF_CREATOR);
        $this->pdf->SetAuthor('Personal');
        // $this->pdf->SetTitle($Titulo);
        // $this->pdf->SetSubject($Asunto);

        //$this->pdf->SetKeywords('TCPDF, PDF, example, test, guide');

        // Header los parametros PDF_HEADER_LOGO y PDF_HEADER_LOGO_WIDTH se encuentran declarados en config/tcpdf_config.php 
        // $ln (string) header image logo
        // $lw (string) header image logo width in mm
        // $ht (string) string to print as title on document header
        // $hs (string) string to print on document header
        // $tc (array) RGB array color for text.
        // $lc (array) RGB array color for line.

        // set default header data
        $this->pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, $hs=$titulo);

        // set header and footer fonts
        $this->pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $this->pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        // set default monospaced font
        $this->pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // set margins
        $this->pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $this->pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        // set auto page breaks
        $this->pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        // set image scale factor
        $this->pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // set some language-dependent strings (optional)
        if (@file_exists(dirname(__FILE__).'/lang/spa.php')) {
            require_once(dirname(__FILE__).'/lang/spa.php');
            $this->pdf->setLanguageArray($l);
        }

    }

    function estructuraPag($contenidoHTML){
        // Seleccionamos la tipografia y tamano de letra
        $this->pdf->SetFont('dejavusans', '', 10);

        // Comienza la pag
        $this->pdf->AddPage();

        // Contenido pdf
        $contenidoHTML;

        $this->pdf->writeHTML($contenidoHTML, true, false, true, false, '');
        // reset pointer to the last page
        $this->pdf->lastPage();

    }


    function generarPdf($file, $name){

        //$file = '../archivos/actas/'.$idContrato;
        if(!is_dir($file))
        {
            if(!mkdir($file, 0777)){
                echo "error";
                exit();
            }
        }

        // Close and output PDF document
        // I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
        // D: send to the browser and force a file download with the name given by name.
        // F: save to a local server file with the name given by name.
        // S: return the document as a string (name is ignored).
        // FI: equivalent to F + I option
        // FD: equivalent to F + D option
        // E: return the document as base64 mime multi-part email attachment (RFC 2045)

        $this->pdf->Output($file.'/'.$name.'.pdf', 'FI');
        exit();
    }

}

here some examples ,

    
asked by Alan 12.10.2016 в 21:23
source

3 answers

1

I the header that I use on my pages is with the following code

$PDF_HEADER_TITLE="Titulo del PDF";
$PDF_HEADER_STRING="SEgunda linea";
$PDF_HEADER_LOGO="imagen"; //Solo me funciona si esta dentro de la carpeta images de la libreria

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

// set header and footer fonts
$this->pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$this->pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
    
answered by 05.07.2017 в 09:49
0

I do not know your level and experience in PHP but from what I see what you have to do with that file is to replace the constants (all the variables you see written in capital letters) by your values, either literal or defining those constants .

I have quickly searched some info about TCPDF in Spanish in case it helps you, here you have among other things a list of the constants and their usefulness: link

It is also mandatory to include a link to the official document here: link

If you have specific doubts write them here and we will solve them.

Salu2

    
answered by 13.10.2016 в 09:48
0

In the TCPDF documentation find out what parameters each method receives and do it. Use direct values or your own variables, there is no need to replace constants.

For example to put an image in the header:

$this->pdf->SetHeaderData('/img/logo.png', 100, 'string to print as title on document header', 'string to print on document header');

To be honest, I do not understand the difference between the third and fourth parameters ... You try.

    
answered by 13.10.2016 в 20:52