我正在使用 wkhtmltopdf 应用程序将我的 ASP.net MVC 2 呈现的 HTML 转换为 PDF 并显示 PDF 而不是标准视图,以获得更好的打印能力。减去一件事,一切都很好。当我在 Web 服务器上的 MVC 应用程序中将 wkhtmltopdf 作为进程运行时,它不会在 PDF 中显示已安装的条形码字体。
这是该过程的代码。
public void HtmlToPdf(string url, string appPath)
{
string message = null;
// to build command argument
StringBuilder argument = new StringBuilder();
// input html file
string switches = "";
switches += "--print-media-type ";
switches += "--margin-top 10mm --margin-bottom 10mm --margin-right 10mm --margin-left 10mm ";
switches += "--page-size Letter ";
switches += "--load-error-handling ignore ";
switches += "--username admin ";
switches += "--password pass ";
argument.Append(switches + " " + url + " " + "C:\\PDF\\temp.pdf");
// to call the exe to convert
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = "C:\\wkhtmltopdf\\wkhtmltopdf.exe";
p.StartInfo.WorkingDirectory = "C:\\wkhtmltopdf";
p.StartInfo.Arguments = argument.ToString();
p.Start();
p.WaitForExit();
message = p.StandardError.ReadToEnd();
if (string.IsNullOrEmpty(message))
{
message = p.StandardOutput.ReadToEnd();
}
else
{
System.Diagnostics.Debug.WriteLine(message);
}
}
不太确定为什么它不会显示条形码,因为它会在您呈现 html 时显示,但不会在 wkhtmltopdf 将其转换为 pdf 时显示。如果您在我的 MVC 应用程序之外运行 wkhtmltopdf,它也可以正常工作。
-谢谢你的帮助