Question

I am converting a project from .NET framework 1.1 to 4.0. The project has some functionality to generate PDF using the below code.

public class GeneratePDF
{       

    public GeneratePDF(){}

    public string ShowPDF(string url, string pdfFile, string exeFile) 
    {
        try
        {
            Process p = new Process();
            p.StartInfo.FileName = exeFile;
            string args = " --webpage -f " + pdfFile + " " + url;
            p.StartInfo.Arguments =  args;              
            p.Start(); 
            p.WaitForExit();
            return "1";
        }
        catch(Exception ex)
        {
            return (ex.ToString()); 
        }
    }
}

and on page the code is as below:

if(!System.IO.Directory.Exists(Request.PhysicalApplicationPath + "pdf"))
                    {
                        System.IO.Directory.CreateDirectory(Request.PhysicalApplicationPath + "pdf");
                    }
                    string response;
                    url = "http://localhost/abc/PDFSpeechDetails.aspx?SpeechNumber=" + speechNumber +"&SpeechType=S";
                    pdfFile = Request.PhysicalApplicationPath + "pdf\\" + speechNumber + ".pdf";
                    exeFile = Request.PhysicalApplicationPath + "html2pdf\\ghtmldoc.exe";
                    GeneratePDF gPDF = new GeneratePDF();
                    response = gPDF.ShowPDF(url, pdfFile, exeFile); 
                    gPDF = null;
                    if(response == "1")
                    {
                        email.sendMailWithAttachments(txtRecEmail.Text,"","",txtSenEmail.Text,txtSenName.Text,txtSubject.Text,txtMessage.Text,Request.PhysicalApplicationPath + "pdf\\" + speechNumber + ".pdf", "speech.pdf");
                        Response.Redirect("EmailThanks.aspx?Email="+txtRecEmail.Text);
                    }
                    else 
                    {
                        throw new Exception(response); 
                    }

One thing more the project has "html2pdf" folder in solution explorer, which has an exe file ghtmldoc.exe file to which the code is pointing. in response i always get 1 as out put, but the PDF is not generated.

My question is: can we generate the PDF using this code if yes then why the code is not working for me as it also not working in the old project (1.1).

please help me

Was it helpful?

Solution

Ok, first of all download the latest version of the HTMLDoc Software from this link. now extract the files and install the software on your Computer. it will ask "libeay32.dll file is missing. add this file from the folder you have in solution explorer. Now go to the folder which is being created by it after installation. copy all the files and folders and pasted it the folder you have in your project.

Now change your code as below:

if (!System.IO.Directory.Exists(Request.PhysicalApplicationPath + "pdf"))
                    {
                        System.IO.Directory.CreateDirectory(Request.PhysicalApplicationPath + "pdf");
                    }
                    string response;
                    url = "http://google.com";
                    pdfFile = Request.PhysicalApplicationPath + "pdf\\" + speechNumber + ".pdf";
                    //pdfFile = @"D:\\test.pdf";
                    exeFile = Request.PhysicalApplicationPath + "html2pdf\\htmldoc.exe";
                    GeneratePDF gPDF = new GeneratePDF();
                    response = gPDF.ShowPDF(url, pdfFile, exeFile);
                    gPDF = null;
                    if (response == "1")
                    {
                        email.sendMailWithAttachments(txtRecEmail.Text, "", "", txtSenEmail.Text, txtSenName.Text, txtSubject.Text, txtMessage.Text, Request.PhysicalApplicationPath + "pdf\\" + speechNumber + ".pdf", "speech.pdf");
                        Response.Redirect("EmailThanks.aspx?Email=" + txtRecEmail.Text);
                    }
                    else
                    {
                        throw new Exception(response);
                    }

That's All. Hope it will help you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top