0

在 PDF 生成的帮助下存在问题wkhtmltopdf.exe

这是我的代码:

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 文档可能无法正确显示。

可能是什么问题..?

4

0 回答 0