Click here to Skip to main content
15,846,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
    }


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

Here i get error Could not find file on

C#
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?
Posted
Comments
Vasudevan Deepak Kumar 20-Dec-13 8:44am    
Check out this document http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html.

You may need to create a temp HTML file and pass it to the tool to convert it and then read the file and pass bytes to Response.BinaryWrite.

1 solution

Did you figure this out? I am also running into same problem. let me know if you find any
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900