Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top