为什么阿拉伯语在wkhmltopdf中无法正确显示?

时间:2015-01-19 20:34:32

标签: c# asp.net wkhtmltopdf

以下脚本的问题是什么?我使用wkhtmltopdf将HTML转换为PDF。它不会在PDF输出中正确显示阿拉伯字体,而英文字体显示正常。我该如何摆脱这个问题?我已经尝试了几个脚本并进行了一些修改,但问题仍然存在。

private void WritePDF(string HTML)
            {
                string inFileName, outFileName;
                Process p;
               // System.IO.StreamWriter stdin;
                ProcessStartInfo psi = new ProcessStartInfo();


                inFileName = Session.SessionID + ".htm";

               outFileName = Path.Combine(Server.MapPath("~/PDF"), "test.pdf");
               // outFileName = Request.PhysicalApplicationPath + "temp\\" + Session.SessionID + ".pdf";
                psi.UseShellExecute = false;

                psi.FileName = Path.Combine(Server.MapPath("~/PDFConverter"), "wkhtmltopdf.exe");
                psi.CreateNoWindow = true;
                psi.RedirectStandardInput = true;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;


                psi.Arguments = "-q -n - " + outFileName;
                p = Process.Start(psi);

                try
                {
                    StreamWriter stdin = new StreamWriter(p.StandardInput.BaseStream, Encoding.UTF8);
                    stdin = p.StandardInput;
                    stdin.AutoFlush = true;
                  //  byte[] bytes = Encoding.Default.GetBytes(HTML);
                   // HTML = Encoding.UTF8.GetString(bytes);
                    stdin.Write(HTML);
                    stdin.Close();

                    byte[] fileContent = null;
                    if (p.WaitForExit(15000))
                    {
                        FileStream fs = new FileStream(outFileName, FileMode.Open, FileAccess.Read);
                        fileContent = new byte[(int)fs.Length];

                        //read the content
                        fs.Read(fileContent, 0, (int)fs.Length);

                        //close the stream
                        fs.Close();
                        Response.Clear();
                        Response.ContentType = "application/pdf";
                        Response.AppendHeader("Content-Disposition", "attachment; filename=pg.pdf");
                        Response.BinaryWrite(fileContent);
                        Response.End();

                    }
                }
                finally
                {
                    p.Close();
                    p.Dispose();
                }



            }

1 个答案:

答案 0 :(得分:0)

在我看来,你使用了错误的编码;据我所知,ASCII不支持阿拉伯语。您可以检查正确的编码here,或者如果要对文件执行编码转换,请选中here。希望有所帮助!

或者,您可以更改

 StreamWriter stdin = new StreamWriter(p.StandardInput.BaseStream, Encoding.UTF8);

如果你想对阿拉伯语使用Windows-1256编码:

StreamWriter stdin = new StreamWriter(p.StandardInput.BaseStream, Encoding.GetEncoding(1256));

或者如果您想使用ISO-8859-6阿拉伯语:

StreamWriter stdin = new StreamWriter(p.StandardInput.BaseStream, Encoding.GetEncoding(28596));

如果您想使用任何其他编码表,请查看我为您提供的第一个链接。 提示:阅读那里的评论表。