Rodney
Rodney

Reputation: 250

Using ZendPdf in Zend Framework 2

I'm trying to wrap my head around zend framework 2 and it's module system. I have the the skeleton application in the Getting Started guide working. I now want to use ZendPdf to load in an existing pdf file and work with it. I've done the following so far:

When ever I call the action in the controller I get the following error

Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (ZendPdf) could not be initialized

Obviously, from the error, I need to add zendpdf to the module_config.php so the ModuleManager can pick it up. This is were I get lost. I don't know how to configure zendpdf in the config file. This is what I have so far:

module.config in the Album Module

return array(
'controllers' => array(
    'invokables' => array(
        'Album\Controller\Album' => 'Album\Controller\AlbumController',
    ),
),
// Routes for 
'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'album' => __DIR__ . '/../view',
    ),
),
'zendpdf' => array() /* what to do here */
);

This is the controller action in my AlbumController

public function viewPdfAction()
{
  $pdf = ZendPdf::load('/tmp/pdf_report.pdf');
}

This is the application.config

<?php
   return array(
     'modules' => array(
       'Application',
       'Album',
       'AlbumRest',
       'ZendPdf'
     ),
     'module_listener_options' => array(
        'config_glob_paths'    => array(
        'config/autoload/{,*.}{global,local}.php',
     ),
      'module_paths' => array(
          './module',
          './vendor',
     ),
  ),
);

I've been reading the docs but am just not getting it. I need a push in the right direction.

Upvotes: 2

Views: 4354

Answers (2)

Rodney
Rodney

Reputation: 250

Using clues from Andy this is what I found that worked.

Install ZendPdf via composer. Using composer will make ZF2 automatically auto-the library. below is the composer.json file that I used

{
    "repositories": [
      {
        "type": "composer",
        "url": "http://packages.zendframework.com/"
      }
    ],
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
        "framework",
        "zf2"
    ],
    "homepage": "http://framework.zend.com/",
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.*",
        "zendframework/zendpdf": "*"
    }
}

In your controller import the ZendPdf namespace

use ZendPdf\PdfDocument;

Then somewhere in a Controller action create a new pdf object and work with it

public funciton someAction()
{
     $pdf = PdfDocument::load('path/to/pdf');
}

Upvotes: 4

ba0708
ba0708

Reputation: 10599

I assume that what you are using is this? In that case, it is not a module, but a library, which would explain why you are not able to load it as a module. If you installed it with Composer, then you should be able to find it within your vendor directory, and it should be autoloaded. You should then be able to use it like you use all of the classes within Zend Framework by using the appropriate namespace. I have not used this myself, but I think this is what you did wrong.

Upvotes: 1

Related Questions