代码之家  ›  专栏  ›  技术社区  ›  David Murdoch

如何在ASP.net[duplicate]中使用wkhtmltopdf.exe

  •  37
  • David Murdoch  · 技术社区  · 16 年前

    wkhtmltopdf 听起来是一个很好的解决方案…问题是我不能从asp.net执行具有足够权限的进程,所以。。。

    Process.Start("wkhtmltopdf.exe","http://www.google.com google.pdf");
    

    有没有一个简单的方法:

    -a) 允许asp.net启动进程(实际上可以执行某些操作)或
    -b) 将wkhtmltopdf.exe编译/包装成我可以从C中使用的东西,如下所示: WkHtmlToPdf.Save("http://www.google.com", "google.pdf");

    5 回复  |  直到 16 年前
        1
  •  25
  •   Răzvan Flavius Panda    13 年前

    你也可以用 Pechkin

    用于WkHtmlToPdf DLL的.NET包装器,该库使用Webkit引擎 将HTML页转换为PDF。

    Nuget包:

    Pechkin.Synchronized

    Pechkin

        2
  •  23
  •   Pablo Ruiz García    15 年前

    我刚刚开始了一个新项目,提供了一个围绕wkhtmltopdf的C#P/Invoke包装器。

    您可以在以下网址查看我的代码: https://github.com/pruiz/WkHtmlToXSharp

    问候。

        3
  •  17
  •   Community Mohan Dere    9 年前

    感谢 Paul wrapper 由Codaxy编写,也可以通过 NuGet .

    经过几次尝试后,我管理了这个MVC操作,它可以立即创建PDF文件并以流的形式返回:

    public ActionResult Pdf(string url, string filename)
    {
        MemoryStream memory = new MemoryStream();
        PdfDocument document = new PdfDocument() { Url = url };
        PdfOutput output = new PdfOutput() { OutputStream = memory };
    
        PdfConvert.ConvertHtmlToPdf(document, output);
        memory.Position = 0;
    
        return File(memory, "application/pdf", Server.UrlEncode(filename));
    }
    

    最后,我们必须将流作为FileStreamResult推送。

    不要忘记将输出流的位置设置为零,否则您将看到下载的PDF文件大小为零字节。

        4
  •  4
  •   David Murdoch    16 年前

    这是我实际使用的代码。请随时编辑这个摆脱一些气味和其他可怕的…我知道它不是那么好。

    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Web;
    using System.Web.UI;
    
    public partial class utilities_getPDF : Page
    {
        protected void Page_Load(Object sender, EventArgs e)
        {
            string fileName = WKHtmlToPdf(myURL);
    
            if (!string.IsNullOrEmpty(fileName))
            {
                string file = Server.MapPath("~\\utilities\\GeneratedPDFs\\" + fileName);
                if (File.Exists(file))
                {
                    var openFile = File.OpenRead(file);
                    // copy the stream (thanks to http://stackoverflow.com/questions/230128/best-way-to-copy-between-two-stream-instances-c)
                    byte[] buffer = new byte[32768];
                    while (true)
                    {
                        int read = openFile.Read(buffer, 0, buffer.Length);
                        if (read <= 0)
                        {
                            break;
                        }
                        Response.OutputStream.Write(buffer, 0, read);
                    }
                    openFile.Close();
                    openFile.Dispose();
    
                    File.Delete(file);
                }
            }
        }
    
        public string WKHtmlToPdf(string Url)
        {
            var p = new Process();
    
            string switches = "";
            switches += "--print-media-type ";
            switches += "--margin-top 10mm --margin-bottom 10mm --margin-right 10mm --margin-left 10mm ";
            switches += "--page-size Letter ";
            // waits for a javascript redirect it there is one
            switches += "--redirect-delay 100";
    
            // Utils.GenerateGloballyUniuqueFileName takes the extension from
            // basically returns a filename and prepends a GUID to it (and checks for some other stuff too)
            string fileName = Utils.GenerateGloballyUniqueFileName("pdf.pdf");
    
            var startInfo = new ProcessStartInfo
                            {
                                FileName = Server.MapPath("~\\utilities\\PDF\\wkhtmltopdf.exe"),
                                Arguments = switches + " " + Url + " \"" +
                                            "../GeneratedPDFs/" + fileName
                                            + "\"",
                                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 = Server.MapPath("~\\utilities\\PDF")
                            };
            p.StartInfo = startInfo;
            p.Start();
    
            // doesn't work correctly...
            // read the output here...
            // string output = p.StandardOutput.ReadToEnd();
    
            //  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, it worked
            return (returnCode == 0) ? fileName : null;
        }
    }
    
        5
  •  0
  •   alwin    9 年前

    我不能评论,所以我把这个作为对上述答案评论的“回答” How to use wkhtmltopdf.exe in ASP.net

    如果 --redirect-delay 不行,试试看 --javascript-delay https://github.com/antialize/wkhtmltopdf/blob/master/README_WKHTMLTOPDF

    或者做 wkhtmltopdf -H 扩展帮助(与上面链接的输出相同)。