views:

157

answers:

3

I'm writing an application that would allow users to edit a calendar, its description and a few other things. I'm using jquery, php and mysql. Each time the user makes a change it asynchronously updates the database.

I'd like to give them the option of turning what they make into a pdf. Is there a way that I can post to my server the raw html of the page after the user makes changes?

I could regenerate the page using only php on the server, but this way would be easier if possible.

+4  A: 

You can use this to get most of the HTML for the page:

var htmlSource = document.getElementsByTagName('html')[0].innerHTML;

However it'll lack the opening and closing HTML tags and doctype, which probably won't matter to you as you could recreate that very easily back on the server.

I'll assume you can just use the same AJAX you're already using to send htmlSource to the server once you've grabbed it.

Daniel Lew
A: 

Regenerating the page from the server is going to be your best bet. To have a good downloading experience, you'll want to be able to send headers for Content-Type and size.

To answer your question, I would use output buffering to capture the output of your scripts, and then use one of the many tools available for turning HTML to PDF.

Jeremy DeGroot
+1  A: 

You can certainly return the innerHTML from jQuery any object that you can select, although it doesn't seem like the best way to go (see other answers for alternatives).

Watch out for XSS attacks. If you just run the HTML back and forth without checking it first you are leaving yourself open to major risks.

acrosman
Good point on the XSS loophole. On a related note, there are various browser plug-ins that modify the page. The point is that the page you get may not be exactly what you expect. If you regenerate it, you won't have that problem.
jdigital
A valid concern. I'll only be allowing trusted parties to use this atm.
uberstuber