0

I am using wkhtmltopdf (https://www.nuget.org/packages/wkhtmltopdf.mingw.86.exe/) to generate PDF file for ASP.NET web page. Date and time is shown to user in this web page which I get from SQL Server 2012 DB. I store all dates in UTC format. When I open web page in browser then date and time is displayed as local date and time because I am using Moment.js to format it based on user local timezone.

Is it possible to do the same thing when generating PDF using wkhtmltopdf? PDF generation happens on server so it is always displaying server date and time, but I want to display user local date and time.

Viktors Telle
  • 763
  • 9
  • 22

1 Answers1

1

Great tool. And it has some features.

I would recommend putting some script on the page to evaluate all date fields with jQuery and manipulate date fields specifically? If your string is in the format yyyy-MM-dd HH24:mm:ss.zzz UTC like the value 2015-10-27 13:58:46.490 UTC then you can parse it in JS with new Date('2015-10-27 13:58:46.490 UTC') and call .toString() on it to get the local time.

Then you can use the --window-status argument with wkhtmltopdf to wait for a given status from your script having run. So let's say we pass in --window-status "Dates Manipulated" into wkhtmltopdf. When your client-side script has completed manipulating the date fields to the local time you want, set window.status = 'Dates Manipulated' and wkhtmltopdf will finish your document conversion.

Since you're rendering this on the server side, I presume you have access to the URL being set. Let's assume your page is something like http://www.example.com/page_renderer and you put a query argument on the back like http://www.example.com/page_renderer?timzeone=America/Toronto. Now you can use that timezone in your Javascript function to convert to the specified timezone.

var localTime = moment.tz("2015-10-27 13:58:46.490 UTC", "America/Toronto");
Mark S
  • 401
  • 4
  • 8
  • I think I understand what you are saying, but how does JS will know the right timezone of the user? PDF conversion happens on server side so it will always be server local time at the end. Or I did not fully understand your idea? – Viktors Telle Oct 27 '15 at 14:24
  • I saw this article on moment.js that talks about time zone conversion... http://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript – Mark S Oct 27 '15 at 14:28
  • 1
    So per that, what if when you call the URL you put the timezone in the query part of the URL? And then when the JS runs, use the value in the query to dictate how timezones are specified? – Mark S Oct 27 '15 at 14:29
  • Thank you. I will definitely try URL query string option. – Viktors Telle Oct 27 '15 at 14:32
  • I edited my answer with my suggestion, based on your use of moment.js. Hope that helps... – Mark S Oct 27 '15 at 14:34