简体   繁体   中英

Wkhtmltopdf encoding issue in C# mvc 4

I am using wkhtmltopdf to convert html to pdf. The issues are fonts with charaters like č,š,ž,đ (those are charaters used by Serbian, Croatian, Slovenian language). They are not displayed coretly in pdf. Html renders correctly.

This is how my html is constructed:

<!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

        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. What am I missing? Below is what i get in 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.

Unfortunately Process doesn't let you do this, so you need to make your own StreamWriter :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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