如何使用wkhtmltopdf将html作为字符串传递?

如何使用asp.net,c#将wtml作为字符串而不是wltmltopdf中的url传递?

在此示例中已重定向STDIn和STDOut,因此您根本不需要文件。

public static class Printer { public const string HtmlToPdfExePath = "wkhtmltopdf.exe"; public static bool GeneratePdf(string commandLocation, StreamReader html, Stream pdf, Size pageSize) { Process p; StreamWriter stdin; ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = Path.Combine(commandLocation, HtmlToPdfExePath); psi.WorkingDirectory = Path.GetDirectoryName(psi.FileName); // run the conversion utility psi.UseShellExecute = false; psi.CreateNoWindow = true; psi.RedirectStandardInput = true; psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; // note: that we tell wkhtmltopdf to be quiet and not run scripts psi.Arguments = "-q -n --disable-smart-shrinking " + (pageSize.IsEmpty? "" : "--page-width " + pageSize.Width + "mm --page-height " + pageSize.Height + "mm") + " - -"; p = Process.Start(psi); try { stdin = p.StandardInput; stdin.AutoFlush = true; stdin.Write(html.ReadToEnd()); stdin.Dispose(); CopyStream(p.StandardOutput.BaseStream, pdf); p.StandardOutput.Close(); pdf.Position = 0; p.WaitForExit(10000); return true; } catch { return false; } finally { p.Dispose(); } } public static void CopyStream(Stream input, Stream output) { byte[] buffer = new byte[32768]; int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, read); } } } 

重定向STDIN可能是完成您要做的事情的最简单方法。

使用wkhtmltopdf(在ASP.Net中)重定向STDIN的一种方法如下:

  private void WritePDF(string HTML) { string inFileName, outFileName, tempPath; Process p; System.IO.StreamWriter stdin; ProcessStartInfo psi = new ProcessStartInfo(); tempPath = Request.PhysicalApplicationPath + "temp\\"; inFileName = Session.SessionID + ".htm"; outFileName = Session.SessionID + ".pdf"; // run the conversion utility psi.UseShellExecute = false; psi.FileName = "c:\\Program Files (x86)\\wkhtmltopdf\\wkhtmltopdf.exe"; psi.CreateNoWindow = true; psi.RedirectStandardInput = true; psi.RedirectStandardOutput = true; psi.RedirectStandardError = true; // note that we tell wkhtmltopdf to be quiet and not run scripts // NOTE: I couldn't figure out a way to get both stdin and stdout redirected so we have to write to a file and then clean up afterwards psi.Arguments = "-q -n - " + tempPath + outFileName; p = Process.Start(psi); try { stdin = p.StandardInput; stdin.AutoFlush = true; stdin.Write(HTML); stdin.Close(); if (p.WaitForExit(15000)) { // NOTE: the application hangs when we use WriteFile (due to the Delete below?); this works Response.BinaryWrite(System.IO.File.ReadAllBytes(tempPath + outFileName)); //Response.WriteFile(tempPath + outFileName); } } finally { p.Close(); p.Dispose(); } // delete the pdf System.IO.File.Delete(tempPath + outFileName); } 

请注意,上面的代码假定您的应用程序目录中有一个临时目录。 此外,您必须为运行IIS进程时使用的用户帐户显式启用对该目录的写访问权限。

我知道这是一个较旧的post,但我希望未来的开发人员有这个选项。 我有同样的需求,并且为了在Web应用程序中获取PDF而必须启动后台进程的想法非常糟糕。

这是另一种选择: https : //github.com/TimothyKhouri/WkHtmlToXDotNet

它是围绕wkhtmltopdf的.NET本机包装器。

示例代码:

 var pdfData = HtmlToXConverter.ConvertToPdf("

COOOL!

");

注意,它现在不是线程安全的 – 我正在努力。 所以只需使用显示器或其他东西或锁。