我正在使用wkhtmltopdf.exe將HTML轉換爲PDF,使用下面的源代碼。問題是 - PDF顯示「?」代替中文,日文,俄文,阿拉伯文等所有非英文字符。以HTML格式輸出時,字符顯示正確。我試着對HTML(utf-8,utf-16,gb2312)設置不同的編碼,但PDF不能渲染非英文語言。wkhtmltopdf - 在導出的PDF中不顯示非英文字體
我在wkhtmltopdf論壇中讀到關於在服務器上安裝中文字體的問題,但看起來他們不適用於Windows服務器環境。此外,字體似乎可以在服務器上使用,因爲HTML呈現正確?
任何想法,使其工作?
代碼:
private void WritePDF(string html)
{
string inFileName,
outFileName,
tempPath;
Process p;
System.IO.StreamWriter stdin;
ProcessStartInfo psi = new ProcessStartInfo();
tempPath = Request.PhysicalApplicationPath
+ ConfigurationManager.AppSettings[Constants.AppSettings.ExportToPdfTempFolder];
inFileName = Session.SessionID + ".htm";
outFileName = Session.SessionID + ".pdf";
// run the conversion utility
psi.UseShellExecute = false;
psi.FileName = Server.MapPath(ConfigurationManager.AppSettings[Constants.AppSettings.ExportToPdfExecutablePath]);
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
//psi.StandardOutputEncoding = System.Text.Encoding.gb;
// note that we tell wkhtmltopdf to be quiet and not run scripts
// NOTE: I couldn't figure out a way to get both stdin and stdout redirected so we have to write to a file and then clean up afterwards
psi.Arguments = "-q -n - " + tempPath + outFileName;
p = Process.Start(psi);
try
{
stdin = p.StandardInput;
stdin.AutoFlush = true;
stdin.Write(html);
stdin.Close();
if (p.WaitForExit(15000))
{
// NOTE: the application hangs when we use WriteFile (due to the Delete below?); this works
Response.BinaryWrite(System.IO.File.ReadAllBytes(tempPath + outFileName));
}
}
finally
{
p.Close();
p.Dispose();
}
// delete the pdf
System.IO.File.Delete(tempPath + outFileName);
}
您是否設法解決此問題?任何進度報告?我最近已經將我的應用程序從磁盤訪問轉換爲直接流,並且它仍然正常工作。那麼,這仍然是一個問題? – Nenotlep 2013-10-21 07:00:16