Skip to content

Instantly share code, notes, and snippets.

@rbaty-barr
Last active October 13, 2016 14:53
Show Gist options
  • Save rbaty-barr/f28d4bfca3499bbe34a1529a188a053e to your computer and use it in GitHub Desktop.
Save rbaty-barr/f28d4bfca3499bbe34a1529a188a053e to your computer and use it in GitHub Desktop.
using System;
using System.Net;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using segalCollateral.Models;
using Umbraco.Web.Mvc;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.Models;
using Umbraco.Web;
using EvoPdf;
using System.Web.UI;
namespace segalCollateral.Controllers.SurfaceControllers
{
public class PdfSurfaceController : SurfaceController
{
//Used with an ActionLink
//@Html.ActionLink("Print Page", "ConvertHtmlToPdf", "PdfSurface", new {printBaseUrl = "http://mygtlawbenefits.com", printUrl = CurrentPage.Id, theFileName = "Greenberg Traurig 2015 Benefits Brochure", altTemplate = "templateAlias", headerTemplate = "templateAlias", headerH = 60}, null)
public ActionResult ConvertHtmlToPdf(FormCollection collection, Pdfmodel m, string printBaseUrl, int printUrl, string theFileName, string altTemplate, string headerTemplate, int headerH)
{
if (!ModelState.IsValid)
return CurrentUmbracoPage();
var theNode = Umbraco.TypedContent(printUrl);
string nodeName = theFileName;
string url = umbraco.library.NiceUrlWithDomain(printUrl) + "/" + altTemplate;
string headerHtmlUrl = Umbraco.UrlAbsolute(printUrl) + "/" + headerTemplate;
string coverPage = Server.MapPath("~/PDF/2015Cover.pdf");
string baseUrl = printBaseUrl;
// HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
//myHttpWebRequest.Referer = printBaseUrl;
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.Letter;
if (altTemplate == "EntireGuidePDF")
{
// Set the PDF file to be inserted before conversion result
htmlToPdfConverter.PdfDocumentOptions.AddStartDocument(coverPage);
string htmlPageNums = "<table style='width: 90%; margin: 0 auto; border: 0px;'><tr><td style='font-size: 12px; font-family: Arial, sans-serif;'>PDF File generated:" + DateTime.Now + "</td><td style='font-size: 12px; font-family: Arial, sans-serif; text-align: right;'>&p;</td></tr></table>";
HtmlToPdfVariableElement footerHtmlWithPageNumbers = new HtmlToPdfVariableElement(htmlPageNums, baseUrl);
htmlToPdfConverter.PdfDocumentOptions.TopMargin = 30;
htmlToPdfConverter.PdfDocumentOptions.BottomMargin = 30;
htmlToPdfConverter.PdfDocumentOptions.ShowFooter = true;
//string footerHtmlUrl = baseUrl + "/pdfFooter";
htmlToPdfConverter.PdfFooterOptions.FooterHeight = 20;
htmlToPdfConverter.PdfFooterOptions.AddElement(footerHtmlWithPageNumbers);
htmlToPdfConverter.PdfBookmarkOptions.AutoBookmarksEnabled = true;
htmlToPdfConverter.PdfViewerPreferences.PageMode = ViewerPageMode.UseOutlines;
}
htmlToPdfConverter.PdfDocumentOptions.ShowHeader = true;
//htmlToPdfConverter.PdfBookmarkOptions.HtmlElementSelectors = new string[] { "H1", "H2" };
// htmlToPdfConverter.PdfViewerPreferences.PageMode = ViewerPageMode.UseOutlines;
if (headerTemplate != "")
{
// Set the header height in points
htmlToPdfConverter.PdfHeaderOptions.HeaderHeight = headerH;
// Create a HTML element to be added in header
HtmlToPdfElement headerHtml = new HtmlToPdfElement(headerHtmlUrl);
// Set the HTML element to fit the container height
headerHtml.FitHeight = true;
// Add HTML element to header
htmlToPdfConverter.PdfHeaderOptions.AddElement(headerHtml);
}
// add the Forms Authentication cookie to request
if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
{
htmlToPdfConverter.HttpRequestCookies.Add(FormsAuthentication.FormsCookieName,
Request.Cookies[FormsAuthentication.FormsCookieName].Value);
}
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToPdfConverter.LicenseKey = "";
// The buffer to receive the generated PDF document
byte[] outPdfBuffer = null;
// Convert the HTML page given by an URL to a PDF document in a memory buffer
outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
fileResult.FileDownloadName = nodeName + ".pdf";
return (fileResult);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment