与wkhtmltopdf的PDF生成问题

时间:2013-03-14 11:31:30

标签: c# pdf-generation wkhtmltopdf

wkhtmltopdf.exe

的帮助下,PDF生成存在问题

这是我的代码:

public bool WKHtmlToPdf(string Url, string outputFilename)
{
    string filename = "E:\\XYZ" + "\\" + outputFilename + ".pdf";

    var p = new System.Diagnostics.Process();
    p.StartInfo.FileName = ConfigurationManager.AppSettings["HtmlToPdfExePath"];

    string switches = "--print-media-type ";
    switches += "--margin-top 4mm --margin-bottom 4mm --margin-right 0mm --margin-left 0mm ";
    switches += "--page-size A4 ";
    switches += "--no-background ";
    switches += "--redirect-delay 100";

    p.StartInfo.Arguments = switches + string.Format(" --\"{0}\" --", Url) + " " + filename;

    p.StartInfo.UseShellExecute = false; // needs to be false in order to redirect output
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.RedirectStandardInput = true; // redirect all 3, as it should be all 3 or none
    p.StartInfo.WorkingDirectory = StripFilenameFromFullPath(p.StartInfo.FileName);

    p.Start();

    string output = p.StandardOutput.ReadToEnd();

    p.WaitForExit(60000);

    // read the exit code, close process


    byte[] buffer = p.StandardOutput.CurrentEncoding.GetBytes(output);
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "inline;filename=download.pdf");
    Response.BinaryWrite(buffer);
    Response.End();

    int returnCode = p.ExitCode;
    p.Close();

    return true;
}

我将该函数称为:

WKHtmlToPdf(Request.Url.GetLeftPart(UriPartial.Authority) + "/SavePDF.aspx?PID="
           + Request.QueryString["PID"].ToString(), "MyInvoice");

HTML所在的页面位于:http://localhost:61493/SavePDF.aspx?PID=10

我的输出PDF文件是:MyInvoice

但我得到了:

  

PDF文档可能无法正确显示。

可能是什么问题..?

0 个答案:

没有答案