1

I'm using ASP.NET WEBFORMS and Telerik and I want to export a document with Telerik components. I'm doing this with client export manager of Telerik https://demos.telerik.com/aspnet-ajax/client-export-manager/overview/defaultcs.aspx, and it works, but I need to render the entire DOM, then show to user, then user clicks on EXPORT button, and if I have more than one document it can be tedious for the user, so I want to export document without this preview, exporting the documents without user interaction. At this moment, I have seen this possible options:

  • Tried to export the DOM before showing to user (in Render() event for example) but I can't execute this with JavaScript because the DOM is not fully rendered.
  • Tried iText library, but this is not working with Telerik components (like graphics or grids)

The only thing on my mind right now is using something like https://phantomjs.org/ to automate the process where the user clicks the button automatically. Could someone tell me any other strategy that does not require a lot of programming time?

Edit:I tried Selenium to simulate the user interaction, but obviously when the file is downloaded it is saved on external navigator, there is a way to save it on the user's navigator?

Chariot
  • 55
  • 1
  • 13
  • @KJ What I am doing is opening a browser with Chromedriver at a certain time (when the user clicks export) and performing the actions described. With user browser I mean the one that does not open with Chromedriver – Chariot Apr 08 '22 at 13:22
  • 1
    can you register a javascript block at render or prerender event so it clicks on every export button ? – Kevin Dimey Apr 12 '22 at 08:26
  • @KevinDimey No, I tried this, but needs to be fully rendered and showing in screen – Chariot Apr 12 '22 at 08:50

2 Answers2

1

I am not sure I follow your description 100% But your DOM is for sure fully rendered after:

This Javascript snippet should allow you to do whatever you want after a complete page load:

<script type="text/javascript">
    document.addEventListener('readystatechange', event => { 
                if (event.target.readyState === "complete") {
                    alert("Dom is now fully rendered.");
                }
            });
    </script>

put your export command after the alert.

Morten Bork
  • 1,413
  • 11
  • 23
  • I think that with this change the problem of 'without this preview' would not be solved, the report would be seen on the screen and I want it to be done in "background" without the user being aware – Chariot Apr 19 '22 at 08:31
  • https://docs.telerik.com/reporting/t-telerik-reporting-processing-reportprocessor Use the "print" method? This should only create "file data" and not render anything. – Morten Bork Apr 21 '22 at 19:06
0

Better way is to use jquery to print the entire page or the part of the document(HTML element).

As jquery wait DOM to render fully to run code.

Create a Javascript function to print the page/element and call that function when ever you want.

<script>
function printDiv(divName) {
 var printContents = document.getElementById(divName).innerHTML;
 var originalContents = document.body.innerHTML;

 document.body.innerHTML = printContents;

 window.print();

 document.body.innerHTML = originalContents;
 }

 $(document).ready(function(){
  printDiv('body')
 });
 });
 </script>

Customize above code as your requirement.

How to print HTML content on click of a button, but not the page?

https://www.geeksforgeeks.org/how-to-execute-jquery-code/#:~:text=The%20purpose%20of%20using%20jQuery,can%20be%20taken%20by%20jQuery.