简体   繁体   中英

How to use ZendPdf\Pdf in Zend Framework 2

I am trying to use ZendPdf in Zend Framework. I am building upon the skeleton application. I have used composer.phar to get the required packages for ZendPdf and all the files are now under a directory called zendpdf in the same directory as the directory zendframwork in vendor so I assume that the package update worked.

Here is my IndexController.php

<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Json\Json;

class IndexController extends AbstractActionController
{
        public function indexAction()
        { 


                    $fileName = 'foo.pdf';
                    $pdf = \ZendPdf\Pdf::load($fileName);   


                    // Some JSON stuff etc
                    // …
        }   
}

I get the following error:

Fatal error: Class 'ZendPdf\Pdf' not found in /mnt/host-home/websites/mywebsite.com/module/Application/src/Application/Controller/IndexController.php on line 16

I am still trying to get my head around namespaces etc. Any help is appreciated.

EDIT:

I know that the PDF functionality is not included with ZF2, which is why I have used composer.phar to get it. What I would really like to know is how to I instantiate the ZendPDF class?

There is no class Pdf in ZendPdf , I think you probably meant PdfDocument :

\ZendPdf\PdfDocument::load($fileName);

Since you said you're new to namespaces, I'd recommend using the use command to help make your code a bit more readable. If you were to add:

use ZendPdf\PdfDocument;

to the top of your class, PHP then knows that PdfDocument refers to \\ZendPdf\\PdfDocument , so you can then just do:

PdfDocument::load($fileName);

in your controller action.

Came here with the same error; I'd solved this way:

A. If downloaded from the composer, go to step C. For manual downloads, create a "zendpdf" folder on vendor\\zendframework:

example: C:\\mywww\\zf2\\vendor\\zendframework\\zendpdf

B. Copy the masterzip contents (Library folder, test folder, composer.json, etc) on the new "zendpdf" folder.

C. Edit the StandardAutoloader.php from:

C:\\mywww\\zf2\\vendor\\zendframework\\zendframework\\library\\Zend\\Loader\\StandardAutoloader.php

    .
    .
    .
    foreach ($options as $type => $pairs) {
switch ($type) {                
    case self::AUTOREGISTER_ZF:                    
        if ($pairs) {
            $this->registerNamespace('Zend', dirname(__DIR__));
            $this->registerNamespace('ZendXml', dirname(dirname((__DIR__))) . '/ZendXml');
            $this->registerNamespace('ZendPdf', dirname(dirname((__DIR__))) . '/ZendPdf');  //add this line.                      
        }
        break;
    .
    .
    .

That's all; now we can use the Pdf functions:

    use ZendPdf\PdfDocument; //dont' forget to add this at start   ;)
    $pdf1 = new PdfDocument(); // Create a new PDF document
    //$fileName = 'test.pdf';
    //$pdf2 = Zend_Pdf::load($fileName);// Load a PDF document from a file
    //$pdf3 = Zend_Pdf::parse($pdfString);// Load a PDF document from a string   

The problem is that you are using ZendPdf\\pdf::load(). This has been modified in zf2 v>2.0 to be ZendPdf\\PdfDocument::load(). Hope this solves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM