简体   繁体   English

C#mvc 4中的Wkhtmltopdf编码问题

[英]Wkhtmltopdf encoding issue in C# mvc 4

I am using wkhtmltopdf to convert html to pdf. 我使用wkhtmltopdf将html转换为pdf。 The issues are fonts with charaters like č,š,ž,đ (those are charaters used by Serbian, Croatian, Slovenian language). 这些问题是字体,如č,š,ž,đ(这些是塞尔维亚语,克罗地亚语,斯洛文尼亚语使用的字符)。 They are not displayed coretly in pdf. 它们不会以pdf格式显示。 Html renders correctly. Html渲染正确。

This is how my html is constructed: 这就是我的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>

In my C# code where I am using wkhtmptopdf i do this 在我使用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);

So as you can see I am using utf-8 encoding on html and wkhtmltopdf as argument but the charaters do not render corectly. 所以你可以看到我在html和wkhtmltopdf上使用utf-8编码作为参数,但是字符不会呈现出核心。 What am I missing? 我错过了什么? Below is what i get in pdf. 以下是我在pdf中获得的内容。 English characters renders normaly. 英文字符呈现正常。

这是pdf作为图像

The default encoding for redirected streams is defined by your default code page. 重定向流的默认编码由您的默认代码页定义。 You need to set it to UTF-8. 您需要将其设置为UTF-8。

Unfortunately Process doesn't let you do this, so you need to make your own StreamWriter : 不幸的是, 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