Welcome Guest, Not a member yet? Register   Sign In
mpdf class not fount error CI 4.1.5
#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


Messages In This Thread
mpdf class not fount error CI 4.1.5 - by ferb - 11-18-2021, 11:07 AM
RE: mpdf class not fount error CI 4.1.5 - by ferb - 11-18-2021, 02:22 PM
RE: mpdf class not fount error CI 4.1.5 - by ferb - 11-19-2021, 01:00 PM
RE: mpdf class not fount error CI 4.1.5 - by ferb - 11-21-2021, 05:13 PM
RE: mpdf class not fount error CI 4.1.5 - by ferb - 11-22-2021, 11:18 AM
RE: mpdf class not fount error CI 4.1.5 - by captain-sensible - 11-28-2021, 03:43 AM



Theme © iAndrew 2016 - Forum software by © MyBB