繁体   English   中英

C#mvc 4中的Wkhtmltopdf编码问题

[英]Wkhtmltopdf encoding issue in C# mvc 4

我使用wkhtmltopdf将html转换为pdf。 这些问题是字体,如č,š,ž,đ(这些是塞尔维亚语,克罗地亚语,斯洛文尼亚语使用的字符)。 它们不会以pdf格式显示。 Html渲染正确。

这就是我的html构造方式:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Export</title>
</head>
<body>
    <h3>č,š,ž,đ</h3>
</body>
</html>

在我使用wkhtmptopdf的C#代码中,我这样做

        Process p;
        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = HtmlToPdfExePath;
        psi.WorkingDirectory = Path.GetDirectoryName(psi.FileName);

        // run the conversion utility
        psi.UseShellExecute = false;
        psi.CreateNoWindow = true;
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;

        // note: that we tell wkhtmltopdf to be quiet and not run scripts
        string args = "-q -n ";
        args += "--disable-smart-shrinking ";
        args += "--orientation Portrait ";
        args += "--outline-depth 0 ";
        args += "--page-size A4 ";
        args += "--encoding utf-8";
        args += " - -";

        psi.Arguments = args;

        p = Process.Start(psi);

所以你可以看到我在html和wkhtmltopdf上使用utf-8编码作为参数,但是字符不会呈现出核心。 我错过了什么? 以下是我在pdf中获得的内容。 英文字符呈现正常。

这是pdf作为图像

重定向流的默认编码由您的默认代码页定义。 您需要将其设置为UTF-8。

不幸的是, Process不允许你这样做,因此你需要制作自己的StreamWriter

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM