CakePHP 2:由Ceeram实现CakePDF插件 [英] CakePHP 2 : Implement CakePDF plugin by Ceeram

查看:189
本文介绍了CakePHP 2:由Ceeram实现CakePDF插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现由Ceeram设计的CakePDF插件。



我使用CakePHP 2和我在本地使用wamp在windows vista上工作。我遵循了readme文件中的所有内容,但是我在某个时候遇到了困难。



我首先想要做的是使用WkHtmlToPdf引擎将HTML链接转换为PDF 。我看到许多人遇到问题,使其工作,所以我将详细一路通过以下不同的步骤。






< h2>第1步:Ceeram的CakePdf插件

我在 https://github.com/ceeram/CakePdf
我将包含的文件夹解压到app / Plugin / CakePdf






步骤2:Bootstrap



我添加了以下行 - app / Config / bootstrap.php:

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

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






步骤3:控制器



我创建了我的控制器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));
}
}






第4步:查看



我在我的视图文件夹中创建了一个pdf文件夹,并在app / View / Invoices / pdf / view.ctp中创建了view.ctp。 p>




第5步:布局



我创建了一个pdf文件夹在我的布局文件夹和创建的应用程序/ View / Layouts / pdf / default.ctp






在我看来,我不能从一个URL的PDF文件。
虽然我不得不提到我是新的在OOP和CakePHP,所以我将非常感谢,如果你能告诉我如何做这个。我相信它会帮助别人,因为有很多新手像我想要这样做,但因为它是所有意味着高级程序员,我们不能弄清楚如何把这些组合在一起。



非常感谢您的理解和帮助!



[此次修改是每次都是一个新的答案,改进它]

解决方案

您需要添加RequestHandler组件,并浏览到localhost / invoices / view / 1.pdf



看起来我忘了在readme中添加RequestHandler组件。



同样对于WkHtmlToPdf,你需要告诉它在哪里可以找到它,你在Windows上的默认位置肯定不会为你工作。在Windows上安装它后,您可以使用Configure :: write('CakePdf.binary','C:\Program Files \wkhtmltopdf\wkhtmltopdf.exe')设置位置


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]

解决方案

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

这篇关于CakePHP 2:由Ceeram实现CakePDF插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆