Avatar of Marthaj
MarthajFlag for United States of America

asked on 

PHP Class Problem with trying to execute pdftotext class

I have been struggling for 2 days on trying to resolve a problem and need help. I am using localhost, Win10 and Wampserver.
My goal is to use pdftotext class for php and composer. I have been trying to follow the instructions at URL:
https://php-xpdf.readthedocs.io/en/latest/

Open in new window

The first problem was trying to install composer. I already have curl installed, PHP 5.6.25 installed but when I tried installing using their curl example :
# Install composer
curl -s http://getcomposer.org/installer | php
# Upgrade your install
php composer.phar install

Open in new window

it errored out.
So I installed Composer from the URL:
https://getcomposer.org/doc/00-intro.md

Open in new window

and selected 'developer' and from the cmd line, I executed composer -v. It appears to be installed correctly. (In my previous attempts, I used the option of everyone but thought better of it so I uninstalled it, reboot my computer and installed using developers mode.
The next step was to download the pdftotext.
I unzipped it to my project folder, made a folder ""php-xpdf/php-xpdf" and moved the contents of the bin32 to "php-xpdf/php-xpdf"
 folder in my project folder.
I noticed that the folders in the Vendor folder, log,monolog,php-console,psr all have a composer.json file.
This is URL for the xpdf:
http://www.xpdfreader.com/download.html

Open in new window


My project path is:
c:\wamp\www\PDFConvert

Open in new window

My Vendor folder path is:
c:\wamp\www\PDFConvert\Vendor

Open in new window


This is the original coding snippet recommended to me:
use Monolog\Logger;
use Monolog\Handler\NullHandler;
use XPDF\PdfToText;

// Create a logger
$logger = new Logger('MyLogger');
$logger->pushHandler(new NullHandler());

// You have to pass a Monolog logger
// This logger provides some usefull infos about what's happening
$pdfToText = PdfToText::load($logger);

// open PDF
$pdfToText->open('PDF-book.pdf');

// PDF text is now in the $text variable
$text = $pdfToText->getText();
$pdfToText->close();

Open in new window


And the composer.json:  (it's located at the root of my project - C:\wamp\www\PDFConvert)
{
    "require": {
        "monolog/monolog": "^1.25",
        "php-console/php-console": "^3.1"
	"php-xpdf/php-xpdf": "master"
    }
}

Open in new window

I even preceded the previous locations with vendor/ and tried it.
I keep getting the same error:
 Class 'XPDF\PdfToText' not found in C:\wamp\www\PDFConvert\testpdf.php on line 16
See attached image.

I am thinking this is just a pathing problem and stupidity on my part - but I sure as heck can not seem to get it right no matter what I try.
Any help would be appreciated, Thank you
Class-XPDFPdfToText-Error.png
PHP

Avatar of undefined
Last Comment
Chris Stanyon
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

It's hard to tell what the first issue is.

1) Do you have composer installed?  composer -v is what shows you have it installed.

2) Did you install xpdf?
In order to use PHP-XPDF, you need to install XPDF. Depending of your configuration, please follow the instructions at on the XPDF website. and the link they provide goes to http://www.xpdfreader.com/download.html

3) " Class 'XPDF\PdfToText' not found in C:\wamp\www\PDFConvert\testpdf.php on line 16"
Post your entire code for testpdf.php

4) What are the major folders you have in your vendor file?  Do you have all the dependencies? This is where using composer comes in.

5) Are you loading the required files? With composer there should be a line
require 'vendor/autoload.php';

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi there,

It sounds like you're not installing the package correctly. The whole point of Composer is that you use that to install your project's dependencies.

Once you've got composer installed, open a terminal (command prompt on windows) at the root folder of your application. Type compser -v and press enter. That will tell you if it's installed correctly.

If all is well, then type the following:

composer require php-xpdf/php-xpdf

And press enter. Composer will download and install the package along with any dependencies into the vendor folder.

Now just add the autoloader to your php file and you should be good to go. Add it at the top of your php file:

<?php
require 'vendor/autoload.php';

...carry on coding

Open in new window

Avatar of Marthaj
Marthaj
Flag of United States of America image

ASKER

Thank you both for responding - I appreciate it. When I installed Composer, I had read to issue composer -v at the cmd line, which I did and it displayed  all the commands  so that was good and I tried again to just make sure.
when I entered at the cmd line, composer require php-xpdf/php-xpdf - it did not like it, because in the preceding line,
I had forgotten to added a comma after the statement "php-console/php-console": "^3.1".
Once I added the comma, it accepted the "require php-xpdf/php-xpdf" but rejected the two previous statements of:
       "vendor/monolog/monolog": "^1.25",
        "vendor/php-console/php-console": "^3.1",

Open in new window

on the basis of "The requires package vendor monolog/monolog could not be found in any version. there may be a typo error in the package name"
See attached image...
I am researching it now.
Sure appreciate you both helping...

So that is where I stand on it.
Screenshot_1.png
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Marthaj
Marthaj
Flag of United States of America image

ASKER

Thank you thank you !! Now I understand what composer really wanted and NOW some of what I read makes sense now. I read so many different things on how to install this part and that part. And I read that about composer being able to do such a thing, but I could not make it happen,  (https://getcomposer.org/doc/00-intro.md,https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup etc.)
 This is a very new area for me but it reminds of what we had back in the mainframe days to pull all needed libraries into a program....yes, I am that from that era of the 60's and 70's. (then in 80's - start pc with Commodore - wrote a program for labels using the Basic language - waaaay back and when).
This might be a silly question but would I be right in executing "composer require php-xpdf/php-xpdf php-console/php-console" when I deploy to my client ?? How does this work on a shared server situation ??
It makes sense to me that I would need to do so...again thank you !!
Avatar of Marthaj
Marthaj
Flag of United States of America image

ASKER

Thank you both very much. I appreciate you folks responding and sharing your knowledge. It's a new area for me and as you see, I have a lot to learn but learn I will and I am sure to have more questions !!
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Good stuff. It does take a little while to get your head around, but once it clicks, adding packages to your app becomes very simple.

Generally speaking, when you share your code with other users / team mates, you tend not to share the vendor folder. Instead you share (commit) the composer.json and composer.lock files. The other users then just run:

composer install

That will read in the composer.json file that you've shared and install all of the project's dependencies (at the correct versions).

When you call composer require, that's you as the developer stating 'my application requires these packages'. Once done, other developers just install the packages that you've told them they need.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo