Can't load pdf with using Mpdf in Codeigniter 4 |
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); } }
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 )
having a quick look
your using Code: $classmap = [ 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 ? (07-09-2020, 03:21 AM)captain-sensible Wrote: having a quick look I'm get mpdf in my controller , this is my code use MPDF
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.
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 The classes you want to use are in src. Now lets look where i put the html2pdf directory: Code: ginbrookes 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 = [ 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; 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
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 ) { Usage in a controller: PHP Code: public function testPdf() { Regards Dirk B.
|
Welcome Guest, Not a member yet? Register Sign In |