Undefined method with Composer library and CodeIgniter
Asked Answered
G

3

0

I'm working on a project and it's getting a little too hard for me... I explain.

I need to parse PDF files with PHP, to analyse the content of those files. To do that, I use pdfparser.org library. I firstly tried to include this library as usually, without any result. After having read all the Internet, since this library requires Composer to be installed (and on my web hosting I can't get Composer installed), I have applied the Composer process on my Windows PC. I got the "vendor" folder with the "autoload.php" file. Fine !!

Then, I have tried to include it properly in CodeIgniter. The solution I chose is :

  1. Creating a file "Pdfparser.php" in application/libraries/

    class Pdfparser
    {
        public function __construct()
        {
            require_once APPPATH."/third_party/pdfparser.php";
        }
    }
    
  2. Then, I add the PdfParser "Composer" application in application/third_party/, and in the /third_party/pdfparser.php I simply put :

    if (!defined('pdfparser')) {
        define('pdfparser', dirname(__FILE__) . '/');
        require(pdfparser . 'pdfparser/autoload.php');
    }
    
  3. Then, I add this library to CodeIgniter /application/config/autoload.php as :

    $autoload['libraries'] = array('pagination', 'form_validation','email','upload','pdfparser');
    
  4. Finally, I call it in my function in application/controllers/Admin.php :

    $parser = new Pdfparser();
            $pdf    = $parser->parseFile(myfile.pdf);
            $full_text = $pdf->getText();
    

    (This 4. block of code is directly taken from official Documentation here : http://www.pdfparser.org/documentation, and just adapted).

  5. But now, I break the Internet... I have this error :

    PHP Fatal error:  Call to undefined method PdfParser::parseFile() in /path/application/controllers/Admin.php on line 3083
    
  6. After having looked CodeIgniter documentation, I try to add the Composer autoloader to the core... in application/config/autoload.php I put :

    $config['composer_autoload'] = APPPATH . "/third_party/pdfparser/autoload.php";
    
  7. Of course, it doest not work. And I'm lost...

Glaydsglaze answered 20/12, 2016 at 8:47 Comment(2)
try like this...$config['composer_autoload'] = TRUE;Mauricio
Hikmat : I tried it, but nothing more... Then, I tried to add my Composer library directly in application/vendor folder, with $config['composer_autoload'] = TRUE; But nothing better...Glaydsglaze
S
1

Use composer properly. $config['composer_autoload'] = TRUE; and inside your application folder run composer install smalot/pdfparser . Then inside your controller it should run, if not use Use :)

use Smalot\PdfParser;
class My_controller extends CI_Controller {
}
Snuff answered 20/12, 2016 at 9:18 Comment(5)
Here is the problem ! I can't do this on my web hosting... And for example if I do this on my local storage, and then upload the generated "vendor" folder, would it work ?Glaydsglaze
i also don't have access throught ssh and it works, the way you've describedSnuff
Well it seems to work now since it finds the class and executes the code ! The only thing is that I get a "TCPDF_PARSER ERROR: Empty PDF data."... I'm checking where it comes from... Thanks !Glaydsglaze
I have solved all the problem !! Really thank you !!Glaydsglaze
You dont have to build own library, use components. You're welcome ;)Snuff
D
1

When using composer, to include a library in your project you do something like that :

composer install smalot/pdfparser

Then, to include the newly installed library, you only need to include the "autoload.php" file provided by composer :

<?php

include 'vendor/autoload.php';

$parser = new Pdfparser();
$pdf    = $parser->parseFile(myfile.pdf);
$full_text = $pdf->getText();

var_dump($full_text);

Nothing more.

Doner answered 20/12, 2016 at 12:41 Comment(1)
cf this article to use composer autoload in code igniter context : https://mcmap.net/q/595418/-codeigniter-composerDoner
M
0

Replace your code

class Pdfparser
{
    public function __construct()
    {
        require_once APPPATH."/third_party/pdfparser.php";
    }
}

with

   <?php

require_once APPPATH."/third_party/pdfparser.php";
class Pdfparser
{
    public function __construct()
    {
    }
}

Include outside of your class.

Rather than using autoloading you can load library like this...

$this->load->library('library_name');

Example:

 $this->load->library('pdfparser');
Mauricio answered 20/12, 2016 at 9:6 Comment(1)
Thank you ! I have tried both solutions, and I get the same error... I'm loosing hope :'(Glaydsglaze

© 2022 - 2024 — McMap. All rights reserved.