Forum How do I...?

Princexml and CodeIgniter - How to add the library?

wal
Hi guys,

I am really new to this tool, and new to PHP codeing.
I want to add this tool to my CodeIgniter.
Currently running on WAMP locally, and on production the server is an Ubuntu Server.

- I'm not sure if I should install the package since I'm going to run this from my code.
Should I install the .exe file? and later on my server the Ubuntu package for this to work?
How does this work in general?

This is what I did so far:
- I downloaded the PHP zip file from here: http://www.princexml.com/download/wrappers/ and added it to my "libraries" folder in the codeigniter directory.
from what I know, I just have to include/call the library and use it's functions regularly.
https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

mikeday
Yes, you need to install the Prince package on whatever server you wish to run it on.

The PHP wrapper takes the executable path when you initialise it.
wal
That's what I did and don't really know how to continue from here, can you please review my post?
http://stackoverflow.com/questions/35726628/how-to-add-prince-library-to-codeigniter?noredirect=1#comment59128239_35726628
mikeday
$prince = new Prince('http://localhost/prince/prince.exe');

This should not be a URL, it should be a local file path to the location of prince.exe on the server that is running the PHP code.
wal
$prince = new Prince('C:\Program Files (x86)\Prince\engine\bin\prince.exe');

like this?

EDIT: I still get the same errors.


EDIT2:
I tried this too:
$prince = new Prince('file:///C:/Program%20Files%20(x86)/Prince/engine/bin/prince.exe');
mikeday
When you load the library, perhaps you can pass the argument to the constructor as the second argument? eg. like this:
$this->load->library('prince', 'C:\Program Files (x86)\Prince\engine\bin\prince.exe');

If it still fails, please check if the error message has changed.
wal
not working :(
mikeday
What is the error message? Is this the correct way to pass arguments to constructors with CodeIgniter?
wal
The same mentioned in the link I posted:

A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for Prince::__construct(), called in C:\wamp\www\tools\system\core\Loader.php on line 1247 and defined

Filename: libraries/prince.php

Line Number: 48

Backtrace:

File: C:\wamp\www\tools\application\libraries\prince.php Line: 48 Function: _error_handler

File: C:\wamp\www\tools\application\controllers\aso\Cli_kas.php Line: 304 Function: library

File: C:\wamp\www\tools\index.php Line: 292 Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: exePath

Filename: libraries/prince.php

Line Number: 50

Backtrace:

File: C:\wamp\www\tools\application\libraries\prince.php Line: 50 Function: _error_handler

File: C:\wamp\www\tools\application\controllers\aso\Cli_kas.php Line: 304 Function: library

File: C:\wamp\www\tools\index.php Line: 292 Function: require_once

A PHP Error was encountered

Severity: Warning

Message: proc_open(): CreateProcess failed, error code - 87

Filename: libraries/prince.php

Line Number: 796

Backtrace:

File: C:\wamp\www\tools\application\libraries\prince.php Line: 796 Function: proc_open

File: C:\wamp\www\tools\application\libraries\prince.php Line: 528 Function: convert_internal_file_to_passthru

File: C:\wamp\www\tools\application\controllers\aso\Cli_kas.php Line: 311 Function: convert_file_to_passthru

File: C:\wamp\www\tools\index.php Line: 292 Function: require_once

An uncaught Exception was encountered

Type: Exception

Message: Failed to execute "" --structured-log=buffered "http://localhost/temp/test.html" -o -

Filename: C:\wamp\www\tools\application\libraries\prince.php

Line Number: 814

Backtrace:

File: C:\wamp\www\tools\application\libraries\prince.php Line: 528 Function: convert_internal_file_to_passthru

File: C:\wamp\www\tools\application\controllers\aso\Cli_kas.php Line: 311 Function: convert_file_to_passthru

File: C:\wamp\www\tools\index.php Line: 292 Function: require_once





It doesn't identify the EXE file "Message: Undefined variable: exePath"
and I don't know how to call it locally using WAMP and CodeIgniter MVC Framework.
mikeday
Okay, these are the important parts:
Missing argument 1 for Prince::__construct()

Undefined variable: exePath

It should be possible to pass the exePath to the construct by putting it in a hash array like this:
$params = array('exePath' => 'C:\Program Files (x86)\Prince\engine\bin\prince.exe');

$this->load->library('prince', $params);
wal
Message: Array to string conversion
Filename: libraries/prince.php


Message: proc_open(): CreateProcess failed, error code - 2
Filename: libraries/prince.php


Message: Failed to execute "Array" --structured-log=buffered "C:\wamp\www\tools\files\banana\test.html" -o -
Filename: C:\wamp\www\tools\application\libraries\prince.php


mikeday
How are you creating the Prince object? It looks like it's converting the parameter array to a string instead of passing the exePath parameter to the constructor.
wal
Solved it!

It was just sending the variable as an array and not as a string - and passing variables to a construct using CI needs to be sent as an array (like you wrote in your previous post) and after reading this 'Passing parameters on class Initialization' in this link:
http://tutsnare.com/create-custom-library-in-codeigniter/

So I changed the __construct to in Prince.php to this:
    public function __construct($exePathArr)
    {
        $exePath = "banana"; 
//var_dump($exePath); just to check this is a string for sure! 
        $exePath = $exePathArr['exePath'];

        $this->exePath = $exePath;
        $this->styleSheets = '';
        $this->scripts = '';
        $this->fileAttachments = '';
        ....
        .....
        ........
        ..........



and used this in my main function:

public function banana(){

    // it should be a local path instead of URL
    // $exe_path = '/usr/bin/prince';
    $exePath = array('exePath' => '/usr/bin/prince');
    
    // you can add parameter for the constructor call
    // $this->load->library('prince', $exe_path);
    $this->load->library('prince', $exePath);
    
    // it also should be a local path where the folder should be writable by apache
    $xmlPath = '/var/www/html/tools/files/banana/test.html';
    $pdfPath = '/var/www/html/tools/files/banana/file.pdf';
    $msgs = array();

    $this->prince->convert_file_to_file($xmlPath, $pdfPath, $msgs);

    var_dump($msgs);
    
}



This worked both on WAMP and on Ubuntu Server :D !


Thanks a lot for your help @mikeday !
mikeday
Oh okay, so maybe we need to change our __construct method for convenience, I thought it was unpacking the arguments by itself; oops! :D