Umbraco Export to Pdf using Aspose.Words

Umbraco ‘Export to Pdf’ macro allow users to export online content into Pdf document using Aspose.Words. This macro makes it very easy and simple to have an offline copy of your favorite online content for sharing and printing etc. in popular Pdf format. It adds a simple ‘Export to Pdf’ button at any desired location on the page and clicking it dynamically exports the content of the page to a Pdf document and then automatically downloads the file to the disk location selected by the user in just couple of seconds.

Export to pdf Umbraco Export to Pdf using Aspose.Pdf

How to install and use Export to Pdf macro?

Please check our blog post for more details http://www.aspose.com/blogs/aspose-products/aspose-words-product-family/archive/2014/01/22/umbraco-export-to-pdf-using-aspose.words.html

Sample Code

Declared Variables

private string OutputFileName
        {
            get
            {
                string name = HttpContext.Current.Request.RawUrl.Substring(HttpContext.Current.Request.RawUrl.LastIndexOf("/"));
                name = name.Replace("/", string.Empty).Replace(".aspx", ".pdf");
                return name;
            }
        }

        private string CurrentPageURL
        {
            get
            {
                string url = Request.Url.Authority + HttpContext.Current.Request.RawUrl.ToString();

                if (Request.ServerVariables["HTTPS"] == "on")
                {
                    url = "https://" + url;
                }
                else
                {
                    url = "http://" + url;
                }

                return url;
            }
        }

        private string BaseURL
        {
            get
            {
                string url = Request.Url.Authority;

                if (Request.ServerVariables["HTTPS"] == "on")
                {
                    url = "https://" + url;
                }
                else
                {
                    url = "http://" + url;
                }

                return url;
            }

Below is the code behind Export to Pdf in web user control:

string html = new WebClient().DownloadString(CurrentPageURL);

            // To make the relative image paths work, base URL must be included in head section
            html = html.Replace("", string.Format("", BaseURL));

            // Check for license and apply if exists
            string licenseFile = Server.MapPath("~/App_Data/Aspose.Words.lic");
            if (File.Exists(licenseFile))
            {
                License license = new License();
                license.SetLicense(licenseFile);
            }

            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(html));
            Document doc = new Document(stream);
            doc.Save(Response, OutputFileName, ContentDisposition.Inline, null);
            Response.End();

Video

Please check the video below to see this macro in action.
http://www.youtube.com/watch?v=EcZZzswqybc

Download

Last edited Feb 13, 2014 at 7:08 AM by asposemarketplace, version 2