如何在C#中获取wkhtmltopdf日志信息?

时间:2013-02-06 16:28:49

标签: c# pdf-generation wkhtmltopdf

我正在使用wkhtmltopdf将某些html转换为pdf。我想我得到了一些javascrpot错误,我想从wkhtmltopdf访问一些debug \ logging。有谁知道如何获取日志信息?

        var wkhtmlDir = Settings.HtmlToPdfFolderPath;
        var wkhtml = Settings.HtmlToPdfExePath;
        var p = new Process();

        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.FileName = wkhtml;
        p.StartInfo.WorkingDirectory = wkhtmlDir;

        string switches = "";
        switches += "--print-media-type --redirect-delay 800 ";
        //switches += "--margin-top 10mm --margin-bottom 10mm --margin-right 10mm --margin-left 10mm ";
        switches += "--page-size Letter ";
        p.StartInfo.Arguments = switches + " " + url 
        p.Start();

        //read output
        byte[] buffer = new byte[32768];
        byte[] file;
        using (var ms = new MemoryStream())
        {
            while (true)
            {
                int read = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length);

                if (read <= 0)
                {
                    break;
                }
                ms.Write(buffer, 0, read);
            }
            file = ms.ToArray();
        }

        // wait or exit
        p.WaitForExit(60000);

        // read the exit code, close process
        int returnCode = p.ExitCode;
        p.Close();

        //return returnCode == 0 ? file : null;

        return file;

1 个答案:

答案 0 :(得分:2)

您可以阅读标准错误和标准输出:

String output =string.Format("STD: {0}\nErr: {1}", p.StandardOutput.ReadToEnd(), p.StandardError.ReadToEnd());

这就是你要找的东西吗?

相关问题