如何在 Codeigniter 中更改从 TCPDF Library 生成的 PDF 文件的 header?

How to change the header of PDF file generated from TCPDF Library , in Codeigniter?

我正在使用 TCPDF 库生成 PDF 发票,但问题是 header 不应该是静态标题/徽标,我知道我可以从 tcpdf_config.php 更改它 从这一行

define ('PDF_HEADER_TITLE', 'TCPDF Example');

/**
 * Header description string.
 */
define ('PDF_HEADER_STRING', "developerd by syam");

但我想从数据库中检索标题和 header。 这是我的 PDF 视图:

<?php
//============================================================+
// File name   : example_005.php
// Begin       : 2008-03-04
// Last Update : 2013-05-14
//
// Description : Example 005 for TCPDF class
//               Multicell
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
//               Tecnick.com LTD
//               www.tecnick.com
//               info@tecnick.com
//============================================================+

/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: Multicell
 * @author Nicola Asuni
 * @since 2008-03-04
 */

// Include the main TCPDF library (search for installation path).
//require_once('tcpdf_include.php');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information

$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Company Name');
$pdf->SetTitle('Comapny Name');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 005', PDF_HEADER_STRING);

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

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

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

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

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

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

// ---------------------------------------------------------

// set font
$pdf->SetFont('times', '', 10);

// add a page
$pdf->AddPage();

// set cell padding
$pdf->setCellPaddings(1, 1, 1, 1);

// set cell margins
$pdf->setCellMargins(1, 1, 1, 1);

// set color for background
$pdf->SetFillColor(255, 255, 127);

// MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0)

// set some text for example


// set some text for example starting from here syam
$title=<<<EOD
<h3> INVOICE </h3>
EOD;
$pdf->writeHTMLCell(0,0,'','',$title,0,1,0,true,'C',true);//syam
$table='<table style="border:1px solid #000;padding: 6px;">';

$table.='<tr style="background-color:#CCCCCC">
        <th style="border:1px solid #000">Product Name</th>
        <th style="border:1px solid #000">Quentity</th>
        <th style="border:1px solid #000">Price</th>
        <th style="border:1px solid #000">Total</th>
            </tr>';

foreach ($data as $row){
    $table.='<tr>
<td style="border:1px solid #000">'.$row->t_p_name.'</td>
<td style="border:1px solid #000">'.$row->t_qty.'</td>
<td style="border:1px solid #000">'.$row->t_p_price.'</td>
<td style="border:1px solid #000">'.$row->t_p_total_price.'</td>
</tr>';
}
$table .='</table>';
$pdf->writeHTMLCell(0,0,'','',$table,0,1,0,true,'C',true);//syam


// move pointer to last page
$pdf->lastPage();
date_default_timezone_set('Africa/Cairo');
// ---------------------------------------------------------

//Close and output PDF document
ob_clean();
$pdf->Output($_POST['download'].'.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+

如何从数据库中检索 header?

如果您从使用常量值更改为从数据库中检索数据,您应该能够设置所需的 header 详细信息,即将常量 PDF_HEADER_TITLE 更改为来自数据库的标题,即 $invoice->title

换行

$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $invoice->title , PDF_HEADER_STRING);

最好还有:

$pdf->SetTitle($invoice->title);