Cakephp 2.6.1, PHPExcel "Unable to load PDF Rendering library" - cakephp

On the same project i'm using phpExcel to generate excel files just fine.
PHPExcel files location:
C:\wamp\www\circulo\app\Vendor\PHPExcel\ (this folder contains PHPExcel.php along with PHPExcel files folder)
Also on the same project, i'm using dompdf alone to generate pdf file just fine (not via PHPExcel). I've just liked a lot how PHPExcel allows excel file construction so i'd like to create pdfs via PHPExcel as well.
dompdf files location:
C:\wamp\www\circulo\app\Vendor\dompdf\
The path seems correct via debugger => C:\wamp\www\circulo\app\Vendor\dompdf\dompdf.php
This is my code to get a pdf file:
I get error:
[PHPExcel_Writer_Exception] Unable to load PDF Rendering library
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
require_once APP . DS . 'Vendor' . DS . 'PHPExcel' . DS . 'PHPExcel.php';
$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererLibrary = 'dompdf.php';
$rendererLibraryPath = APP . 'Vendor' . DS . 'dompdf' . DS .$rendererLibrary;
Debugger::log($rendererLibraryPath);
if (!PHPExcel_Settings::setPdfRenderer( $rendererName, $rendererLibraryPath )) { die( 'Please set the $rendererName and $rendererLibraryPath values' . PHP_EOL . ' as appropriate for your directory structure' . $rendererLibraryPath ); }
$objPHPExcel = new PHPExcel;
$objWriter = new PHPExcel_Writer_PDF($objPHPExcel);
//$objWriter->save("emptyPdfJustYet.pdf");
Tried their demo 21pdf.php to same results.
Can you help? Thanks a lot!!

When I print the error to log, I realize it is trying to load the dompdf_config.inc.php from an invalid location. See below.
C:\wamp\www\myapp\app\Vendor\dompdf\dompdf.php/dompdf_config.inc.php\n
Rather than altering the class files of PHPExcel, it's wise to change the configuration in your view. Ignoring the $rendererLibrary completely in the $rendererLibraryPath fixed my issue. I think the PHPExcel know how to pick dompdf.php file. Try below code let us know if it doesn't work.
And you are providing wrong render constant too, changed PDF_RENDERER_MPDF to PDF_RENDERER_DOMPDF.
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'dompdf.php';
$rendererLibraryPath = APP . 'Vendor' . DS . 'dompdf';
And for create writer object, I tried
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');

Related

How to import vendor files in CakePHP 3x

I'm working with CakePHP 3(beta 2) version recently launched. I need to integrate Facebook Login using PHP SDKs and I'm not clear with importing vendor files in this version.
In CakePHP 2x, I had used
App::import('Vendor', 'Facebook', array('file' => 'Facebook' . DS . 'src'. DS. 'facebook.php'));
So I need to reproduce the same in CakePHP 3x(I'm not using composer).
Any reference for this?
Well you will have to load it yourself if composer is not an option. You can always use the very basic require method and create a new instance of the vendor class yourself. Reference: http://book.cakephp.org/3.0/en/core-libraries/app.html#loading-vendor-files
Use:
//The following line should do the same like App::import() in the older version of cakePHP
require_once(ROOT . 'vendor' . DS . 'Facebook' . DS . 'src' . DS . 'facebook.php');
$facebookApi = new facebook();
In cakephp3 , to add a new vendor library you can follow below steps :
Place library folder under your_project/vendor/
include library file using require_once(ROOT . DS . 'vendor' . DS . "my_library_folder" . DS . "my_library_base_class.php")
,this includes the library code file in our code.
Include class name in top of Controller like :
namespace App\Controller;
use MyLibraryBaseClass;
,
this imports the library code file in our namespace to be used.
create object of loaded class as
$my_obj= new MyLibraryBaseClass();
the answer provided by Ayman B. does not look like doing the job as expected in the question after i tried it myself , for the following reasons :
the vendor folder in cakephp3 is not located in src folder under APP namespace , it is moved to the ROOT folder , by doing you will not be able to load your Facebook class as expected , try it yourself and you will see the result ...
By loading a vendor file this is does not automatically load the class name itself , if your vendor lib does not follows the follwing rule as PSR-0 rule :
\VENDOR\PACKAGE\TEST.CLASS.PHP and inside the test.class.php there is not a class definition that must be called or imported in your script with a defined namespace keyword in the begining of this script as follows : namespace
then the code above will not work
To correct the answer you have to do some several steps as follows :
1 - Define in bootstrap.php a new cakephp constant like so :
define('VENDOR',ROOT . DS . 'vendor' .DS); as VENDOR constante is removed in cakephp 3.x you can define it yourself
2 - After that , you have to specify the vendor name , the package name and the class name in a vendor constante like :
define('_',; and then you can do $facebookApi = new \\();
this is will work for you as expected in the question
If you have issues try to get back to me , i will show you an example of use as described here ...
I also had the same problem with CakePHP 3.0.
Do the installation as instructed using Composer.
Then You have to properly load the plugin in your controller with the use statement. Like this:
use Ghunti\HighchartsPHP\Highchart;
If you're using the plugin in most of the pages, instead of loading in each Controller, you can add the same line in your bootstrap.php file, right below the other use statements.
That will solve the problem of using the plugin.
As of CakePhp 3.x, the recommended code standard is to use require_once without the brackets"()".
require_once(ROOT.'Folder'.DIRECTORY_SEPARATOR.'requiredfile.ph');
becomes
require_once ROOT.'Folder'.DIRECTORY_SEPARATOR.'requiredfile.ph';
https://book.cakephp.org/3.0/en/contributing/cakephp-coding-conventions.html
Hope that helps someone in the future.

require command issue in CakePHP

I have a library in my project.When I want to use this with this code:
require('../Plugin/Utils/DateTimeUtil.php');
it says no such file exists. my cakephp 1s 2.3 what should I do?
The Routing in cakephp is different from pure php.I had something like this.At first you should find the path Plugin folder with this code
$pluginPath = App::path('Plugin');
Then It returns an array which contains the plugin folder's path in 0 index.So you should the returned value like blow:
require($pluginPath[0] . 'Utils' . DS . 'DateTimeUtil.php');
You can use slash instead of DS. DS is DIRECTORY_SEPRATOR.

$Email->attachments() does not attach file

We are using Cake's E-Mail class to send emails with attachments. It works fine for all cases except one and we are not able to figure out where the problem is.
Process:
A pdf-File is created & written to the file-system (file is written correctly and exists)
when the Email is sent the attachment is 0bytes in size (whereas the file to attach is created correctly in the file-system)
Working code:
// Write invoice as file
$CakePdf->write(APP . 'tmp' . DS . 'invoices' . DS . $invoiceNo . '.pdf');
[...]
// Send invoice to customer
$Email = new CakeEmail('invoice');
$Email->attachments(APP . 'tmp' . DS . 'invoices' . DS . $invoiceNo . '.pdf');
$Email->to($this->Invoice->Customer->getEmailAdress($customerId));
$Email->viewVars(array('invoice_no' => $invoiceNo));
$Email->send();
Not working code (attachment is zero bytes in size):
$CakePdf->write(APP . 'tmp'. DS .'certificates' . DS . $certLoginId . $certCourseId . '.pdf');
[...]
// Send certificate to customer
$Email = new CakeEmail('certificate');
$Email->attachments(APP . 'tmp'. DS .'certificates' . DS . $certLoginId . $certCourseId . '.pdf');
$Email->to($emailOfUser);
$Email->viewVars(array('courseName' => $certCourseName, 'probandName' => $probandName));
$Email->send();
Edit - there is no typo it is all correctly set. The problem seems to be, that the generation of a PDF by tcpdf runs asycnronously in the background. So when Cake tries to attach the file it is not written to the file system completly. So it cannot be attached.
If tried to let the script sleep for a while with no success:
echo '<br>';
echo $path_to_certificate;
echo '<br>';
echo filesize($path_to_certificate);
sleep(10);
echo '<br>';
echo $path_to_certificate;
echo '<br>';
echo filesize($path_to_certificate);
echo '<br>';
sleep(10);
echo $path_to_certificate;
echo '<br>';
echo filesize($path_to_certificate);
Outputs:
C:\xampp\htdocs\www\eflux_frontend\app\tmp\certificates\13750.pdf
0
C:\xampp\htdocs\www\eflux_frontend\app\tmp\certificates\13750.pdf
0
C:\xampp\htdocs\www\eflux_frontend\app\tmp\certificates\13750.pdf
0
Whereas the file is generated in the meantime, because I can see & access the file in the filesystem. It isn't a locking Problem because the other code works in a different place, but the file generated is smaller so it does not take up so much time to process.
How can I now ensure that the generation process is complete?
It seems that we are not able to ensure, that the PDF is properly created before attaching it to an email (maybe someon can give me a hand here).
Due to the fact, that the created PDF is written to a database an ugly workaround is possible: After the PDF is written to database, we can take it out of the database, write a file using the CakeFileHandler and attach this to the email which works for me:
// Workaround
$this->Certificate->recursive = -1;
$data = $this->Certificate->findById($cert_id);
$pdf = base64_decode($data['Certificate']['certificate_pdf']);
$path_to_certificate = APP . 'tmp'. DS .'certificates' . DS . $certLoginId . $certCourseId . '.pdf';
$certificate_file = new File($path_to_certificate);
$certificate_file->write($pdf);
[do mail stuff]
$certificate_file->delete();
$certificate_file->close();

CakePHP copy file to new directory

The cakephp documentation is a bit poor according the File api and I would like to use it to copy one file to another directory and if the directory does not exist to create it.
Thanks!
You can do something like this:
$file = new File('your_file.ext');
if ($file->exists()) {
$dir = new Folder('folder_inside_webroot', true);
$file->copy($dir->path . DS . $file->name);
}
API about these classes:
Folder;
File;
I hope it helps.

CakePHP Media Plugin - Upload Folder

I am using the Media Plugin (http://www.ohloh.net/p/cakephp-media).
I wanna define custom folder for all the uplaods. i am a little confused where it has to be done.
This is the folder structure i want to achieve
webroot/media/image/original (for the original file storage)
webroot/media/image/large (for the large image filter)
webroot/media/image/medium (for the medium image filter)
webroot/media/image/small (for the small image filter)
also i want to use a random name that i want to generate using the following sript.
//UUID generator
function _imgName() {
return time() . substr(md5(microtime()), 0, 12);
}
Firstly, having used this plugin, I would recommend considering the default directory structure to keep your app simpler. You don't want to edit the plugin directly as it will make upgrading more painful...
# /app/webroot/media/transfer/img/slug.ext (for the original file storage)
# /app/webroot/media/filter/l/img/slug.ext (for the large image filter)
# /app/webroot/media/filter/m/img/slug.ext (for the medium image filter)
# /app/webroot/media/filter/s/img/slug.ext (for the small image filter)
However, the Media plugin configuration file is located at /app/plugins/media/config/core.php and contains some constants that you can override application-wide by defining them in /app/config/bootstrap.php first. To achieve a format somewhat similar to what you want, you could define the following variables:
define('MEDIA_TRANSFER', WWW_ROOT . 'media' . DS . 'original' . DS);
define('MEDIA_FILTER', WWW_ROOT . 'media' . DS);
define('MEDIA_TRANSFER_URL', 'media/original/');
define('MEDIA_FILTER_URL', 'media/');
# /app/webroot/media/original/img/slug.ext (for the original file storage)
# /app/webroot/media/l/img/slug.ext (for the large image filter)
# /app/webroot/media/m/img/slug.ext (for the medium image filter)
# /app/webroot/media/s/img/slug.ext (for the small image filter)
(Note: you can also set the above paths on a per-model basis by passing the correct configuration options when adding the behavior to your models.)
You can also redefine the names of the image filters being used to get a step closer to your goal. You need to do this in /app/config/bootstrap.php again, but after you have loaded the Media plugin configuration:
require APP . 'plugins' . DS . 'media' . DS . 'config' . DS . 'core.php';
Configure::write('Media.filter.image', array(
'small' => array('convert' => 'image/png', 'zoomCrop' => array(100, 100)),
'medium' => array('convert' => 'image/png', 'fitCrop' => array(300, 300)),
'large' => array('convert' => 'image/png', 'fit' => array(600, 440)),
));
# /app/webroot/media/original/img/slug.ext (for the original file storage)
# /app/webroot/media/large/img/slug.ext (for the large image filter)
# /app/webroot/media/medium/img/slug.ext (for the medium image filter)
# /app/webroot/media/small/img/slug.ext (for the small image filter)
If you read the documentation for the Media.TransferBehavior::transferTo() method, you will see you can customize the latter part of the path (ie. img/slug.ext) by reimplementing this method in your model (eg. MyModel::transferTo()). Something like this would get you even closer:
class MyModel extends AppModel {
public function transferTo($via, $from) {
extract($from);
$mime = Mime_Type::guessName($mimeType ? $mimeType : $file);
$name = $this->_imgName();
$path = $mime . DS . $name
$path .= !empty($extension) ? '.' . strtolower($extension) : null;
return $path;
}
}
# /app/webroot/media/original/image/129916996632c787226a0b.ext (for the original file storage)
# /app/webroot/media/large/image/129916998392a3570a1828.ext (for the large image filter)
# /app/webroot/media/medium/image/12991699891c7625bebedb.ext (for the medium image filter)
# /app/webroot/media/small/image/12991699938ab22d80cfc6.ext (for the small image filter)
While not exactly what you were looking for (/large/image/ vs /image/large/), this is about as far as you can go without reimplementing larger portions of the plugin. This is because the path is treated as two parts (eg. /media/large/ and image/129916998392a3570a1828.ext) that get appended together later. You can see the append operation happening in the Media.TransferBehavior::_prepare() method and in the Media.GeneratorBehavior::make() method. You would need to either extend the plugin and duplicate those methods (with alterations) in your application code, or hack those two lines directly to get the desired output!

Resources