本文介绍了使用wkhtmltopdf从html创建pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i来自html的genf pdf。我用wkhtmltopdf来做这个东西。下面是我试过的代码。



Hi,
i am genrating pdf from html.I have used wkhtmltopdf for doing this stuff.Below is the code i have tried.

private void WritePDF(string HTML)
    {
        string inFileName,
                outFileName,
                tempPath;
        Process p;
        System.IO.StreamWriter stdin;
        ProcessStartInfo psi = new ProcessStartInfo();

        tempPath = Request.PhysicalApplicationPath + "ExcelFiles\\";
        inFileName = Session.SessionID + ".htm";
        outFileName = Session.SessionID + ".pdf";

        // run the conversion utility
        psi.UseShellExecute = false;
        psi.FileName = "E:\\wkhtmltopdf";
        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);
    }





我从这里发现这个答案如何使用wkhtmltopdf将html作为字符串传递?



这里我收到错误无法找到文件





I found this answer from here how to pass html as a string using wkhtmltopdf?

Here i get error Could not find file on

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);
                }







我现在可以为此创建文件吗?




how i can create file for this now?

推荐答案


这篇关于使用wkhtmltopdf从html创建pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 22:30