Thursday 10 November 2022

Make PDF from hyperlink | ASP.NET C#


 This post will help you to create a PDF file from a URL in ASP.Net C#. Follow the steps given below. We will save a PDF file on the server then after viewing the PDF file, the file will be deleted. 

Step 1 : 

Download Select.HtmlToPdf.NetCore package from NuGet. 

Goto Tools Menu>Nuget Package Manager > Manage Nuget Package for Solution

Step 2 : 

 
 Create a Button and call the javascript function, which will create a PDF file from the given URL. The passing argument is URL of the page. Which will be converted into PDF file on the server at the given path. Then you can download the file.

<input type="button" value="Get PDF" onclick="createPDF('https://girfa.co.in/')" />

Step 3 


Javascript Code

function createPDF(pageurl)
 {

         $.ajax({

                url: '/Common/htmltopdf/',
                async: true,
                dataType: "json",
                type: "GET",
                contentType: 'application/json; charset=utf-8',       

                data: { url: pageurl},

                success: function (data) {              

                    DelPDF('/upload/print/' + $.trim(invid) + '.pdf');                   

                    window.location.href = '/upload/print/' + $.trim(invid) + '.pdf';                          

                }

            });          

        }

 

function DelPDF(path) 
{          

            $.ajax({

                url: '/Common/DeleteCommonFile?path=' + path,

                type: "POST",

                contentType: false, // Not to set any content header

                processData: false, // Not to process data           

                 success: function (result) {

                    //Update Extension
                    if (result.sMessage == "1")
                    {
   

                    }
                    else
                        alert(result.sMessage);

                },
                error: function (abc) {
                    alert(abc.statusText); 

                }

            });

}

Step 4

Controller C# code

public JsonResult htmltopdf(string url,string inv)

        {

            // instantiate a html to pdf converter object

            HtmlToPdf converter = new HtmlToPdf(); 

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(url); 

            // save pdf document

            doc.Save(Server.MapPath("~/upload/print/" + inv + ".pdf")); 

            // close pdf document

            doc.Close();

            return Json(new { sMessage = "11"}, JsonRequestBehavior.AllowGet); 

        }

 

[HttpPost]

        public JsonResult DeleteCommonFile(string path)
        {

            try
            {

                string filename = Server.MapPath(path);
                if (System.IO.File.Exists(filename))
                {

                    System.IO.File.Delete(filename);

                } 

                return Json(new { sMessage = "1", JsonRequestBehavior.AllowGet });

            }

            catch (Exception ex)
            {

                return Json(new { sMessage = ex.Message, JsonRequestBehavior.AllowGet });

            }

        }


No comments:

Post a Comment