NURUL FERDOUS

html2pcl and html2ps wrapper class

Posted on: December 17, 2008

This wrapper class is a gift for my blog readers. I think the class is self descriptive. This is why I ain’t write how to use it bla bla bla…You can make pcl and ps files from html with this class. I have submitted this class at phpclasses too. You may have a look by clicking HERE. It runs both on Windows and *Nix envioronment.

html2pcl class starts here:

<?php
/* Description of wrapper class Html2pcl
* This class is used to produce .PCL and .PS file from HTML file
* @Author      : Nurul Ferdous
* Version      : 1.0.0
* Date         : 16 Dec 2008
* License      : Freeware
* Dependancy-1: html2ps package, you may get it from here: http://user.it.uu.se/~jan/html2ps.html
* you may install it from terminal by typing "sudo apt-get install html2ps" withpcl quote in kubuntu (in my case)
* html2ps uses perl
* Dependancy-2: Ghostscript, you may get it from here: http://pages.cs.wisc.edu/~ghost/
* Download it and run "make" command from terminal. you have to download and run exe installer if you use Windows
* The ps_output directory is used to store .ps files which are used to generate smaller sized pcl file
* keep in your mind that you should have write permission in "pcl_output" directory
*/

class Html2pcl {

    public $device = 'laserjet'; //alternatively you may use 'epson' etc as printers
    public $paperSize = 'a4'; //alternative you may use 'letter' etc as papersize

    function __construct(){
        //checking whether the directory named "out" already exists or not
        if(!file_exists("pcl_output")){
            mkdir("./pcl_output", 0755);
        }

        //checking whether the directory named "ps_output" already exists or not
        if(!file_exists("ps_output")){
            mkdir("./ps_output", 0755);
        }
    }

    function __destruct(){
        //Clearing file status cache
        clearstatcache();
    }

    function isWinServer()
    {
        return !(strpos(strtoupper($_SERVER['SERVER_SOFTWARE']), 'WIN32') === false);
    }

    //please don't use $htmlFileName without "http://" rather use like this "http://www.google.com"
    //please use $pclFileName without the extension like "google"
    function makePCL($htmlFileName, $pclFileName){
        $file = './pcl_output/'.$pclFileName.'.pcl';
        if($this-&gt;isWinServer()){
            //checking whether the file name given as parameter is already exists or not
            if(!file_exists($file)){
                exec("perl html2ps $htmlFileName &gt; ./ps_output/".$pclFileName.".ps");
                exec("gs -sDEVICE={$this-&gt;device} -sPAPERSIZE={$this-&gt;paperSize} -sOutputFile=./pcl_output/$pclFileName.pcl -dNOPAUSE -q ./ps_output/$pclFileName.ps -c quit");
            }
        }
        else{
            if(!file_exists($file)){
                exec("html2ps $htmlFileName &gt; ./ps_output/".$pclFileName.".ps");
                exec("gs -sDEVICE={$this-&gt;device} -sPAPERSIZE={$this-&gt;paperSize} -sOutputFile=./pcl_output/$pclFileName.pcl -dNOPAUSE -q ./ps_output/$pclFileName.ps -c quit");
            }
        }
    }
}
?>

Example usage starts here:

<?php
/*
* Usage of html2pcl class
*/
require_once('class.html2pcl.php');
$obj = new Html2pcl;
//please don't use $htmlFileName without "http://www." rather use like this "http://www.google.com"
//please use $pclFileName without the extension like "google"
$obj-&gt;makePCL("http://www.google.com", "google");
?>
About these ads
Tags: ,

7 Responses to "html2pcl and html2ps wrapper class"

Again rocking!

Great Job ferdous bhai. Keep it up.

fine , congrats man

8DuNjT Thanks for good post

eDAHrk Thanks for good post

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

I am Nurul Ferdous a Zend Certified Engineer (ZCE), serving at Bangladesh Internet Press Limited (BIPL) as programmer. I am a FOSS advocate and love RnD.

Categories

Me @ LinkedIn

View Nurul Ferdous's profile on LinkedIn

My Photos @ Flickr

00bcd191b03b72a98ce7f120f066f81afa3ebdd7ecec46a6095d55500c10fb8e

MOTEEL HOME PAGE







More Photos

Blog Stats

  • 38,505 [ Vistors ]

My Twitter Updates

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: