2

I want to add a png-image to an existing pdf file. Therefore I'm trying to integrate FPDI/FPDF into my project, that is based on the SLIM framework with ELOQUENT.

FPDI/FPDF is added with composer ( composer require setasign/fpdi-fpdf ).

I successfully testet the following code in a single php file ("pdf_test.php") directly opened in the browser:

require_once('../vendor/setasign/fpdf/fpdf.php');
require_once('../vendor/setasign/fpdi/fpdi.php');

$pdf = new FPDI();
$filename = '1005236946.pdf';
$pageCount = $pdf->setSourceFile($filename);
$templateId = $pdf->importPage(1);
$pdf->useTemplate($templateId);
$pdf->Image('9959544245.png',268,184,20, 'PNG');
$pdf->Output('F', '1005236946_PNG.pdf');

But when I use this code in SLIM route ("/php_test") I get a "500 Internal Server Error" at this point:

$pageCount = $pdf->setSourceFile($filename);

I've checked that the $pdf-object is created well.

I searched the web but found nothing so far that could help. Maybe it has to do something with the Apache Server and the .htaccess file... ?

Any help very appreciated :-)

Mika Tuupola
  • 19,877
  • 5
  • 42
  • 49
PeteMeier
  • 521
  • 1
  • 6
  • 18
  • General rule: If you get a 500, go look at the server's error_log for details – Marc B Aug 26 '16 at 17:20
  • check Slim support fpdf or not ? – Hassan ALi Aug 26 '16 at 17:28
  • error_log: PHP Warning: require(../app/models/pdf_parser.php): failed to open stream: No such file or directory. So it seem to relate to the autoloader... – PeteMeier Aug 26 '16 at 17:40
  • Please don't use relative paths. imo always use absolute paths. i,e, `require_once __DIR__ .'/../vendor/setasign/fpdf/fpdf.php';`. Yes, this is still an absolute path and is valid on windows, linux etc. It always works as it does not use the 'current working directory' (CWD), which is what normally causes relative filepath lookups to fail. Note: `include, require etc.` are language constructs not functions so don't need the parenthesis. – Ryan Vincent Aug 27 '16 at 13:15
  • i have also the same problem. i checked the chmod settings of the file to be loaded. i also put absolute paths everywhere. when i try to count the pages of a document, generated by fpdf its working, when it is one page only. when i try to read a fpdf file with more than one page or a scanned pdf file, it does not work. – Bernhard Dec 30 '16 at 14:34
  • " By default FPDI can "only" handle PDF documents up to PDF version 1.4. Beginning with PDF version 1.5 there were new compression features introduced which involve internal structure changes how a PDF document can be created. With this seperate parser, as a commercial addon, you're up to date and FPDI will be able to handle PDF documents with a version higher than 1.4 without problems." https://www.setasign.com/products/fpdi-pdf-parser/details/#p-168 – Bernhard Dec 30 '16 at 22:35

1 Answers1

0

error_log: PHP Warning: require(../app/models/pdf_parser.php):

This means that the autoload implementation doesn't check if a file exists and simply requires it. Because of such strange implementation a simple class_exist() call (which is the case here) on an not existing class will produce this error.

So fix the autoload implementation or require the parser separately:

require_once('../vendor/setasign/fpdi/pdf_parser.php');

Also I'm unsure why you require both script manually and why you do not use the autoloader of composer. (Wouldn't change the behaviour but would make more sense)

Jan Slabon
  • 4,736
  • 2
  • 14
  • 29
  • Still? This answer is 2 years old... If you get a 500 error, check your error logs and settings to get more details. – Jan Slabon Aug 24 '19 at 12:08