Search code examples
phplaravelcomposer-phpfpdf

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?


Solution

  • There is now a package https://github.com/Setasign/tfpdf (released in September 2018, a couple of month after the question was asked here)

    Install with composer like this:

    composer require setasign/tfpdf
    

    you can either use it like this:

    namespace your\namespace;
    
    class Document extends \tFPDF
    

    or like this:

    $pdf = new \tFPDF();