0

我正在构建一个 POC,在其中我使用 razorlight 编译 index.cshtml,然后使用 dinktopdf 生成一个 pdf,到目前为止,我的那部分工作正常。

接下来,使用 vuejs 和 vue-qrcode,我能够将 qrcode 添加到 qr-code 标签转换为画布并显示 qrcode 的页面。

现在,我遇到的问题是,在生成 pdf 的过程中,vue-qrcode 没有被编译成常规的 html(它应该是一个画布标签),并且没有任何东西被添加到 pdf 中。

解决方案在以下存储库中

page = await engine.CompileRenderAsync(path, model);显示 html 的完整字符串,在那里您可以看到 qr-code 标记仍然保持原样。

    public async Task<byte[]> PrintPDFAsync()
    {
        var engine = new RazorLightEngineBuilder()
                        .UseFileSystemProject(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))
                        .UseMemoryCachingProvider()
                        .Build();

        var model = new List<WeatherForecast>(Get());
        string page = null;
        try
        {
            var path = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), $"Views/Home/Index.cshtml");
            page = await engine.CompileRenderAsync(path, model);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        var doc = new HtmlToPdfDocument()
        {
            GlobalSettings = {
                ColorMode = ColorMode.Color,
                Orientation = Orientation.Landscape,
                PaperSize = PaperKind.A4Plus
            },
            Objects = {
                new ObjectSettings() {
                    PagesCount = true,
                    HtmlContent = page,
                    WebSettings = { DefaultEncoding = "utf-8" },
                    HeaderSettings = { FontSize = 9, Right = "Page [page] of [toPage]", Line = true, Spacing = 2.812 }
                }
            }
        };

        byte[] pdf = null;
        try
        {
            pdf = _pdfConverter.Convert(doc);

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

        return pdf;
    }
4

1 回答 1

0

我能够使用纯 JavaScript QRCode而不是 vue-qrcode 库来解决这个问题。

最后的看法

@model IEnumerable<POCWeb.Models.WeatherForecast>

@{
    var BaseUrl = @Model.FirstOrDefault().BaseUrl;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="stylesheet" href="@BaseUrl/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="@BaseUrl/lib/vue/css/bootstrap-vue.min.css" />
</head>
<body>
    <div id="app">
        <header>
            <div class="container">
                <img src="@BaseUrl/img/logo.png" class="img-thumbnail">
            </div>
        </header>
        <div class="container">
            <main role="main" class="pb-3">
                <table class="table">
                    <thead>
                        <tr>
                            <td>
                                Date
                            </td>
                            <td>
                                TemperatureC
                            </td>
                            <td>
                                Summary
                            </td>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td>
                                    @item.Date
                                </td>
                                <td>
                                    @item.TemperatureC
                                </td>
                                <td>
                                    @item.Summary
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </main>
        </div>
        <div class="container">
            <div id="qrcode"></div>
        </div>
        <footer class="border-top footer text-muted">
            <div class="container">
                &copy; 2020 - POCWeb - <a asp-area="" asp-page="/Privacy">Privacy</a>
            </div>
        </footer>
        <p id="log"></p>
    </div>

    <script src="@BaseUrl/js/qrcode.js"></script>

    <script>
            try {
                new QRCode(document.getElementById("qrcode"), "http://jindo.dev.naver.com/collie");

                var c = document.getElementById("log");
                c.innerHTML = "printed";
            }
            catch (err) {
                var c = document.getElementById("log");
                c.innerHTML = "catch: " + err.messge;
            }
    </script>
</body>
</html>
于 2020-11-29T07:39:53.580 回答