Rob Monhemius
Rob Monhemius

Reputation: 5144

Using FPDF with composer autoloading

I can use the FPDF class if I require the right file from my library:

<?php namespace MyNamespace;

require_once '../../../vendor/setasign/fpdf/fpdf.php';
// use FPDF;

$pdf = new \FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();

Note: the fpdf.php file is not namespaced.

Composer should be autoloading this file for me. A snippet from my autoload_classmap.php generated by composer:

// autoload_classmap.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'FPDF' => $vendorDir . '/setasign/fpdf/fpdf.php',
    'File_Iterator' => $vendorDir . '/phpunit/php-file-iterator/src/Iterator.php',

Another library, phpunit is loading fine.

How can I autoload the FPDF library with composer and use: use FPDF;?

Upvotes: 2

Views: 5018

Answers (1)

Rob Monhemius
Rob Monhemius

Reputation: 5144

I neglected to require the autoload.php file from the vendors folder. Because the function is global I don't even have to add use FPDF;.

<?php namespace MyNamespace;

require_once '../../../vendor/autoload.php';

$pdf = new \FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();

Phpunit was working because phpunit is being accessed from the commandline and I had phpunit set up to include the autoload.php file.

I hope this helps someone else too. It's a little frustrating when you spend hours on something seemingly simple like this.

Upvotes: 2

Related Questions