Search code examples
phputf-8tcpdffpdffpdi

Using FPDF & FPDI & TCPDF


I want to write some text on top of a PDF template, and I achieved this using: FPDF & FPDI libraries.

My code:

<?PHP

include_once('fpdf.php');
require_once('fpdi.php');

$dbhost="xxx";
$dbuser="xxx";
$con = mysqli_connect($dbhost,$dbuser, "");
if (!$con) 
    {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
mysqli_select_db($con,'ecertificate');

$id = $_GET['id'];

$result = mysqli_query($con,"SELECT `eventinfo`.`Date`,`eventinfo`.`template`,`eventinfo`.`cer_title`, `attendeesinfo`.`fullName`,
`attendeesinfo`.`email`,`attendeesinfo`.`eventID`

FROM `attendeesinfo` INNER JOIN `eventinfo` ON 
`attendeesinfo`.`eventID`=`eventinfo`.`ID` WHERE `attendeesinfo`.ID='$id'");
if (mysqli_num_rows($result) > 0)
    {
        while($row = mysqli_fetch_assoc($result)) 
            {
                $cer_title =$row['cer_title'];
                $date =$row['Date'];
                $template =$row['template'];
                $fullName =$row['fullName'];
                $email =$row['email'];
                $event_ID = $row['eventID'];
            }
$newDate = date("d-m-Y", strtotime($date));

$pdf =& new FPDI();
$pdf->addPage('L');
$pagecount = $pdf->setSourceFile('template/'.$template);
$tplIdx = $pdf->importPage(1); 
$pdf->useTemplate($tplIdx); 
$pdf->SetFont('Times','IB',30);
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(0,62);
$pdf->Cell(0, $height, $fullName, 0, 0, 'C');
$pdf->SetXY(0, 102); 
$pdf->Cell(0, $height, $cer_title, 0, 0, 'C');
$pdf->SetFont('Times','IB',20);
$pdf->SetXY(0, 140); 
$pdf->Cell(0, $height, $newDate, 0, 0, 'C');
$pdf->Output();
}
?>

The problem is

In case of writing Arabic text (UTF-8 Encoding), the text appears as question marks.

My journey in solving this problem

I found that TCPDF Support UTF-8 Unicode and Right-To-Left languages (https://tcpdf.org/) and when I used it, I found that some methods like setSourceFile is not supported (Call to undefined method TCPDF::setSourceFile()) then I found a solution for this issue which is to inherit classes from FPDI and FPDF and I couldn't achieve this, so I stopped here...

Kindly help me in changing my code to use all the libraries so case of English and Arabic latter won't be a problem.


Solution

  • Just require TCPDF instead of FPDF before FPDI. FPDI will extend TCPDF then.