【问题标题】:Rotativa wkHTMLtoPDF header html utf8Rotativa wkHTMLtoPDF 标头 html utf8
【发布时间】:2018-06-23 10:48:32
【问题描述】:

我在 .NET Core 2 中使用 Rotativa (wkHTMLtoPDF)。当我想以波斯语 (utf-8) 将参数发送到 html 页眉和页脚时,它无法解析它,而是看到 ???? ????? ????我的话。

            var fileHeader = Path.Combine(Directory.GetCurrentDirectory(),
                       "wwwroot", "html", "pdfTemplate", "myHeader.html");
            var fileFooter = Path.Combine(Directory.GetCurrentDirectory(),
                       "wwwroot", "html","pdfTemplate", "myFooter.html");
            string cusomtSwitches = $"--footer-html {fileFooter} --footer-spacing 0 --header-html {fileHeader} --header-spacing 0 --replace test 15 --encoding utf-8 --replace totalValue \"بهمن شفیعی هستم\"";                
            var retVal = new ViewAsPdf("Preview", model)
            {
                //FileName = "Test.pdf",
                PageSize = RotativaCore.Options.Size.A4,
                PageOrientation = RotativaCore.Options.Orientation.Portrait,
                PageMargins = { Left = 1, Right = 1 },
                ContentDisposition = ContentDisposition.Inline,
                IsJavaScriptDisabled = false,
                DisableSmartShrinking=true,
                CustomSwitches = cusomtSwitches,
                IsLowQuality=false,

            };

我的 HTML 代码是:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" http-equiv="encoding" />
    <title></title>
    <script>
    function subst() {
        var vars = {};
        var query = document.location
            .toString()
            // get the query string
            .replace(/^.*?\?/, '')
            // and remove any existing hash string (thanks, @vrijdenker)
            .replace(/#.*$/, '')
            .split('&');

        for (var i = 0, l = query.length; i < l; i++) {
            var aux = decodeURIComponent(query[i]).split('=');
            vars[aux[0]] = aux[1];
        }
        var x = ['frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection', 'test', 'totalValue'];
        for (var i in x) {
            var y = document.getElementsByClassName(x[i]);
            for (var j = 0; j < y.length; ++j) y[j].textContent = vars[x[i]];

        }
    }

    </script>
</head>
<body onload="subst()">
<table class="table table-bordered;" style="font-family:'B Nazanin'" dir="rtl">
    <tr>
        <td style="text-align:right">
            جمع کل صورت حساب <span class="totalValue"></span>
        </td>
        <td style=" text-align:center"><img style="width:100px;" src="http://www.gammadesk.com/wp-content/uploads/2015/10/Gamma-2rang-22.png" /></td>
        <td>شماره سند:<span class="test"></span></td>
    </tr>
</table>

我的输出是:

【问题讨论】:

    标签: html utf-8 header wkhtmltopdf rotativa


    【解决方案1】:

    我找到了解决方案。 我在 c# 中编码字符串并在 javascript 上对其进行解码。

                String t1 = "بهمن شفیعی هستم";                
                List<int> arr = new List<int>();
                t1.Each(c => arr.Add(Convert.ToInt32(c)));                
                string cusomtSwitches = $"--footer-html {fileFooter} --footer-spacing 0 --header-html {fileHeader} --header-spacing 0 --replace test 15 --encoding \"utf-8\" --replace totalValue \"{string.Join(",", arr)}\"";
    

    Javascript 代码:

    var array=[];
    vars[css_selector_classes[css_class]].split(',').forEach(function (s, e) {
        array.push(s);
    });
    res = String.fromCharCode.apply(null,array);
    element[j].textContent = res;
    

    【讨论】:

      【解决方案2】:

      这对我有用:

       string.Format("  --footer-center \"{0}\"   --no-footer-line  --encoding 'UTF-8' "
      + " --footer-font-size \"12\" --footer-font-name \"B Mitra\" "
      + "--footer-spacing -2 "
      , "تست" );
      

      【讨论】:

        猜你喜欢
        • 2012-12-19
        • 2018-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-18
        • 2010-09-12
        • 2012-10-07
        相关资源
        最近更新 更多