Question

I am using following code

<?php

$hd1 = $_POST["hd1"];
require_once('fpdi.php');
require_once('fpdf.php');
require('tfpdf.php');

$pdf =& new FPDI();

$pagecount = $pdf->setSourceFile('template.pdf');
$tplIdx = $pdf->importPage(1); 
$s = $pdf->getTemplatesize($tplIdx);
$pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L', array($s['w'], $s['h'])); 
$pdf->useTemplate($tplIdx,0, 0, 0, 0, true); 

$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->AddFont('DejaVu', 'B', 'DejaVuSansCondensed-Bold.ttf', true);


$pdf->SetFont('DejaVu', '', 14);

$pdf->Cell(50,10,$hd1,0,1);
// Select a standard font (uses windows-1252)
$pdf->SetFont('Arial', '', 14);
$pdf->Ln(10);
$pdf->Write(5, 'The file uses font subsetting.');

$pdf->Output('doc.pdf', 'I');
?>

I am getting error:

Fatal error: Class 'FPDF' not found in C:\xampp\htdocs\san\ak-form\pdf\fpdi_bridge.php on line 33

When I use following code from http://www.setasign.com/products/fpdi/demos/tfpdf-demo/

<?php

$hd1 = $_POST["hd1"];

// require tFPDF
require_once('tfpdf.php');

// map FPDF to tFPDF so FPDF_TPL can extend it
class FPDF extends tFPDF
{
    /**
     * "Remembers" the template id of the imported page
     */
    protected $_tplIdx;

    /**
     * Draw an imported PDF logo on every page
     */
    public function Header()
    {
        if (is_null($this->_tplIdx)) {
            $this->setSourceFile("template.pdf");
            $this->_tplIdx = $this->importPage(1);
        }
        $size = $this->useTemplate($this->_tplIdx, 130, 5, 60);

        $this->SetFont('DejaVu', 'B', 16);
        $this->SetTextColor(0);
        $this->SetXY($this->lMargin, 5);

        $text = 'tFPDF (v' . tFPDF_VERSION . ') and FPDI (v'
              . FPDI::VERSION . ')';
        $this->Cell(0, $size['h'], $text);
        $this->Ln();
    }
}

// just require FPDI afterwards
require_once('fpdi.php');

// initiate PDF
$pdf = new FPDI();

// Add some Unicode font (uses UTF-8)
$pdf->AddFont('DejaVu', '', 'DejaVuSansCondensed.ttf', true);
$pdf->AddFont('DejaVu', 'B', 'DejaVuSansCondensed-Bold.ttf', true);

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

$pdf->SetFont('DejaVu', '', 14);

// Select a standard font (uses windows-1252)
$pdf->SetFont('Arial', '', 14);
$pdf->Ln(10);
$pdf->Write(5, 'The file uses font subsetting.');

$pdf->Output('doc.pdf', 'I');
?>

And getting following error:

Warning: fopen(/home/content/w/i/s/wiseinmotion/html/test3/pdf/font/unifont/DejaVuSansCondensed.ttf): failed to open stream: No such file or directory in C:\xampp\htdocs\san\ak-form\pdf\font\unifont\ttfonts.php on line 496
Can't open file /home/content/w/i/s/wiseinmotion/html/test3/pdf/font/unifont/DejaVuSansCondensed.ttf

What is the correct way so that I can use exiting pdf and can use utf-8 font?

Was it helpful?

Solution

You're asking two different questions here:

  1. why isn't FPDF defined in the first code sample, and

  2. why can't it load my font in the second code sample.

I'm going to answer #2 first, because it's easy: the font file is not being found. Check your path and make sure the code is looking for the font in the right place.

Regarding #1: I suspect the issue comes down to your require_once statements. Specifically, you're requiring fpdf.php in your code when the example code does not. My guess is that there's a conflict there somewhere.

Rather than spending a lot of time looking around the libraries for a problem, I would start with the example code (which comes from the developer's website, so we're pretty sure it works - maybe not, but let's assume it does until proven otherwise). Assuming that code works (and all you need to do there is fix the path to your font), start with that and modify it to do what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top