CSS based page numbering in PDF creation

I frequently use PDF engine for generating PDF of multiple web pages a kind of book. Though you can use engine specific switch to insert page number(s) but if you want beautiful CSS based numbering then below code can be used. Just paste the code in a .CSS file and link to your html pages.


/* Page number at the bottom of page */

@page {

@bottom-center {

content: "Page " counter(page) " of " counter(pages);

font-style: italic;

color: green

}

}

Solving the authentication problem while opening Office documents hosted on Apache in IE8/IE9 on Windows 7

We were facing a problem in IE 8/9 on Windows 7 while accessing  Office 2007/ Office 2010 documents hosted on apache/Cent OS 4.6. After some analysis I found the reason and finally ended in a fix. See below my findings and solution. Hope this helps:

The main issue is with the Microsoft’s way of implementing Webdav protocol for accessing web content through Microsoft Web Client. When we click on a Office document then web client  sends HTTP /1.1 OPTIONS Request header to server to check the WebDav communication (My server doesn’t have WebDav). In response Apache return 200 OK Response header to Web Client which results in prompting the authentication screen by Windows 7.  Well you have option in IE to pass the authentication login automatically but that would be security breach as you will be exposing your machine authentication to internet so I would not prefer that. Best way is to configure Apache to reject these request. This is how i have solved. These changes needs to be done in httpd.conf file in /etc/httpd/conf folder (Cent OS 4.6)

# One way to doing it – Deny access based on request method


RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^(OPTIONS|PROPFIND)$ [NC]
RewriteRule ^.*$ - [F,L]

# Another way to implementing – Deny acess based on user agent (Vista and Windows 7 used same user agent with different version so this Regx shall work for both

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^Microsoft-WebDAV-MiniRedir
RewriteRule ^.*$ - [F,L]

Explanation on Flags:

1. [F] flag causes the server to return a 403 Forbidden status code to the client.

2. Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn’t care whether letters appear as upper-case or lower-case in the matched URI.

3. The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl.

 Some References:

Microsoft knowledge article on authentication requests from office documents 

Apache mod_rewrite rule documentaion

fiddler tool for debugging HTTP requests

Web platform video for developers

http://www.youtube.com/watch?v=3i4dtgh3ym0

I liked the video.. for more visit you can visit https://sites.google.com/a/chromium.org/dev/developers/meet-the-web-platform-companion

http://www.youtube.com/watch?v=3i4dtgh3ym0

I liked the video.. guy is showing some of the cutting edge capabilities of the web platform for developers. For more visit you can visit this link

Enjoy!

 

List.js: Cross browser native JavaScript

List.js A 7 KB cross-browser native JavaScript that makes your plain HTML lists super flexible, searchable, sortable and filterable.

This library is still in beta but worth to check.

For more you can check the README of the project on Github.

Good luck.

Message: Invalid argument: IE 8 Issue

In my Perl based server application, I want to execute a script in a new pop up window. I was using a syntax like:


var my_window = window.open('open_window.pl, 'my window', 'width=800,height=600,resizable,scrollbars');

When I run the program in FF and Chrome it all works fine. I can open my pop-up window with above parameters and can see my desired result but the issue starts when I do the same test with IE 8. In spite of pop-up blocker off, the pop-up window was not launching, it was giving me a error message like:

Message: Invalid argument.
Line: 100 Char: 5
Code: 0

After debugging I found a solution which is some way very ridiculous. Issue was because of the reason that Microsoft does support the name property in window.open() with spaces [http://msdn.microsoft.com/en-us/library/ms536651%28v=vs.85%29.aspx] this means that if I make my window.open like [See GREEN string

var my_window = window.open('open_window.pl, <strong><span style="color:#00ff00;">'my_window', 'width=800,height=600,resizable,scrollbars');

then it should works.

After doing this change it works with me for all the browsers.

I don’t know why IE always through such type of errors,  even if you launch the in-built Developer tool from IE interface then also you will get the same error message  “Invalid arguments” no further clue/pointer to solve the issue. MS really has to improve the error handling messages to user.

So guys, be careful while developing pop-up window for all browsers specially for IE.

Thanks

Testing Javascript in Perl applications

There is a tool called Selenium which is used to test the Javascript component in Perl based applications. This is used to run tests directly in the browser. Actually this tool sits between Perl application, test script and browser. The test script sends the Selenium server commands to run inside the web browser. The web browser then executes those commands, and returns the results to Selenium server and then to test script. It requires JRE and a working web server to work with.

Jonathan Rockway in his book has explained and used this in Catalyst applications.

Working With AJAX and Perl with example

Hello,

While doing Ajax Perl application development, I came across an interesting article on the following link.

The article demonstrate with an example how Ajax can be used with Perl.  In the example given, there is table containing Student names and Marks. Every row has an Edit button, by which user can edit the information for that row. THE DB used is Microsoft Access to keep things simple. In order to run the below example code you will have to create a table by the name “Student” in MS Access”. It should have the following columns.

Column Name Data type Sl_No(Primary Key) Number Name Text Marks Number

Also you need to create a DSN by the name “mydsn” pointing to the Access DB.

The basic logic here used in the example is that we have two separate rows for View and Edit (for every Student). Initially we hide the Edit row (by using style=”display:none”) and display the View Row(by using style=”display:block”). When the user clicks on the “Edit” button, the View row becomes hidden and the Edit row is displayed. We have used JavaScript to toggle between the rows. Below is the code snippet used in files (AjaxExample.pl and student.js).

============================================================

AjaxExample.pl

============================================================

#!perl

use DBI qw(:sql_types);

use CGI;

use CGI qw/:standard/;

use CGI::Ajax;

my $cgi = new CGI();

#——————————————————————–

# Mapping the Perl subroutine to the triggering function

#——————————————————————–

my $ajax = new CGI::Ajax( ‘saveStudInfo_JScript’ => &saveStudInfo_PerlScript );

$cgi->header(-charset=>’US-ASCII’);

print $ajax->build_html($cgi,&generateHTML);

#——————————————————————–

# Subroutine which generates the HTML

#——————————————————————–

sub generateHTML {

# Hash which contains existing data

%Students = get_Student_Info();

$cnt = 1;

# Write the html code in the form of a string

$returnHTML .=  “n<HTML>”;

$returnHTML .=  “n<HEAD>”;

$returnHTML .=  “n<TITLE>Student Information</TITLE>”;

$returnHTML .=  “<SCRIPT language=”javascript” src=”/javascript/student.js” type=”text/javascript”></SCRIPT>”;

$returnHTML .=  “n</HEAD>”;

$returnHTML .=  “n<BODY bgColor=”#ffffff”>”;

$returnHTML .=  “<FORM  method=”POST” enctype=”multipart/form-data” name=”StudentForm”>”;

$returnHTML .= “n<BR>”;

$returnHTML .= “n<BR>”;

$returnHTML .= “n<TABLE cellspacing=”0″ cellpadding=”0″ align=”center”>”;

$returnHTML .= “n<TR><TD>”;

$returnHTML .= “n<TABLE width=”100%” align=”center” border=”1″ cellpadding=”3″ cellspacing=”1″ id= “StudentInfoTable”>”;

$returnHTML .= “n<TR>”;

$returnHTML .= “n<TD align=”center” >SL No</TD>”;

$returnHTML .= “n<TD align=”center”  nowrap>Name  </TD>”;

$returnHTML .= “n<TD align=”center” nowrap>Marks  </TD>”;

$returnHTML .= “n<TD align=”center” >&nbsp;</TD>”;

$returnHTML .= “n</TR >”;

foreach $key (sort { $a <=> $b }(keys %Students)) {

#View row

$returnHTML .=  “n<INPUT type=”hidden” name=”SerialNo” id=”SerialNo_$key” value=”$key”>”;

$returnHTML .= “n<TR style=”display:block”>”;

$returnHTML .=”n<TD align=”center” >$key</TD>”;

$returnHTML .=”n<TD align=”center”  nowrap><Div id=”Div_Name_$key”>”. (($Students{$key}->{Name}) ? $Students{$key}->{Name} : “&nbsp;”).”</DIV></TD>”;

$returnHTML .=”n<TD align=”center”  nowrap><Div id=”Div_Marks_$key”>”. (($Students{$key}->{Name}) ? $Students{$key}->{Marks} : “&nbsp;”).”</DIV></TD>”;

$returnHTML .= “n<TD  align=”center”><INPUT type=”button” name=”EditButton” value=”Edit” style=”cursor: hand; width: 40px” onClick=”makeRowEditable($cnt,’StudentInfoTable’)”></TD>”;

$returnHTML .= “n</TR >”;

#Edit row

$returnHTML .= “n<TR style=”display:none”>”;

$returnHTML .=”n<TD align=”center”  nowrap>$key</TD>”;

$returnHTML .=”n<TD align=”center”  nowrap><INPUT type=”text” size=”30″ id=”Student_Name_$key” value=”$Students{$key}->{Name}”  style=”text-align: center”></TD>”;

$returnHTML .=”n<TD align=”center”  nowrap><INPUT type=”text” size=”10″ id=”Student_Marks_$key” value=”$Students{$key}->{Marks}”  style=”text-align: center”></TD>”;

$returnHTML .= “n<TD  align=”center”><INPUT type=”button” name=”SaveButton” value=”Save” style=”cursor: hand; width: 40px” onClick=”saveStudInfo_JScript([‘Student_Name_$key’,’Student_Marks_$key’,’SerialNo_$key’,’NO_CACHE’],[‘Div_Name_$key’,’Div_Marks_$key’],’POST’);makeRowViewable($cnt,’StudentInfoTable’)”></TD>”;

$returnHTML .= “n</TR >”;

$cnt += 2;

}

$returnHTML .= “n</TABLE>”;

$returnHTML .= “n</TD></TR>”;

$returnHTML .= “n</Table>”;

}

#——————————————————————–

# Perl Subroutine which is called aschronously

#——————————————————————–

sub saveStudInfo_PerlScript {

# Accept Input

my $Name = shift;

my $Marks = shift;

my $SerialNo = shift;

# Call subroutine which does database update

update_Student_Info($SerialNo,$Name,$Marks);

# Return Output

return (@Return, ($Name ne “”) ? $Name : “&nbsp;”,($Marks ne “”) ? $Marks : “&nbsp;”);

}

#——————————————————————–

# Subroutine which fetches the data

#——————————————————————–

sub get_Student_Info {

my %Details;

my ($sql, $sth, $row);

# DSN with the name “mydsn”  points to the Db

$DB = “mydsn”;

# User name and password if any need to be specified. Currently no user name and pwd set.”;

$DB_USER = “”;

$DB_PASS= “”;

$dbh = DBI->connect(“dbi:ODBC:$DB”, $DB_USER, $DB_PASS);

$sql = “SELECT * FROM Student”;

$sth = $dbh->prepare($sql);

$sth->execute;

$cnt = 1;

while ($row = $sth->fetchrow_hashref) {

$Details{$cnt++} = $row;

}

$sth->finish();

return %Details;

}

#——————————————————————–

# Subroutine which updates the Student Table in DB

#——————————————————————–

sub update_Student_Info   {

my $SerialNo = shift;

my $Name = shift;

my $Marks = shift;

my ($sql, $sth,$row);

# DSN with the name “mydsn”  points to the Db

$DB = “mydsn”;

# User name and password if any need to be specified. Currently no user name and pwd set.

$DB_USER = “”;

$DB_PASS= “”;

$dbh = DBI->connect(“dbi:ODBC:$DB”, $DB_USER, $DB_PASS);

$sql  = “Update Student set Name = ‘$Name’,Marks = $Marks where Sl_No = $SerialNo “;

$sth = $dbh->prepare($sql);

$sth->execute;

$sth->finish();

return $sql;

}

============================================================

student.js

============================================================

/*

* Toggles the rows from EDIT mode to VIEW mode.

* Invoked on clicking the ‘SAVE’ button in last column.

*/

function makeRowViewable(rowNumber,id) {

var table = document.all ? document.all[id]  : document.getElementById ? document.getElementById(id) : null;

var editableRowNumber = rowNumber + 1 ;

var nonEditableRowNumber = editableRowNumber -1 ;

table.rows[editableRowNumber].style.display = “none” ;

table.rows[nonEditableRowNumber].style.display = “block” ;

}

/*

* Toggles the rows from view mode to edit mode.

* Invoked on clicking the ‘EDIT’ button in last column.

*/

function makeRowEditable(rowNumber,id) {

var table = document.all ? document.all[id]  : document.getElementById ? document.getElementById(id) : null;

var editableRowNumber = rowNumber + 1 ;

var nonEditableRowNumber = editableRowNumber -1 ;

table.rows[editableRowNumber].style.display = “block” ;

table.rows[nonEditableRowNumber].style.display = “none” ;

}


As we know, the term Ajax stands for ‘Asynchronous JavaScript And XML’. It is not a technology but a set of technologies being used together in a particular way. With effective use of existing technologies like HTML, DHTML, DOM, CSS, JavaScript one can make web pages more dynamic and interactive.

Using JavaScript, Ajax makes an asynchronous call to the server and fetches an XML document from a server-side component. Upon completion of the request, JavaScript may be used to update or modify the Document Object Model (DOM) of the HTML page. Only the necessary portions of the HTML DOM are re-rendered in the HTML page. In short Ajax techniques let you update parts of your web page without reloading the whole page.

The general benefit here is that an asynchronous operation executes in a separate thread. So when an operation is triggered asynchronously by the application, it can continue executing while the asynchronous method performs its task. Moreover only the data that needs to be updated or inserted can be sent instead of sending the entire form data. Perl Ajax framework are also available for faster development and test.

Popular web applications like  Gmail, Amazon, Orkut etc are using Ajax.

References and more links:

Simple Example demonstrating Ajax Implementation using Perl « Perl recipes « ActiveState Code.

http://www.perl.com/pub/2006/03/02/ajax_and_perl.html

http://perl.about.com/b/2006/03/17/using-ajax-from-perl.htm

http://ajaxpatterns.org/Perl_Ajax_Frameworks

http://search.cpan.org/~bpederse/CGI-Ajax-0.707/


Enjoy using Ajax from Perl.