C# - NReco.PdfGenerator 非英文字符未呈现

C# - NReco.PdfGenerator non english character not rendering

我正在使用 NReco.PdfGenerator 库从 html 生成 pdf。它适用于英语。我还需要包括 non-english language(marathi) ,在那种情况下不能像预期的那样工作。渲染黑盒。

这是我的操作方法:

public ActionResult Assesment_DownloadResultSummaryData(string htmlContent){
  var htmlToPdf = new HtmlToPdfConverter();
                    var margins = new PageMargins();
                    margins.Bottom = 10; // margins.Left = 5;margins.Right = 5;
                    margins.Top = 10;
                    htmlToPdf.Margins = margins;
                    htmlToPdf.CustomWkHtmlPageArgs = "--enable-smart-shrinking  --encoding <encoding>";
                    htmlToPdf.PageFooterHtml = $@"page <span class=""page""></span> of <span class=""topage""></span><br />";
                    //htmlToPdf.Orientation = NReco.PdfGenerator.PageOrientation.Portrait;
                    htmlToPdf.Zoom = 1.0f;
                    return File(htmlToPdf.GeneratePdf(htmlContent, null), "application/pdf","myFile.pdf");

}

我也试过用

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

但不工作。也尝试像这样解码为 un​​icode :

      byte[] utf8Bytes = Encoding.Default.GetBytes(htmlContent);

       byte[] utf16Bytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, utf8Bytes);

htmlContent = Encoding.Unicode.GetString(utf16Bytes);

这对我也不起作用。 我的 html 包含像 विषय,मूल्यांकन प्रकार 这样的字符。在我的 pdf 中它显示为 मॠलॠयांकन निकाल

部分html内容:

   <tbody><tr>
                    <td  colspan="6"><p><b>मुल्यांकन निकाल</b></p></td>
            </tr>
            <tr>

                <td style="width:5%"><h4><b>वर्ग :</b></h4></td>
                <td style="width:10%;text-align:left;"><h4><b>Class 1-A</b></h4></td>
                <td style="width:10%"><h4><b>बॅच :</b></h4></td>
                <td style="width:40%;text-align:left;"><h4><b> 25-07-2017-Class 1-A-General Knowledge-English-1</b></h4></td>
                <td style="width:20%"><h4><b>विद्यार्थ्यांची एकूण संख्या :</b></h4></td>
                <td style="width:10%"><h4><b>18</b></h4></td>
            </tr>
            <tr >
                <td style="width:20%"><h4><b>विषय :</b></h4></td>
                <td style="width:10%;text-align:left;"><h4><b>English</b></h4></td>
                <td style="width:10%"><h4><b>विषय :</b></h4></td>
                <td style="width:30%;text-align:left;"><h4><b>General Knowledge</b></h4></td>
                <td style="width:20%"><h4><b>उपस्थित विद्यार्थी संख्या :</b></h4></td>
                <td style="width:10%"><h4><b>17</b></h4></td>
            </tr>

        </tbody>

我们将不胜感激。

我发布这个答案是因为万一有人遇到同样的问题 获得帮助:

我通过使用 NReco 的 SelectPdf insted 找到了解决这个问题的方法。

一些示例代码:

public ActionResult ConverToPdf(string htmlContent)
        {
            try
            {

                // instantiate a html to pdf converter object
                HtmlToPdf converter = new HtmlToPdf();
                PdfPageSize pageSize = PdfPageSize.A4;
                PdfPageOrientation pdfOrientation = PdfPageOrientation.Portrait;
                int webPageWidth = 1024;
                // set converter options
                converter.Options.PdfPageSize = pageSize;
                converter.Options.PdfPageOrientation = pdfOrientation;
                converter.Options.WebPageWidth = webPageWidth;
                converter.Options.MarginLeft = 10;
                converter.Options.MarginRight = 10;
                converter.Options.MarginTop = 5;
                converter.Options.MarginBottom = 5;
                //converter.Options.WebPageHeight = webPageHeight;

                // create a new pdf document converting an url
                PdfDocument doc = converter.ConvertHtmlString(htmlContent);

                // save pdf document
                byte[] pdf = doc.Save();

                // close pdf document
                doc.Close();

                // return resulted pdf document
                FileResult fileResult = new FileContentResult(pdf, "application/pdf");
                fileResult.FileDownloadName = "Document.pdf";
                return fileResult;
}
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

这个命令对我有用。