Category Archives: ajax

html2pdf using jsp and linux

Have you ever thought of downloading the current viewed page as PDF. Here’a  quick program which can read an HTML and convert it to PDF using JSP or Servlet. I am going to call an external linux command called ‘convert’ which does all the magic for me.

All I need is a linux box with ImageMagick setup along with html2ps and ghostscript. I can get this very very easily from my ubuntu’s apt-get install …

So here’s the small snippet which I wrote after setting up.

try
{
Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec(“convert /usr/local/share/pdfformat1.html /usr/local/share/pdfformat.pdf”  ) ;
p.waitFor();   //<– This waits until the operation is over
System.out.println(p.exitValue());  //<– if the output is 0 it means pdf was generated successfully
}
catch (Exception e){
e.printStackTrace();
}

Now this can be quite a good replacement for iText html to pdf conversion which is quite buggy (I am a huge fan of iText, but somehow this HTML needs to be rendered properly I feel)

After having got the file, read the file and reply to your browser by response.setContentType(“application/pdf”);

Please note, ONLY valid HTML tags are allowed. This method doesn’t support CSS. If someone knows of it, please let me know.This method works perfectly well with valid HTML syntax. So if your output pdf isn’t looking as expected, please check your HTML.

You can use this little utility to check your valid HTML #: sudo apt-get install wdg-html-validator and then run #: validate filename.html
This will list out all the invalid HTML tags.

So for all you report building guys out there, here’s a quick tip: Just output your report as HTML <offcourse with data poluated> and then use this utility to generate a PDF out of it!

Some Javascript Tips

I have been doing a lot of JavaScript these days. Each day I learn newer things and each day I am starting to like it more and more.

I am going to compile a few queries where I broke my head.

Q) If I do window.open(‘anotherPage.html’), How can I change the DOM of the parent window.
This problem arrived when I was trying to add more rows to a table from another window itself.
Problem: Firefox allows it. But IE doesn’t allow.
Solution: Use cloneNode() for FF. For IE , create the row again by attachRow()
Now for each cell, loop through it and do insertCells() and then copy the innerHTML to it.
All this is done in a simple loop through the table.

Q I have 3 frames on a page, I want to submit requests to page 2 and 3 at the same time
Do: document.forms[2].functionName();
yes, its as easy as this…
in your functionName(), you can add code to submit the form.submit();

Q Recursive Ajax calls.
Problem: To keep refreshing content of a page through Ajax.
One might typically use setInterval() but what if setInterval fires while a request from your ajax is coming back?
Solution: Use a global variable called “lock”. Everytime you make a request set this lock, the call back function should release the lock.
When setInterval fires your ajax request function it should exit if its locked.

Q. getElementById(‘idname’). Don’t use it too often, Try to traverse though the DOM by your code.

Q. Always give IDs to your form. Someday you’ll find that this is very useful. You can submit a form from anywhere if you do this by getting the refrence of the form and then yourform.submit()

Q. Try to avoid excess IDs, This will occupy more browser memory interms of DOM parsing.
For example, If you have a table, don’t give ID to each row. instead give the id to the table and then traverse the table.

Q. Do overuse Ajax and submit multiple requests simultaneously. Browser might allow this, but your database will not. Specially when you’re doing connection pooling. In connection pooling , you have the same connection object and multiple execution of sql queries on the same object from different clients will result in failed transactions.

Q. attaching events is a good idea , but beware, IE and FF behaves differently. The API is completely different.

Q. Always use a common Javascript Browser detection Module. Make the detected browser global and then customize your scripts that way.

Q. Often while debugging we use alert(“debug msg here..”); and waste time in commenting and uncommenting it. Instead use a global variable called “debug=true”. And use if (debug) alert(“”);
You wont have to comment or uncomment it all the time.

Q. There are some good Javascript debuggers. Use them. The simplest one is FF error console. But what about IE? Yes, now you can use FF error console to debug IE related Errors too.. Install the FF IE tab plugin and have fun.

Q. Another great FF extension is Firebug. MUST MUST have for all you JS guys….

Barcamp Kolkata : Why Kolkata still doesn’t have a Barcamp

Why Kolkata still doesn’t have a Barcamp

October 2007, I went to Kolkata for a conference called WebOSS ’07 and I can probably make out why there’s no way Kolkata can have a barcamp in near future. I tried to carry out a Barcamp in Kolkata. My sympathies to the wonderful city where I grew up.

1. What is BarCamp? A ‘Drinking Bar’ may be…I’m not interested.
2. People don’t know what a WIKI is. They don’t know how to edit it despite the instructions.
3. No one willing to take the effort. They have all the time to sleep.
4. The so called ‘open source’ community sucks big time. The so called HEAD of LUG group look out for publicity more than helping to get a job done. I am not afraid to take the name of Krishnendu Paul who promised and suddenly disappeared.
5. The student community is suppressed by the colleges.
6. The colleges are more interested in Placements rather than knowledge sharing.
7. College professors are trying to get some freelance projects all the time.
8. Big shots like IBM, TCS etc. are least bothered to even have a look.
9. Free T-shirts = Bring in all your friends and leave in next 1 hour.
10. Free food = hog as much as you want.
11. The IT dept of the government want to just hold on conferences and publicity stunts to make their place in the newspaper. I am again not afraid to blame the minister for not checking his Mailbox which is always full. And also to be not available to common people without a PROTOCOL!
11. What is sponsorship? What will I gain. How many people are going to come? This is the question which all news/publicity media ask!
12. Lastly, Who’s wanting to spoil their weekend!

Way to go Kolkata, I don’t see a Barcamp Kolkata until some revolutionary thing happen!
Dear City of Joy, I left you some time back and I don’t feel like returning!

Barcamp Pune 4

“Barcamp Pune 4: Love for technology” took place today at Thoughtworks Pune.
I gave a workshop on PHP & Ajax along with Priyank. This time I didn’t develop any website but rather a Proxy server using PHP!
We also conducted a BOF session on PHP and Ajax Security and Meta Trader the online forex platform.

Get some pics here:

Barcamp Pune 4 Logo


PHP & Ajax workshop


Barcamp schedule.


Preparing for workshop

Slides and Video to come next..