Welcome Guest, Not a member yet? Register   Sign In
mpdf class not fount error CI 4.1.5
#1
Shocked 

I am using CI 4.1.5, installed from composer. On fedora 35 x86_54.

Code:
$ composer create-project codeigniter4/appstarter ci4-mpdf


I installed mpdf from composer as well, the code I use in the invocation is:

Code:
$ composer require mpdf/mpdf


PHP Code:
<?php
namespace App\Controllers;

class 
Home extends BaseController{
     public function index(){
         $mpdf = new \Mpdf\Mpdf();
         $html view('html_to_pdf',[]);
         $mpdf->WriteHTML($html);
         $this->response->setHeader('Content-Type''application/pdf');
         $mpdf->Output('arjun.pdf','I');
     }



And I also modified it as follows:

PHP Code:
<?php
namespace App\Controllers;
use 
Mpdf\Mpdf;

class 
Home extends BaseController{
     public function index(){
         $mpdf = new Mpdf();
         $html view('html_to_pdf',[]);
         $mpdf->WriteHTML($html);
         $this->response->setHeader('Content-Type''application/pdf');
         $mpdf->Output('arjun.pdf','I');
     }


and the error show this:

Code:
Error
Class "Mpdf\Mpdf" not found
APPPATH/Controllers/Home.php at line 43

38    {
39        return view('welcome_message');
40    }
41
42    public function pdf(){
43        $mpdf = new Mpdf();



Checking the composer autoload file, I found that it does not include the mpdf installation path, I tried to regenerate it with composer dump-autoload and it still does not work.

I tried to add it manually, and it doesn't work either. Is it necessary to modify some other composer path or add the mpdf installation manually?

Thanks!
Reply
#2

if what your trying to do is so surfers can download a pdf version of your web page , then i can tell you i have had success with : https://packagist.org/packages/tecnickcom/tcpdf


Now i installed that via entry in composer.json file but it didn't work

Now in that particular case, the tcpdf.php main file had no namespace , so i had to load it in app/Config/Autoload.php using :

Code:
public $classmap = [  'TCPDF'=>ROOTPATH.'tecnickcom/tcpdf/tcpdf.php'
   ];


if mpdf files have a namespace then you can try running :

Code:
composer dump-autoload


have a look inside class files to see if they have a namespace or not.
CMS CI4     I use Arch Linux by the way 

Reply
#3

Thank you for your response.

I need mpdf or tcpdf, to generate sales reports, tickets, inventory, or reports cash dynamically.
Reply
#4

(This post was last modified: 11-19-2021, 11:04 AM by captain-sensible. Edit Reason: forgot something )

yeah i've got it working for blog postings - basically using output from database and links in view , whaterver blog is posted its possible to download a pdf of it.

i can't show you live since waiting for daughter to come up with one. I'll have a look maybe over week end at mpdf .

if its not considered shameless promotion of my CMS by mods you can download :
https://sourceforge.net/projects/codeigniter4cms/ unzip go into controllers

Code:
and look at  ProducePDF.php  in controllers.

look at app/Config/Autoload.php to see how i make sure class  can be found by system .


look at blogArticle.php  in vews to see how i dynamically list link to produce pdf

look at routes  , particularly :   $routes->get('linkPdf/(:segment)','ProducePDF::getPdf/$1');

if you want the thing to run, go into route of unzip and run from cli (depending on how you have composer set up , better still if your on linux ) : composer install


thats because to cut down on download i didn't upload vendor.


that should give you a clue of basics how i did it
CMS CI4     I use Arch Linux by the way 

Reply
#5

(11-19-2021, 11:03 AM)captain-sensible Wrote: yeah i've got it working for blog postings - basically using output from database and  links in view , whaterver blog is posted its possible to download a pdf of  it.

i can't show you live since waiting for daughter to come up with one. I'll have a look maybe over week end at mpdf .

if its not considered shameless  promotion of my CMS by mods you can download : 
https://sourceforge.net/projects/codeigniter4cms/    unzip go into controllers

Code:
and look at  ProducePDF.php  in controllers.

look at app/Config/Autoload.php to see how i make sure class  can be found by system .


look at blogArticle.php  in vews to see how i dynamically list link to produce pdf

look at routes  , particularly :  $routes->get('linkPdf/(:segment)','ProducePDF::getPdf/$1');

if you want the thing to run, go into route of unzip and run from cli  (depending on how you have composer set up , better still if your on linux )  :  composer install


thats because to cut down on download i didn't upload vendor.


that should give you a clue of basics how i did it

thanks, i try it.
Reply
#6

Are you sure the installation is in ci4-mpdf directory?
Reply
#7

(11-21-2021, 04:18 AM)nfaiz Wrote: Are you sure the installation is in ci4-mpdf directory?

Hi, yeap.

The files package are inside vendor folder /mpdf/mpdf
Reply
#8

(This post was last modified: 11-22-2021, 11:20 AM by ferb.)

II found the error:

On my local lo server, I found that a php library was missing for gd. And this was causing mpdf version 6.1.3 to be installed, the current version is 8.0.10, however composer was not installing it.

So I installed the missing library, updated composer and everything worked perfectly, now I can use the mpdf library.
Reply
#9

(This post was last modified: 11-28-2021, 03:50 AM by captain-sensible. Edit Reason: spelling )

Recently i had trouble with html 2 pdf on the fly on my live server hosting. While getting help from web support, I had a play with Dompdf. I also had a quick look at mpdf and then back to the original library tcpdf.


if your requirement is just getting text from html then , mpdf ( https://packagist.org/packages/mpdf/mpdf ) is really easy.

Composer should not be run as root, so that can pose some problems running composer from inside your web app on live server , i've found particularly if web app is a sub domain or directory within your main hosting. Maybe its my hosting ?

A hack around running composer live, i've found, is simply to get things working on local dev , running composer on local dev, zipping up the whole vendor directory and then over writing the vendor directory on the live server. Its worked for me !

So on local dev say to get mpdf , from a linux command line ( you are using linux i hope ) cd to web root and run :
Code:
composer   require mpdf/mpdf


Doing that should put mpdf library in your CI vendor directory, and you should also see it updates your composer.json file eg :


Code:
"require": {
        "php": ">=7.2",
        "codeigniter4/framework": "^4",
        "twbs/bootstrap": "^4.5",
        "tecnickcom/tcpdf": "^6.4",
        "dompdf/dompdf": "^1.1"
        
    },



Now the autoloader should take care of your CI app finding the library.

The way I use it is on the html page for a blog, is to present a link which includes the slug of the blog title.

So basically when someone clicks on a get page as pdf link , it goes to a route ,which then invokes a controller class and its method to do the work.

IN my case the link( for a blog entry ) is in the form :

Domain.com/linkPdf/slug

This will invoke route :

Code:
$routes->get('linkPdf/(:segment)','ProducePDF::getPdf/$1');


The controller class is ProducePDF and the class method is : getPdf()

Basically it will retrieve from the database the blog title, article and image name,


Anyway, let me give a simple example of using mpdf in a controller.

At the beginning of the controller :

Code:
use Dompdf\Dompdf;

the above means use namespace called Dompdf and use the class also called Dompdf


Now the basics of using mpdf in a controller are :



Code:
$mystring ="<h1>   some html text </h1>";
$someTitle = " hello world";

$dompdf->loadHtml($mystring);

// set paper size and orientation
$dompdf->setPaper('A4', 'portrait');

// render html as pdf

$dompdf->render();

$dompdf ->stream($someTitle".pdf");
// what you will see at this stage is a save fiel coming up in your browser

How do you evoke controller in the first place. Well its easy if the html is stored in a database with say field for say title, main article , slug, image, then all you have to do is get the strings for the text , containing html

then you can use a link on a view, which uses a route so that it goes t o a controller to do all the work

Since my daughter has now got around to doing her first blog post , you can see html 2 pdf, being produced on the fly by clicking "cms in action " below my post then on menu blogs -> click on the single listing -> bottom of page -> pdf link.

You should get a pdf file that has the blog image inserted.


Now I played with mdpf to insert image and it was a bit of a pain conversion to base64 being needed, so i just used tcpdf instead.

getting html2pdf is more complicated with tcpdf
CMS CI4     I use Arch Linux by the way 

Reply
#10

I get every thing working on my local system using vhosts, then I upload ftping every thing to the root.

Only change I have to make is change public to public_html because my live server uses that.
I change it on my local server ftp to root and it just runs right off.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB