2

I am using NReco.PdfGenerator library to generate pdf from html. It is working fine for english language. I also required to include non-english language(marathi) , in that case not working like as expected. rendering black boxes.

This is my action method:

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");

}

I am also tried using

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

but not working. also put a try on decoding to unicode like this :

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

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

htmlContent = Encoding.Unicode.GetString(utf16Bytes);

this also not worked for me. My html contains characters like विषय,मूल्यांकन प्रकार this . in my pdf its showing as मà¥​लà¥​यांकन निकाल</p>

Some part of htmlContent:

   <tbody><tr>
                    <td  colspan="6"><p><b>मुल्यांकन निकाल&lt;/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>

Any help will be appreciated.

4

2 回答 2

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

That command worked for me.

于 2019-07-17T15:06:01.517 回答
0

I am posting this answer because in case any one facing same problem will be get helped:

I found a solution for this problem by using SelectPdf insted of NReco .

some sample code:

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;
}
于 2017-07-26T06:17:57.323 回答