我正在使用wkhtmltopdf.exe将HTML转换为PDF,使用以下源代码。问题是 - PDF显示“?”取代所有非英文字符,如中文,日文,俄文,阿拉伯文。以HTML格式输出时,字符显示正确。我尝试为HTML设置不同的编码(utf-8,utf-16,gb2312),但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);
}
答案 0 :(得分:0)
确保你的字体支持字符,你的源代码是UTF-8,它应该可以工作 - 我已经使用韩语,中文,波兰语和其他各种字符测试了wkhtmltopdf,它一直有效。请参阅我对其他类似问题https://stackoverflow.com/a/11862584/694325
的回答我写的html源代码,但我的PDF代码与你的非常相似。我会检查到处都是utf-8。
using (TextWriter tw = new StreamWriter(path, false, System.Text.Encoding.UTF8))
{
tw.WriteLine(contents);
}
从这样的源代码生成的PDF似乎没有问题。