【问题标题】:Rendering Google charts in Razor View Engine .net core在 Razor View Engine .net core 中渲染 Google 图表
【发布时间】:2020-12-22 00:19:58
【问题描述】:

直到最近,我才使用 Google 图表运行一些 pdf 报告,突然 Google 图表不会出现。报告的其余部分工作正常。我正在使用 razor 视图引擎来运行一个模板文件,该文件有 javascript 来加载谷歌图表。

模板文件有一个由<body onload=initCharts()> 触发的initCharts() 函数。该函数加载谷歌图表如下:

<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

这个模板文件然后被剃刀视图引擎加载如下:

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 返回html。 javascript 不在 razorview 中执行,因此图表不会被渲染。但是,如果我将输出复制到 HTML 文件并使用浏览器打开它,图表将按预期工作。在 google.charts.loadline 之后,javascript 似乎没有在 razorview 引擎中执行。

无论如何我可以看到在 razorview 引擎中执行 javascript 的结果吗?如果有错误,我可以看到错误吗?我可以加载像google这样的第三方脚本并在razorview引擎中执行吗?

我花了很多时间都无济于事。非常感谢您的帮助!

【问题讨论】:

  • 这里可能是一个错字,而不是在真实页面上,但值得检查。 onload 属性指向 --> initCharts -- 而 google 加载在一个名为 --> initChart 的函数中
  • @WhiteHat 是的,这是一个错字!我已经设法在initCharts 函数中加载谷歌图表的行之前和之后写入div。回调函数drawCharts() 永远不会被调用。
  • 您检查过浏览器控制台是否有错误?
  • 您的“加载各种图表的其他功能”让我很担心。您是否多次调用 google.charts.load?
  • @WhiteHat 该页面不会发送到浏览器,而是 DinkToPdf 有一个无头浏览器,可以运行该页面并将其转换为 pdf。我找不到从 DinkToPdf 中查看 javascript 错误的方法。

标签: razor charts google-visualization razor-2 dinktopdf


【解决方案1】:

问题已通过用 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。

【讨论】:

    猜你喜欢
    • 2013-11-26
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2021-12-07
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多