在 Razor View Engine .net 核心中呈现 Google 个图表
Rendering Google charts in Razor View Engine .net core
我一直在运行使用 Google 图表制作一些 pdf 报告,直到最近,突然 Google 图表不会出现。报告的其余部分工作正常。我正在使用 razor 视图引擎 运行 一个模板文件,该文件具有 javascript 来加载 google 图表。
模板文件有一个由 <body onload=initCharts()>
触发的 initCharts()
函数。此函数加载 Google 个图表如下:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
function initChart() {
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawCharts);
}
other functions to load various charts
此模板文件随后由 razor 视图引擎加载,如下所示:
public async Task<string> RenderToStringAsync(string viewName, object model, string logoUrl)
{
Dictionary<object, object> dictionary = new Dictionary<object, object>();
dictionary.Add("LogoUrl", logoUrl);
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider, Items = dictionary };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
using (var sw = new StringWriter())
{
var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);
if (viewResult.View == null)
{
throw new ArgumentNullException($"{viewName} does not match any available view");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = model
};
var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
sw,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
return sw.ToString();
}
}
razorview 引擎的输出(来自 RenderToStringAsync
函数)提供给 DinkToPdf 库以转换为 pdf。
我遇到的问题是,当 google.charts.load
行执行时,它失败或在加载 javascript razorview returns html 之前失败。 javascript 不会在 razorview 中执行,因此不会呈现图表。但是,如果我将输出复制到 HTML 文件并使用浏览器打开它,图表将按预期工作。 javascript 似乎没有在 google.charts.load
行之后在 razorview 引擎中执行。
我是否可以在 razorview 引擎中看到执行 javascript 的结果?如果有错误,我可以看到吗?我可以加载像 google 这样的第三方脚本并在 razorview 引擎中执行它吗?
我花了很多时间都无济于事。非常感谢您的帮助!
问题已通过使用 PhantomJS 替换 DinkToPdf 来创建 PDF 来解决。 DinkToPdf 无法加载 Google 个图表。这可能是由于 Google 将对 jsapi 的请求重定向到较新的 loader.js,由于某些原因 DinkToPdf 无法处理。
运行 DinkToPdf 使用的 wkhtmltopdf 在命令行上访问 https 时显示如下错误:
QSslSocket: cannot resolve CRYPTO_num_locks ] 10%
QSslSocket: cannot resolve CRYPTO_set_id_callback
QSslSocket: cannot resolve CRYPTO_set_locking_callback
QSslSocket: cannot resolve sk_free
QSslSocket: cannot resolve sk_num
QSslSocket: cannot resolve sk_pop_free
QSslSocket: cannot resolve sk_value
QSslSocket: cannot resolve SSL_library_init
QSslSocket: cannot resolve SSL_load_error_strings
QSslSocket: cannot resolve SSLv3_client_method
QSslSocket: cannot resolve SSLv23_client_method
QSslSocket: cannot resolve SSLv3_server_method
QSslSocket: cannot resolve SSLv23_server_method
QSslSocket: cannot resolve X509_STORE_CTX_get_chain
QSslSocket: cannot resolve OPENSSL_add_all_algorithms_noconf
QSslSocket: cannot resolve OPENSSL_add_all_algorithms_conf
QSslSocket: cannot resolve SSLeay
QSslSocket: cannot call unresolved function CRYPTO_num_locks
QSslSocket: cannot call unresolved function CRYPTO_set_id_callback
QSslSocket: cannot call unresolved function CRYPTO_set_locking_callback
QSslSocket: cannot call unresolved function SSL_library_init
QSslSocket: cannot call unresolved function SSLv23_client_method
QSslSocket: cannot call unresolved function sk_num
QSslSocket: cannot call unresolved function SSLv23_client_method3%
QSslSocket: cannot call unresolved function SSL_library_init
Warning: Failed to load https://www.gstatic.com/charts/loader.js (ignore)
QSslSocket: cannot call unresolved function SSLv23_client_method
QSslSocket: cannot call unresolved function SSL_library_init
由于我们无法解决这个问题,我们决定使用 PhantomJS,它可以很好地创建 PDF。
我一直在运行使用 Google 图表制作一些 pdf 报告,直到最近,突然 Google 图表不会出现。报告的其余部分工作正常。我正在使用 razor 视图引擎 运行 一个模板文件,该文件具有 javascript 来加载 google 图表。
模板文件有一个由 <body onload=initCharts()>
触发的 initCharts()
函数。此函数加载 Google 个图表如下:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
function initChart() {
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawCharts);
}
other functions to load various charts
此模板文件随后由 razor 视图引擎加载,如下所示:
public async Task<string> RenderToStringAsync(string viewName, object model, string logoUrl)
{
Dictionary<object, object> dictionary = new Dictionary<object, object>();
dictionary.Add("LogoUrl", logoUrl);
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider, Items = dictionary };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
using (var sw = new StringWriter())
{
var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);
if (viewResult.View == null)
{
throw new ArgumentNullException($"{viewName} does not match any available view");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = model
};
var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
sw,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
return sw.ToString();
}
}
razorview 引擎的输出(来自 RenderToStringAsync
函数)提供给 DinkToPdf 库以转换为 pdf。
我遇到的问题是,当 google.charts.load
行执行时,它失败或在加载 javascript razorview returns html 之前失败。 javascript 不会在 razorview 中执行,因此不会呈现图表。但是,如果我将输出复制到 HTML 文件并使用浏览器打开它,图表将按预期工作。 javascript 似乎没有在 google.charts.load
行之后在 razorview 引擎中执行。
我是否可以在 razorview 引擎中看到执行 javascript 的结果?如果有错误,我可以看到吗?我可以加载像 google 这样的第三方脚本并在 razorview 引擎中执行它吗?
我花了很多时间都无济于事。非常感谢您的帮助!
问题已通过使用 PhantomJS 替换 DinkToPdf 来创建 PDF 来解决。 DinkToPdf 无法加载 Google 个图表。这可能是由于 Google 将对 jsapi 的请求重定向到较新的 loader.js,由于某些原因 DinkToPdf 无法处理。
运行 DinkToPdf 使用的 wkhtmltopdf 在命令行上访问 https 时显示如下错误:
QSslSocket: cannot resolve CRYPTO_num_locks ] 10%
QSslSocket: cannot resolve CRYPTO_set_id_callback
QSslSocket: cannot resolve CRYPTO_set_locking_callback
QSslSocket: cannot resolve sk_free
QSslSocket: cannot resolve sk_num
QSslSocket: cannot resolve sk_pop_free
QSslSocket: cannot resolve sk_value
QSslSocket: cannot resolve SSL_library_init
QSslSocket: cannot resolve SSL_load_error_strings
QSslSocket: cannot resolve SSLv3_client_method
QSslSocket: cannot resolve SSLv23_client_method
QSslSocket: cannot resolve SSLv3_server_method
QSslSocket: cannot resolve SSLv23_server_method
QSslSocket: cannot resolve X509_STORE_CTX_get_chain
QSslSocket: cannot resolve OPENSSL_add_all_algorithms_noconf
QSslSocket: cannot resolve OPENSSL_add_all_algorithms_conf
QSslSocket: cannot resolve SSLeay
QSslSocket: cannot call unresolved function CRYPTO_num_locks
QSslSocket: cannot call unresolved function CRYPTO_set_id_callback
QSslSocket: cannot call unresolved function CRYPTO_set_locking_callback
QSslSocket: cannot call unresolved function SSL_library_init
QSslSocket: cannot call unresolved function SSLv23_client_method
QSslSocket: cannot call unresolved function sk_num
QSslSocket: cannot call unresolved function SSLv23_client_method3%
QSslSocket: cannot call unresolved function SSL_library_init
Warning: Failed to load https://www.gstatic.com/charts/loader.js (ignore)
QSslSocket: cannot call unresolved function SSLv23_client_method
QSslSocket: cannot call unresolved function SSL_library_init
由于我们无法解决这个问题,我们决定使用 PhantomJS,它可以很好地创建 PDF。