Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

This is pdf as image

like image 576
TheMentor Avatar asked Dec 27 '12 13:12

TheMentor


1 Answers

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);
like image 69
Cory Nelson Avatar answered Sep 22 '22 06:09

Cory Nelson