How To Generate Pdfs Or Images Using Phantomjs Inwards Codeigniter?

PhantomJS is a headless WebKit alongside JavaScript API. It has fast together with native back upwards for diverse spider web standards: DOM handling, CSS selector, JSON, Canvas, together with SVG." Let's run into what PhantomJS tin create to fulfil my chore requirements:

It renders spider web pages the same equally the browsers which uses Webkit, such equally Google Chrome or Apple Safari.
It supports SVG.
It has JavaScript API which lets me alter the spider web page equally needed.
It has a cookie API which solves the user authentication issue.
It tin output the rendering page inwards a few formats which includes PDF.

You tin easily uncovering 1 helper file called "PhantomJS Wrapper Helper For CodeIgniter" inwards the meshing which almost solves problem. Read together with download it from here.

I spent almost two days to brand this work. I followed all the steps together with I was able to run it successfully via command work but non via PHP script. When I tried to generate pdfs via PHP, it didn't worked at all. I searched a lot inwards Google together with flora many suggested suggestions but none of it worked.

I together with hence tried inwards the alive server, it piece of work conduct away. That was really strange. :)

I knew that work was virtually permission issues.

In my localhost environment, I added sudo -u anup inwards exec() role together with it worked. :)

Don't forget to usage escapeshellcmd () role equally well, other wise it won't piece of work if yous convey enquiry strings inwards your URL.
 $url = escapeshellcmd($url); /*escapeshellcmd() escapes whatever characters inwards a string that mightiness hold out used to play tricks a crunch command into executing arbitrary commands. This role should hold out used to brand certain that whatever information coming from user input is escaped earlier this information is passed to the exec() or system() functions, or to the backtick operator. */ 

Replace the master copy helper file alongside next helper file. You tin uncovering helper file nether "application/helpers/phantomjs_helper.php"
<?php if ( ! defined('BASEPATH')) exit('No conduct script access allowed');  if ( ! function_exists('rasterize_wrapper')) {  role rasterize_wrapper($url='', $output=''){    if($url=='' || $output=='')      {        show_error('URL or Output file advert non defined correctly');         log_message('error','rasterize_wrapper: non initialized');        exit;      }          $url = escapeshellcmd($url);  // ADDED BY ME    //using exec here, hence hold out resposible together with brand certain yous validate both the $output together with $url to a higher house inwards the to a higher house role itself. I convey non  //Assuming that phantomjs's path has been included inwards yous $PATH var     exec('sudo -u anup phantomjs '.realpath('assets/js').'/rasterize.js '.$url.' '.realpath('assets/pdfs').$output,$output_status, $return_status); // SUDO -U USERNAME ADDED BY ME   if($return_status=='0'){ homecoming true;}  homecoming false; //something obiviously went incorrect together with yous tin cheque the $output together with $return_status inwards your implementations to homecoming to a greater extent than to controller.  }  }  

Remember this helper file is to piece of work unopen to localhost only. For alive server, usage the exec () without whatever sudo
  exec('phantomjs '.realpath('assets/js').'/rasterize.js '.$url.' '.realpath('assets/pdfs').$output,$output_status, $return_status); 

Don't forget to double cheque the files location such equally rasterize.js, pdfs folder etc.

Tutorial to add together PATH inwards final tin hold out flora @http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/

Source: Generating PDFs together with Thumbnails of Views.
http://php.net/manual/en/function.escapeshellcmd.php
Sumber http://developer-paradize.blogspot.com

Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?