in cakephp 3.x , using plugin cakepdf
i tried wkhtmltopdf , dompdf , both of them had same problem>>> cannot download real pdf file, downloads plain text file.
my bootstrap.php:
plugin::load('cakepdf', array('bootstrap' => true, 'routes' => true)); routes.php: .......  router::scope('/users/view', function ($routes) {     $routes->extensions('pdf');     $routes->connect('/view/*', ['controller' => 'users', 'action' => 'view']);     $routes->fallbacks('inflectedroute'); });  router::scope('/', function ($routes) { ......   everything ok, when click in browser this:
/cakephp/users/view/5.pdf
i dont proper pdf file, instead of download text file (the view ctp seems work)...any idea??? :
 <!doctype html> <html> <head>     <meta http-equiv="content-type" content="text/html; charset=utf-8"/>    <title>         users\pdf    </title>     <link rel="stylesheet" href="http://localhost/fct/css/style.css"/></head> <body> <h2>informe pdf</h2>     <div id="container">         <div id="content">             <div class="users view"> <h2>user</h2>     <dl>         <dt>id</dt>         <dd>             2                      </dd>          <dt>username</dt>         <dd>             tutor666                      </dd>         <dt>created</dt>         <dd>             8/5/16, 7:34 pm                      </dd>         <dt>modified</dt>         <dd>             8/5/16, 7:34 pm                      </dd>     </dl> </div>          </div>     </div> </body> </html>   so maybe router.php wrong?...  router::scope('/users/view', function ($routes) { $routes->extensions('pdf'); $routes->connect('/view/*', ['controller' => 'users', 'action' => 'view']); $routes->fallbacks('inflectedroute'); –   
reviewing cakepdf docs seems "/cakephp/users/view/5.pdf" right way....but stills download text file instead of pdf file. feel desperate!!! –
the file should 'user_15.pdf' '15.pdf, maybe code not working: userscontroller:view(): $this->viewbuilder()->options([ 'pdfconfig' => [ 'orientation' => 'portrait', 'filename' => 'user_' . $id . '.pdf' ] ]);
i @ debug history , says "get 200 application/pdf " can see above download text/text header.
here how implement cakepdf. use dompdf can installed via composer.
step 1. install cakepdf plugin
composer require friendsofcake/cakepdf   step 2. install dompdf engind
composer require dompdf/dompdf   step 3. make sure have requesthandler component loaded in appcontroller.php ([rootdir]/src/appcontroller.php)
public function initialize() {     parent::initialize();      $this->loadcomponent('requesthandler'); }   step 4. enable cakepdf plugin in bootstrap.php
plugin::load('cakepdf', ['bootstrap' => true]);   step 5. setup appropriate routes in routes.php
router::scope('/', function (routebuilder $routes) {     $routes->extensions(['pdf']);     /** other route setup **/     $routes->fallbacks(dashedroute::class); });   step 6. create pdf folder under templates/layouts folder , create pdf.ctp file contain following
<?= $this->fetch('content'); ?>   the above acts blank layout pdf. can use whatever markup appropriate case
step 7. in template folder want generate pdfs in cases users (i'm assuming [rootdir]/templates/users/), create pdf folder , create view file in pdf folder (i.e [rootdir]/templates/users/pdf/view.ctp). contain markup view. example use
<?= $show;?>   step 8. in controller function (assuming view also). should contain following
public function view () {     /** i'm displaying "text" on pdf file **/     $show = "text";      $this->viewbuilder()->options([         'pdfconfig' => [             'engine' => 'cakepdf.dompdf',             'orientation' => 'portrait',             'filename' => 'pdf view'             /** other config options can go here**/         ]     ]);      $this->set(compact('show'));     $this->set('_serialize', ['show']); }   step 9. point browser [webpath]/users/view.pdf , should have pdf file showing "text"
you can adapt needs
cheers.
Comments
Post a Comment