簡體   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