CakePHP 2 : Implement CakePDF plugin by Ceeram

陌路散爱 提交于 2020-01-05 04:54:06

问题


I'm trying to implement the CakePDF plugin designed by Ceeram.

I'm using CakePHP 2 and I work locally using wamp on windows vista. I followed everything from the readme file but I got stucked at some point.

What I'd firstly like to do is converting an HTML link to a PDF using the WkHtmlToPdf engine. I see many people having issues to make it work so I'm gonna detail all the way through in the following different steps.


STEP 1: CakePdf plugin by Ceeram

I downloaded the plugin at https://github.com/ceeram/CakePdf I extracted the contained folder into app/Plugin/CakePdf


STEP 2: Bootstrap

I added the following lines - app/Config/bootstrap.php :

CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));

Configure::write('CakePdf', array(
    'engine' => 'CakePdf.WkHtmlToPdf'
    ),
    'orientation' => 'portrait',
    'download' => true
));

STEP 3: Controller

I've created my controller "InvoicesController.php" - app/Controller/InvoicesController.php:

class InvoicesController extends AppController {

    public $components = array('RequestHandler');

    public function view($id = null) {

        $this->Invoice->id = $id;

        if (!$this->Invoice->exists()) {
            throw new NotFoundException(__('Invalid invoice'));
        }

        $this->pdfConfig = array(
            'orientation' => 'portrait',
            'filename' => 'Invoice_' . $id
            );

        $this->set('invoice', $this->Invoice->read(null, $id));
    }
}

STEP 4: View

I've created a pdf folder in my view folder and created view.ctp in app/View/Invoices/pdf/view.ctp.


STEP 5: Layout

I've created a pdf folder in my layout folder and created app/View/Layouts/pdf/default.ctp


That's about it. In my view I couldn't make a PDF file from an URL. Although I have to mention I'm new in OOP and CakePHP so I would be very grateful if you could show me how to have this done. I'm sure it will help others because there many newbies like me who want to do this but because it's all meant for advanced programmers we cannot figure out how to put the pieces together.

Thank you very much for your understanding and your help!

[THIS POST IS MODIFIED EACH TIME THERE IS A NEW ANSWER WHICH IMPROVES IT]


回答1:


You need to add RequestHandler component, and browse to localhost/invoices/view/1.pdf

Looks like i had forgotten to mention to add RequestHandler component in the readme.

Also for WkHtmlToPdf you need to tell it where it can find it, and since you are on windows the default location surely wont work for you. You can set the location with Configure::write('CakePdf.binary', 'C:\Program Files\wkhtmltopdf\wkhtmltopdf.exe') after having installed it on windows




回答2:


You are missing this code in app/config/routers.php

Router::parseExtensions(); Router::setExtensions(array('json', 'xml', 'rss', 'pdf'));

details available on: http://www.dereuromark.de/2014/04/08/generating-pdfs-with-cakephp/



来源:https://stackoverflow.com/questions/14751428/cakephp-2-implement-cakepdf-plugin-by-ceeram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!