0

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

模板文件有一个initCharts()<body onload=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

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

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

我花了很多时间都无济于事。您的帮助将不胜感激!

4

1 回答 1

0

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

于 2020-09-11T11:16:56.300 回答