How can I convert my current page to pdf after some content is dynamically added via AJAX?

I found several libraries or web services in PHP that do the job. The problem is that the conversion is done when the page is fully loaded, I would like to convert the page to PDF after some content dynamically added via AJAX to the onload event.

Thanks a lot, Omar

+4
source share
6 answers

Wow, thanks to everyone I did not know that this community is so active. To respond Beepcake to a project:

When the page loads the application, extracts biological information from more than 40 servers through an AJAX request, then a unique view is displayed where you can manipulate the graphics with many parameters.

So, the cool thing will be printed when the user makes his own version of the graphic. I think the best solution is POST all HTML with document.getElementsByTagName ('html') [0] .innerHTML, as RoBorg said, and then generated a PDF using a library like dompdf

+1
source

You can send document.getElementsByTagName('html')[0].innerHTML to the server (possibly using AJAX) and generate a PDF.

+5
source

When you add content with AJAX, this only happens on the client machine, so the server side PHP conversion code will not work with it. You will need to come up with a server-side method for including dynamic content if you want to create a PDF on the server.

+4
source

Perhaps you can implement an AJAX call that sent the page content / state to the server server after adding dynamic contens, where it can be represented as a PDF. You may not need the whole page, depending on where this "dynamic content" will go.

It seems a bit messy, but without knowledge of the project it's hard to say if there could be a cleaner method to do what you are trying to do.

+2
source

This is a client-side requirement due to the Ajax situation. If they use a Mac, they can just save as PDF, but otherwise you have little control over what they can do.

However, for server-side implementations, you can save a record of the page and all your AJAX requests from the moment this page loads and build HTML and PDF on the server from this record, if requested. Of course, this is not an easy task and rather overkill. You will probably be better off having a different mechanism for creating a server-side report on a different page than converting HTML to PDF.

+1
source

If your page can only be updated on the client without a trip to the server, you will have to publish a backup of your application. Thus, you will have all the content and will not rebuild, which would be impossible if there were only interactions on the client side.

+1
source

Source: https://habr.com/ru/post/1277645/


All Articles