0

I am using the FPDI library from JanSlabon for securing PDF file uploads from my laravel app. But I can't execute the code require_once even though I navigated to the file itself. I am getting the error:

Failed opening required '../../vendor/setasign/fpdf/fpdf.php' (include_path='.:/usr/local/Cellar/php/7.3.4/share/php/pear')

My require code is:

require_once('../../vendor/setasign/fpdf/fpdf.php');
require_once('../../vendor/setasign/fpdi/src/autoload.php');
ejuhjav
  • 2,660
  • 2
  • 21
  • 32
zernzern1993
  • 235
  • 8
  • 19

3 Answers3

1

Your relative path ../../ to vendor is probably wrong. To avoid this issue, use the Laravel base_path() helper which will provide an absolute path.

require_once(base_path('vendor/setasign/fpdf/fpdf.php'));
require_once(base_path('vendor/setasign/fpdi/src/autoload.php'));
matticustard
  • 4,850
  • 1
  • 13
  • 18
1

When the libraries are already located in your vendor folder, you should simply make use of the autoload.php file of composer (doesn't laravel uses this by default?).

So just add the dependencies to your composer.json (if not already done):

"require": {
    "setasign/fpdf": "^1.8",
    "setasign/fpdi": "^2.2",
    "setasign/fpdi-protection": "^2.0"
}

Update via composer update and:

<?php
use setasign\FpdiProtection\FpdiProtection;

require_once('vendor/autoload.php');

$pdf = new FpdiProtection();
...
Jan Slabon
  • 4,736
  • 2
  • 14
  • 29
  • Thank you this worked perfectly after also adding base_path() to the require_once cuz for some reason laravel cant find my vendor folder – zernzern1993 Aug 15 '19 at 08:07
  • Sorry to bother you but I am getting an error of "Call to undefined method setasign\FpdiProtection\FpdiProtection::FPDF()" when using the method "$pdf->FPDF('P', 'in', array('6','9'));" been looking for solutions for a while now :( – zernzern1993 Aug 15 '19 at 08:22
  • Why would you call the constructor that way? `$pdf = new FpdiProtection('P', 'in', array('6','9'));` should do the job? – Jan Slabon Aug 15 '19 at 09:41
  • Ahhh I see now sorry I am new to coding... I managed to output and created a encrypted pdf but once I use the method "->Output($fileName, 'F')" it generates the pdf but it generates it in the vendor folder, is there a way to change this path? – zernzern1993 Aug 15 '19 at 23:51
  • The `$name` parameter can also be a path, sure. – Jan Slabon Aug 16 '19 at 07:34
0

You can autoload using composer.json. First of all, create a directory called Custom in app directory and copy fpdi directory to app/Custom.

Now in autoload section of your composer.json file, require the file. After requiring the file, your composer.json file's autoload block should look like this if it is a fresh Laravel app:

"autoload": {
    "psr-4": {
        "App\\": "app/"
    },
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "files": [
        "app/Custom/fpdi/FPDI_Protection.php"
    ]
},

After updating your composer.json file, run composer dumpautoload. Now you can utilize the classes in your Laravel controllers or models without requiring the files manually.

While doing tests, I see that this library uses some deprecated methods and so on. You will have to deal with it, i.e. update the code to suite your needs. But I hope that this answer will help you in a way that you will be able to use any other library as well. Do a Google search and find a more modern library if this one's fixes are too broad.

Rehmat
  • 4,681
  • 3
  • 22
  • 38
  • All packages are available through packagist/composer so there's nothing to load manually. – Jan Slabon Aug 15 '19 at 05:57
  • @JanSlabon That's not true. A lot of old and rare code pieces are not available via packagist and sometimes you need to manually load certain libs. – Rehmat Aug 15 '19 at 05:59
  • For sure with "all packages" I mean the packages that were used/needed by the author. – Jan Slabon Aug 15 '19 at 09:42