使用 C# 中的 Response 对象无法将带有 unicode utf-8 的 html 字符串打印到 PDF 文件

问题描述 投票:0回答:1

我有一个 HTML 表单,它接受用户的数据。用户可以是世界各地的任何人,因此需要 UTF-8 支持,以便用户可以使用任何语言输入数据 - 阿拉伯语、中文、日语等。

目前正在用中文输入数据,将数据导出为PDF时,不显示中文。但是,如果是英文,PDF 会正确显示。

我正在使用 SautinSoft.PdfMetamorphosis,它是生成 PDF 输出的第三部分工具。

SautinSoft.PdfMetamorphosis oPdf = new SautinSoft.PdfMetamorphosis();
            oPdf.PageStyle.PageSize.A4();
            oPdf.PdfOptions.PdfVersion = SautinSoft.PdfMetamorphosis.CPdfOptions.ePdfVersion.PDF_A;

            //oPdf.UnicodeOptions.DetectFontsDirectory = SautinSoft.PdfMetamorphosis.CUnicodeOptions.eUnicodeDetectFontsDirectory.Custom;
            oPdf.TextStyle.FontFace.Custom("Arial Unicode MS");
            //oPdf.TextStyle.FontSize = 20;
            
            byte[] bBytes = oPdf.HtmlToPdfConvertStringToByte(OutputAsTable(fUserAdmin, pTranslation, pReplace, pCustom));
            oPdf = null;

            if (pCustom)
            {
                var oCustom = new Custom();
                oCustom.DropTable("VW_TR_TBL_Custom_" + fCompanyID + "_" + fEmployeeID);
            }

            var Response = HttpContext.Current.Response;

            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            //Response.ContentType = "application/pdf";
            Response.ContentType = "text/html";
            Response.AddHeader("content-disposition", "attachment;filename=" + fFormName + ".pdf");
            //Response.Charset = "utf-8";
            //Response.ContentEncoding = Encoding.UTF8;
            Response.BinaryWrite(bBytes);
            Response.Flush();
            Response.End();

上面的注释行我也尝试过,但根本不起作用。

c# unicode utf-8 httpresponse html-to-pdf
1个回答
0
投票

所以,代码一切都很好。

据我发现,阿拉伯语运行良好,但中文、日语、韩语不支持,这是由于 PDF Metamorphosis 版本所致。

需要升级版本,成功了。

谢谢大家。

© www.soinside.com 2019 - 2024. All rights reserved.