Welcome Guest Search | Active Topics | Sign In | Register

Setting Header and Footer template Options
Karthick M
Posted: Tuesday, November 7, 2017 6:46:19 AM
Rank: Member
Groups: Member

Joined: 6/16/2014
Posts: 22
We are evaluating moving from 2015 version of HTML to pdf converter to 2017 version. In 2015 version we have left the trigger mode to default value of Auto and the browser engine used to wait for all our ajax calls and javascript code to execute and then convert the HTML to PDF. In 2017 version, for the same url, we noticed that the conversion doesn’t wait till all the ajax calls and javascript code execution and produce pdf with intermediate content. So we tried with Manual trigger mode and added the eoapi.convert call on the client side. This seem to work for us. We need some suggestion related to the header. The content that we display in our header is dynamic and we used to populate that by calling javascript from the C# code

Our actual flow in the older version used to be:
1. Create HtmlToPdfSession object,
2. Load html using session.LoadHtml
3. Call session.ExecScript to get the content for the header from the client side
4. Set the header using session.Options.HeaderHtmlFormat
5. Call session.RenderAsPDF to create the final PDF.

With the newer version we are setting the trigger mode to Manual as mentioned above. With that, by the time we call session.ExecScript the required java script variable is not set, so header is not getting set. We tried the following after that:

1. Create HtmlToPdfSession object,
2. Add RunWebViewCallback to the session
3. In the callback register a c# method to be called from javascript using RegisterJSExtensionFunction
4. Call webView.LoadRequestAndWait(request);
5. Call session.RenderAsPDF to create the final PDF.

We called the C# method from javascript and the found that the expected value is reaching the server.
But when we set this value to the HeaderHtmlFormat of the session from within the C# method, we see that the header is empty in the final PDF. But when we re-execute the entire flow again we see that the header details set in the last execution is getting rendered in the subsequent execution, probably because both the execution uses the same thread and the header template set in the first is coming to effect for the second execution.

We tried using AfterPageRender Event to set the header, this works but the performance (overall time taken to create the pdf) is not in acceptable range.

Can you please provide some suggestion on the right place to set the header template for kind of requirement mentioned above?
eo_support
Posted: Tuesday, November 7, 2017 2:35:35 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,070
Hi,

Your method should work fine. What code do you use to set the session's HeaderHtmlFormat?

Thanks!
Karthick M
Posted: Wednesday, November 8, 2017 1:22:41 AM
Rank: Member
Groups: Member

Joined: 6/16/2014
Posts: 22
Hi,

Please find the code that I am trying.
First time the header is not set, but when I call executeJob on the same thread for the second time, it is setting the header.
But the header set second time round is value set during the first execution.

Code: C#
void executeJob()
   {
	EO.Base.Runtime.EnableEOWP = true;                      
	using (HtmlToPdfSession session = HtmlToPdfSession.Create())
	{
		session.Options.TriggerMode = HtmlToPdfTriggerMode.Manual;
		WebView internalView = (WebView)session.RunWebViewCallback((WebView webView, object args) =>
		{
			//Create a EO.WebBrowser.Request object
			Request request = new Request(fullUrl);
			webView.RegisterJSExtensionFunction("demoAbout", new JSExtInvokeHandler(WebView_JSDemoAbout));
			//Assign the session object to a class level variable so that we can access that in the registered Extension
			_currentSession = session;
			//Load the request                    
			webView.LoadRequestAndWait(request);
			return webView;
		}, null);


		string AbsoluteFilename = pdfPath.TrimEnd(new char[] { '\\' }) + "\\" + filename + ".pdf";
		var result = session.RenderAsPDF(AbsoluteFilename);
		}
   }
        
		
		
	//Called from Javascript
	void WebView_JSDemoAbout(object sender, JSExtInvokeArgs e)
        {
               LogMessage("Calling from the client side to set the header and footer");
                fromTimeHeader = e.Arguments[0] as string;
                toTimeHeader = e.Arguments[1] as string;
                if (!string.IsNullOrWhiteSpace(fromTimeHeader))
                    fromTimeHeader = fromTimeHeader.ToString() + " - ";
                var headerTemplate = string.Empty;
                string headerTemplate1 = String.Empty;
                string headerTemplate2 = String.Empty;

                //Read the html template and pass the from and to time received from the client side.
                ReadHeaderTemplateFromFiles(out headerTemplate1, out headerTemplate2);                

                headerTemplate = String.Format(headerTemplate2, _logoPath, headerFooterFontFamily,
                    this._reprtName, fromTimeHeader, toTimeHeader);                
                _currentSession.Options.HeaderHtmlFormat = headerTemplate;
                _currentSession.Options.HeaderHtmlPosition = 0.3f;
       }
eo_support
Posted: Wednesday, November 8, 2017 9:09:39 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,070
Hi,

This is mostly due to the fact that LoadRequestAndWait returned BEFORE your JavaScript code has done running. This is perfectly normal since the page is considered loaded WITHOUT considering any AJAX requests in the page. The loader has no idea when when or how your page will contact the server again through AJAX, so it will report the page has done loading as soon as the initial contents is loaded. For example, a stock quote web page can have AJAX calling back to the server every seconds forever. Obviously in that case the loader can not wait for it since it will never end.

You might have already run into this problem since you have already switched to manual trigger. However the manual trigger wait inside RenderAsPDF. You need to wait BEFORE RenderAsPDF. So if you add some synchronization in your code (make sure you wait until your JS extension function is called before you call RenderAsPDF the issue would most likely be resolved.

Thanks!
Karthick M
Posted: Sunday, November 19, 2017 10:31:11 PM
Rank: Member
Groups: Member

Joined: 6/16/2014
Posts: 22
Thanks for the suggestion, we will try it out.
eo_support
Posted: Monday, November 20, 2017 1:15:56 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,070
You are welcome. Please feel free to let us know if there is anything else.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.