Sunday, January 08, 2017

Steps to install wkhtmltopdf in Centos 7 Redhat

Steps to install wkhtmltopdf in Centos 7 Redhat


As you may know, wkhtmltopdf is a tool that is used to create a pdf on the fly from the Linux/Unix shell from any html document
Step 1 is to create a html document
Step 2 is to call wkhtmltopdf with the html document as input and pdf as output

But, before that, we need to install wkhtmltopdf in the Linux server machine.

Install wkhtmltopdf in Centos 7

Before we install wkhtmltopdf, we need to install the dependencies or support packages, which are required for wkhtmltopdf to work as expected.

So, we execute the following steps.

i. sudo yum install -y xorg-x11-fonts-75dpi
ii. sudo yum install -y xorg-x11-fonts-Type1

Let us get the rpm package from the link gna.org using the link below.
wget http://download.gna.org/wkhtmltopdf/0.12/0.12.2.1/wkhtmltox-0.12.2.1_linux-centos7-amd64.rpm

sudo rpm -Uvh wkhtmltox-0.12.2.1_linux-centos7-amd64.rpm

error: Failed dependencies:
        libXext is needed by wkhtmltox-1:0.12.2.1-1.x86_64
        libXrender is needed by wkhtmltox-1:0.12.2.1-1.x86_64

As you can see, there are two libraries that are missing. Let us install them too.


Install dependencies libXext libXrender for wkhtmltopdf


iii. sudo yum install libXext libXrender

Loading mirror speeds from cached hostfile
 * base: mirror.web-ster.com
 * epel: mirrors.cat.pdx.edu
 * extras: mirrors.xmission.com
 * updates: centos.eecs.wsu.edu
Resolving Dependencies
--> Running transaction check
---> Package libXext.x86_64 0:1.3.3-3.el7 will be installed
---> Package libXrender.x86_64 0:0.9.8-2.1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved



Installing:
 libXext                                  x86_64                               1.3.3-3.el7                                    base                                39 k
 libXrender                               x86_64                               0.9.8-2.1.el7                                  base                                25 k



Install  2 Packages

Total download size: 64 k
Installed size: 127 k
Is this ok [y/d/N]: y
Downloading packages:
(1/2): libXrender-0.9.8-2.1.el7.x86_64.rpm                                                                                                      |  25 kB  00:00:00
(2/2): libXext-1.3.3-3.el7.x86_64.rpm                                                                                                           |  39 kB  00:00:00

Finally, let us try installing the rpm package again.

iv. sudo rpm -Uvh wkhtmltox-0.12.2.1_linux-centos7-amd64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:wkhtmltox-1:0.12.2.1-1           ################################# [100%]

We have successfully set up and installed wkhtmltopdf on our machine.

Now, let us test if the module is working. We will try to create a pdf document  out of Google's first page.

wkhtmltopdf https://www.google.com google.pdf

Wednesday, January 04, 2017

Call to undefined function mcrypt_encrypt() in ... on line ...

Call to undefined function mcrypt_encrypt() in ... on line ...


When porting from my local server to the live server for testing, the php page was not loading and returned an Error 500 or Internal Server Error.

Analyse Apache error logs


As a first step, we tried to analyse the apache error logs to shortlist the reasons. The following line seemed to hold the key to the problem.

PHP Fatal error:  Call to undefined function mcrypt_encrypt() in /path/to/myAppFolder/myFile.php on line 13

Identify the problem


Missing php-mcrypt module


The issue seems to be related to a missing module in the Centos server. The php program was using the php-mcrypt module and it was not yet installed on the server yet.

Install php-mcrypt and restart Apache


So, we installed the php-mcrypt module using the below command

sudo yum install php-mcrypt

As a final step, we restarted the Apache Web Server

sudo systemctl restart httpd

Calling the php program now does not generate error now. Problem of Call to undefined function mcrypt_encrypt()  SOLVED.

Tuesday, January 03, 2017

Ajax call not working as expected in Firefox


Ajax call not working as expected in Firefox

Okay, I had this peculiar problem of my web form updating the relevant tables when being called from Google Chrome and not doing so, when the call was made through Firefox.

The form went into sleep and did not respond after the Submit button was clicked. It looked as if the Ajax call was in indefinite loop in Mozilla Firefox.

As we went digging for reasons that this was happening, we realised that Firefox is strict about how data has to be sent to it.

Google Chrome is someone who tries to make sense out of the data and so, we got away with Chrome.

So, we understand that what we need to return from a php, which is being called through an ajax request, is content preceded by a header, which indicates the type of content that follows.

It could be any of these types as defined below.

Header Content-type: Common examples:
application/json
application/x-www-form-urlencoded
application/pdf
multipart/form-data
text/html
text/plain
image/png
image/jpeg

image/gif

So, at the end of your php code, you will  have something like what you are seeing below. In the first example, the returned value is a json content, while the second example returns a plain text message in response to the Ajax request.

Php program returning JSON in response to an Ajax call, Example 1:


    header('Content-type: application/json');
    echo json_encode($response_array);
?>


Php program returning text value to Ajax call, Example 2:


    header('Content-type: text/plain');
    echo 'success';
?>