我使用wkhtmltopdf.exe使用下面的代碼生成PDF。在c#中使用wkhtmltopdf.exe生成PDF
string url = HttpContext.Current.Request.Url.AbsoluteUri;
//string[] strarry = sPath.Split('/');
//int lengh = strarry.Length;
var pdfUrl = HtmlToPdf(pdfOutputLocation: "~/PDF/", outputFilenamePrefix: "DT", urls: new string[] { url });
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
Response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + pdfUrl.ToString().Substring(6) + "\"");
byte[] data = req.DownloadData(Server.MapPath(pdfUrl.ToString()));
response.BinaryWrite(data);
File.Delete(Server.MapPath(pdfUrl.ToString()));
response.End();
和
public static string HtmlToPdf(string pdfOutputLocation, string outputFilenamePrefix, string[] urls,
string[] options = null,
string pdfHtmlToPdfExePath = "C:\\Program Files\\wkhtmltopdf\\wkhtmltopdf.exe")
{
string urlsSeparatedBySpaces = string.Empty;
try
{
//Determine inputs
if ((urls == null) || (urls.Length == 0))
throw new Exception("No input URLs provided for HtmlToPdf");
else
urlsSeparatedBySpaces = String.Join(" ", urls); //Concatenate URLs
string outputFolder = pdfOutputLocation;
string outputFilename = outputFilenamePrefix + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fff") + ".PDF"; // assemble destination PDF file name
var p = new System.Diagnostics.Process()
{
StartInfo =
{
FileName = pdfHtmlToPdfExePath,
Arguments = ((options == null) ? "" : String.Join(" ", options)) + " " + urlsSeparatedBySpaces + " " + outputFilename,
UseShellExecute = false, // needs to be false in order to redirect output
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true, // redirect all 3, as it should be all 3 or none
WorkingDirectory = HttpContext.Current.Server.MapPath(outputFolder)
}
};
p.Start();
// read the output here...
var output = p.StandardOutput.ReadToEnd();
var errorOutput = p.StandardError.ReadToEnd();
// ...then wait n milliseconds for exit (as after exit, it can't read the output)
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
// if 0 or 2, it worked so return path of pdf
if ((returnCode == 0) || (returnCode == 2))
return outputFolder + outputFilename;
else
throw new Exception(errorOutput);
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-length", theData.Length.ToString());
//if (Request.QueryString["attachment"] != null)
// Response.AddHeader("content-disposition", "attachment; filename=ExampleSite.pdf");
//else
// Response.AddHeader("content-disposition", "inline; filename=ExampleSite.pdf");
//Response.BinaryWrite(theData);
//HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception exc)
{
throw new Exception("Problem generating PDF from HTML, URLs: " + urlsSeparatedBySpaces + ", outputFilename: " + outputFilenamePrefix, exc);
}
}
從上面的代碼的PDF生成well.but我有兩頁與登錄和註銷User.for例如相同的URL可以說www.xyz/pdf/brason 。同一個URL用於LogIn和LogOut User,但內容將根據用戶登錄或註銷而有所不同。
現在,當我登錄並嘗試使用上述代碼生成PDF時,它總是向我顯示註銷用戶頁面的內容。我不確定我該如何解決此問題。
謝謝... Nenotlep –