Error when trying to use the class using php - php

I found the code from internet which help me to extract pdf files to text.
this is the code that i have used.
<?php
// Include Composer autoloader if not already done.
//include 'vendor/autoload.php';
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('document.pdf');
$text = $pdf->getText();
echo $text;
?>
when i use this method to call the relevant class it gives an error on the browser.
Fatal error: Class 'Smalot\PdfParser\Parser' not found in D:\workspace_PHP\pdfparser-master\index.php on line 7
i am using php version 5.4
if anyone can tell me the reason, it will be great help.
Thanks in advance

You haven't included the smalot PDFparser library so it could not found the class file for instance..
you find it here..
https://github.com/smalot/pdfparser
Include this in your source directory and that is it!..

You need to include the Smalot class file to your PHP file. As we see there is no include on your PHP file.
Include it something like
include('/Smalot/PdfParser/Parser.php');
See the GitHub here

PDFParser has been designed on top of composer library.
I invite you to read composer documentation to understand how to generate and use the composer autoloader.
It's really easy to use and really powerful.
Install composer (http://getcomposer.org/) and type
composer.phar install
In the same folder as the "composer.json" file.
All dependencies will be downloaded into the "vendor" folder.
Once done, a file will be generated named "vendor/autoload.php".
You have just to include it at first in your script.
http://pdfparser.org/documentation

Related

FPDF with Laravel

I am attempting to use the FPDF (http://www.fpdf.org/) PHP addon for my website that uses Laravel. I want to be able to dynamically create a PDF.
I have stored the libraries' files in the folder: '/public/vendor'.
This is what I have so far:
require $_SERVER['DOCUMENT_ROOT'] . '/vendor/fpdf/fpdf.php';
which works fine. However when I try to use the FPDF class using:
$pdf = new FPDF('P', 'pt', array(500,233));
I get the error: Class "'App\Console\Commands\FPDF' not found"
How can I fix this to use the library. I do not have access to command line so I have to manually import any folders or files.
Any help is greatly appreciated.
Update:: I do not have any access to the console so i cannot use composer. Any way to still do this?
Ideally if using Laravel, you should load it via composer the get it to work correctly.
If you do not have access to composer on the server, you could download the project, make sure you are running the same version of PHP as the server and run composer install fpdf/fpdf. Re-upload the composer.json, composer.lock and vendor folder.
https://packagist.org/packages/fpdf/fpdf
Try following code:
$pdf = new \Fpdf\Fpdf('P', 'pt', array(500,233));
FPDF is in the root namespace and you are within another namespace.
Highly recommend using https://github.com/mccarlosen/laravel-mpdf which can be installed via composer
It supports blade views and you can pass variables to it just like in view()

Cannot find libraries added from github in native PHP

I'm working on a native PHP project. Using terminal in the root I added 2 github libraries and they get saved in the vendor folder. In a sub folder in the root in a PHP file I add
use nadar\quill\Lexer;
$lexer = new Lexer($row['reply']);
echo $lexer->render();
But I get
Uncaught Error: Class 'nadar\quill\Lexer' not found in /home/USER/public_html/production/filename.php:469
I tried another library and get the same error Cannot find the class. How to solve this? Tis is the first library url: https://github.com/nadar/quill-delta-parser
Every file where you need classes loaded by the autoloader need to include the autoload file. Otherwise, these classes are unknown by the interpreter.
Add this to the beginning of your file:
require __DIR__ . '/vendor/autoload.php';

mPDF : Methods with the same name as their class will not be constructors in a future version of PHP; mPDF has a deprecated constructor

I'm new to mpdf and I'm having a problem implementing it on my project. I saw a link in Facebook that has a tutorial for mpdf, I read and followed all the instructions but unfortunately there is still an error about some deprecated value.
Here is the link I followed : http://codesquery.com/convert-html-pdf-codeiniter-using-mpdf/
And here is the screenshot of the problem:
I hope you guys can help me.
I guess you use php 7. You must change names of function. In old php for example 5.19 the same name of function and class means that this function is a constructor. In PHP 7 it must be for example:
public function __construct(mPDF $mpdf){}
Click here to see a list of all modification
You can easily install mpdf by using composer in codeigniter. This is how I implemented composer in CodeIgniter 3.It is very easy. You have to install composer on your machine download it from https://getcomposer.org/. After installing composer in your pc,
Copy and paste composer.json file in the project folder to application folder.
In the config.php file $config['composer_autoload'] = TRUE;
Now you have composer in your project. Now i will saw you how to install mpdf using composer
Open cmd and direct to application folder
Inside application directory Type composer require mpdf/mpdf
Now a vendor folder will be created inside application folder and inside vendor folder you can see all your packages downloaded by composer.
Now since you autoloaded composer now you can just use the code given by mpdf official manual like in your controllers.
function m_pdf(){
$mpdf = new mPDF();
// Write some HTML code:
$mpdf->WriteHTML('Hello World');
// Output a PDF file directly to the browser
$mpdf->Output();
}
Remember you don't need to type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php'; since you already autoloader composer. If not prefer to autoload composer you must type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php' at the beginning of each controllers where you use the mpdf vendor libraries. If you encountered any problems please comment below.

Use Composer module in PHP file

I want to use the following AntiXSS library in one PHP file. It is my first time using Composer, but I followed their installation steps and I installed it successfully. I downloaded the library through Composer, updated it and Composer created the vendor/ folder in my directory with the necessary files.
Now I added the following require 'vendor/autoload.php' into my PHP file. I created a new AntiXSS class but I obtain the following error:
Class AntiXSS not found in my directory on line 3.
I tried to use an absolute path instead of vendor/autoload.php but it isn't working yet and I don't know if I should do something more.
Best regards
The class is located in the voku\helper namespace. Use new \voku\helper\AntiXSS() to instantiate it or use use imports to import the namespace.
See php.net for more information about namespaces.

Class not found error when try to use XBase lib in PHP

I am trying to use https://github.com/hisamu/php-xbase lib in my project.
I have copied XBase folder from repository to the my root and created index.php in my root.
When I try this code:
<?php
use XBase\Table;
$table = new Table(dirname(__FILE__) . 'data/test.dbf');
while ($record = $table->nextRecord()) {
echo $record->my_column;
}
?>
I received this error:
Fatal error: Class 'XBase\Table' not found in ...
What is wrong?
You are not requiring the file, that's why PHP can not find the class.
The example you see on GitHub, assumes you have installed and configured composer for your project.
To do this, download and configure composer, then run
composer require 'hisamu/php-xbase: *'
in root folder of your project. Then include vendor/autoload.php in your scripts. All installed classes using composer are now available.
Most frameworks do this for you, so you only use the class as mentioned. But when you are using a framework of yourself, or only plain PHP scripts, it's your responsibility to require autoload.php
Put these lines in the beginning of the table.php file:
include "Column.php";
include "Record.php";

Categories