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

html string with unicode utf-8 not getting printed to PDF file using Response object in C#

提问人:Agnib 提问时间:9/15/2023 最后编辑:Agnib 更新时间:9/18/2023 访问量:52

问:

我有一个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

评论

0赞 Klaus Gütter 9/15/2023
您可能希望从问题中删除您的许可证序列号
0赞 Agnib 9/18/2023
@KlausGütter - 是的,做到了。谢谢:)

答:

0赞 Agnib 9/18/2023 #1

所以,代码一切都很好。

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

需要升级版本,它起作用了。

谢谢大家。