• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't load pdf with using Mpdf in Codeigniter 4

#1
Excuse me , i need help .. why my code can't load pdf .? I'm using Mpdf .. how to load view if view made variable ??
this is my code

//CONTROLLER (app/controller)
<?php namespace App\Controllers;
use App\Models\PesertaModel;
use App\Models\KondisiModel;
use App\Models\UserModel;
use App\Models\GejalaModel;

use MPDF;
class Reporting extends BaseController
{
    public function cetakpdf()
    {
        $data['datapeserta'= $this->pesertaModel->getpeserta()->getResult();

        $sumber = view('test',$data);

        $mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8''format' => 'A4''margin_left' => 10'margin_top' => 10'margin_right' => 10'margin_bottom' => 1'orientation' => 'P' ]);

        $mpdf->WriteHTML($sumber);

        $this->response->setHeader('Content-Type''application/pdf');
        return redirect()->to($mpdf->Output('Cetak Peserta.pdf''I'));
    }
}




//AUTOLOAD (app/config/autoload.php)
$classmap = [
            'MPDF' => APPPATH .'Libraries/mpdf.php'
];



//LIBRARIES (app/libraries/mpdf.php)

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class mpdf {
    
    function m_pdf()
    {
        $CI = & get_instance();
        log_message('Debug', 'mPDF class is loaded.');
    }
 
    function load($param=NULL)
    {
        include_once APPPATH.'/third_party/mpdf60/mpdf.php';
         
        if ($params == NULL)
        {
            $param = '"en-GB-x","A4","","",10,10,10,10,6,3';          
        }
         
        return new mPDF($param);
    }
}
Reply

#2
Are you using CodeIgniter 3 or CodeIgniter 4 ???
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
having a quick look

your using
Code:
$classmap = [
            'MPDF' => APPPATH .'Libraries/mpdf.php'
];

so if my coffee has kicked in you should not be using php require stuff

what you will need somewhere is something like
Code:
use \namespace\mPDF;

where did you get mPDF from ?
Reply

#4
(07-09-2020, 03:20 AM)InsiteFX Wrote: Are you using CodeIgniter 3 or CodeIgniter 4 ???
I'm using Codeigniter 4
Reply

#5
(07-09-2020, 03:21 AM)captain-sensible Wrote: having a quick look

your using
Code:
$classmap = [
            'MPDF' => APPPATH .'Libraries/mpdf.php'
];

so if my coffee has kicked in you should not  be using php require stuff

what you will need somewhere is something  like
Code:
use \namespace\mPDF;

where did you get mPDF from ?

I'm get mpdf in my controller , this is my code 

use MPDF
Reply

#6
i will have a look over weekend ; it might be something useful for me in providing a pdf download for a blog. This then is just for starters.



i think i found what you are using :https://github.com/mpdf/mpdf


I have edited the post; I have taken a step back to review the issue.

First can we clarify your purpose. You obviously show code that doesn't work ; but is the objective to create a pdf on the fly from say txt or html , or simply that you want to have a download for say a blog.

Quite often on web sites you see a web page and they offer that you can download what you see in as pdf file, so you can read offline. Probably laterally thinking my approach to provide that is going to be produce html ; do a conversion offline to pdf , load that to server with a download link.

Now if you want to produce a pdf using code from say a controller thats something different.  I have used composer but for my local dev i'm not using it. Probably if you just use for Mpdf :
Code:
composer require mpdf/mpdf

that will make life easier. For me this is where i am with it. I'm looking at not just mPDF but also fpdf library and html2pdf library to see ease of use etc.

i got a pdf fairly easily with fpdf but the class has no namespace so i'm going to play with things and come back if i get anywhere.
Reply

#7
ok i got it working as follows:

first i did manual download of :
https://github.com/spipu/html2pdf/releases/tag/v5.2.2

I unzipped it and renamed it to : html2pdf i placed it at location as seen below .

That dir when you loo inside  looks like  this:

Code:
html2pdf
├── CHANGELOG.md
├── LICENSE.md
├── README.md
├── composer.json
├── doc
├── examples
├── phpunit.xml
├── src
└── test



The classes you want to use are in src.
Now lets look where i put the html2pdf directory:



Code:
ginbrookes
├── PHPMailer
├── admin
├── app
├── composer.json
├── composer.lock
├── contributing
├── fontawesome
├── gulpfile.js
├── html2pdf
├── node_modules
├── package-lock.json
├── package.json
├── public
├── scss
├── system
├── vendor
└── writable
ginbrookes is the name of my CI4 development directory in apache. There is some stuff like sccs and gulpfile.js you might not have so ignore that.


Now taking into consideration where the directoy of  html2pdf is and its name space of the classes of :

Code:
Spipu\Html2Pdf


I let CI4 about this in app/Config/Autoload.php as follows:

Code:
$psr4 = [
            'Config'      => APPPATH . 'Config',
            APP_NAMESPACE => APPPATH,                // For custom namespace
            'App'         => APPPATH,                // To ensure filters, etc still found,
            
             'Forms'  => ROOTPATH.'forms',
             'PHPMailer\\PHPMailer'=>ROOTPATH.'PHPMailer/src',
             'Spipu\\Html2Pdf'=>ROOTPATH.'html2pdf/src'
            
            
        ];


Note the 2 backslashes in Spipu\\Html2Pdf , thats because one backslash escapes the first one.

Now in a controller to make use of html2pdf you have to declare in controller


i just used one of html2pdf examples. :


Code:
<?php namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\I18n\Time;
use Spipu\Html2Pdf\Html2Pdf;

class Spam  extends BaseController
{

//then in a method i just used simple example
public function test()
{

$html2pdf = new Html2Pdf();
$html2pdf->writeHTML('<h1>HelloWorld</h1>This is my first test');
ob_end_clean();
$html2pdf->output('my_doc.pdf', 'D');
}    
    

}


couple of things i made sure that the directory of html2pdf had the appropriate ownerships .Also i got a
Code:
throw new Exception('TCPDF ERROR: '.$msg);


when i added the line


Code:
ob_end_clean();


that fixed it and i got a dialogue box come up to save a file called my_doc.pdf which when opened with a pdf viewer opened and displayed "hello world".

Thats a start now just a question of adpating to your needs also obviously you need to set up a route in my case taking int oaccount above would be using Spam::test
Reply

#8
I have been using Mpdf in my CI 3 projects and tested it in CI 4.  The method I used was as below:

I installed Mpdf via composer.

Then I created my own helper function to create the Mpdf object.  I needed to add custom fonts hence the additional code prior to obtaining the Mpdf object.

The Helper function definition
PHP Code:
function myPdf$orientation ) {

   $defaultFontConfig = ( new Mpdf\Config\FontVariables() )->getDefaults();
   $fontData          $defaultFontConfig'fontdata' ] + [
         'homemadeapple'        => [
            'R' => 'HomemadeApple-Regular.ttf',
         ],
         'roboto'          => [
            'R'  => 'Roboto-Regular.ttf',
            'I'  => 'Roboto-Italic.ttf',
            'B'  => 'Roboto-Bold.ttf',
            'BI' => 'Roboto-BoldItalic.ttf',
         ],
         'robotocondensed' => [
            'R'  => 'RobotoCondensed-Regular.ttf',
            'I'  => 'RobotoCondensed-Italic.ttf',
            'B'  => 'RobotoCondensed-Bold.ttf',
            'BI' => 'RobotoCondensed-BoldItalic.ttf',
         ],
      ];
   $defaultConfig     = ( new Mpdf\Config\ConfigVariables() )->getDefaults();
   $fontDirs          $defaultConfig'fontDir' ];
   if ( $orientation == 'L' ) {
      $myPdf = new \Mpdf\Mpdf(
         array(
            'format'   => 'A4-L'
            'margin_left' => 10
            'margin_right' => 10
            'margin_top' => 30
            'margin_bottom' => 20
            'margin_header' => 8
            'margin_footer' => 8
            'orientation' => 'L'
            'mode' => 's',
            'fontDir'  => array_merge$fontDirs, [ FCPATH 'public/assets-app/fonts', ] ),
            'fontdata' => $fontData,
         ) );
   }
   else {
      $myPdf = new \Mpdf\Mpdf( array(
         'format'        => 'A4-P',
         'margin_left'   => 20,
         'margin_right'  => 10,
         'margin_top'    => 40,
         'margin_bottom' => 20,
         'margin_header' => 8,
         'margin_footer' => 8,
         'orientation'   => 'P',
         'mode'          => 's',
         'fontDir'       => array_merge$fontDirs, [ FCPATH 'public/assets-app/fonts', ] ),
         'fontdata'      => $fontData,
      ) );
   }

   return $myPdf;



Usage in a controller:
PHP Code:
   public function testPdf() {
      $pdf myPdf('P');

      $pdf->writeHTML"Hello World" );

      $pdf->OutputFCPATH "writable/uploads/test.pdf"'F');

      return redirect()->tosite_url('/') );

   
In my opinion mPdf works extremely well when taking an HTML block and sending it to writeHTML.  I found it much simpler and easier to use than for example tcpdf.

Regards
Dirk B.
Dirk B.
Abatrans Software
No SEO spam - see forum guidelines
Reply

#9
ok well at least there are a couple of options. I'm still playing with mine; I have a base dev app; for that I used composer for both CI4 appstarter and Spipu\Html2Pdf . The benefits easy to update and also it autoloads classes .
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.