Autoload tFPDF class with composer

I want to use the tFPDF class and load it with composer-autoload. Since I could not find an official tFPDF composer repo, I simply downloaded the zip file and extracted it in the folder vendor/tfpdf.

Next I added the psr-4 to the composer.json file:

 "autoload": {
        "psr-4": {
            "App\\": "app/",
            "tFPDF\\": "vendor/tFPDF"
        },

I also added in the tfpdf.php the namespace

<?php
namespace tFPDF;

define('tFPDF_VERSION','1.25');

class tFPDF
{

Finally I produced a new autoload file:

composer dump-autoload

When I now try to create a PDF like this:

$pdf = new \tFPDF\tfpdf();
$pdf->AddPage();

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

Then the last line will rise an error:

Cannot access private property tFPDF\TTFontFile::$charWidths

enter image description here

Why is that and how can I fix it?

1 answer

  • answered 2018-07-18 06:22 Adam

    I had to add the following line to ttfonts.php

    namespace tFPDF\font\unifont;
    

    and in tfpdf.php I had to add

    namespace tFPDF;
    
    use tFPDF\font\unifont\TTFontFile;
    

    and remove

    require_once($this->_getfontpath().'unifont/ttfonts.php');
    

    in row 507 and 1851