Skip to content

Instantly share code, notes, and snippets.

@jeremy-farrance
Created November 17, 2021 19:30
Show Gist options
  • Save jeremy-farrance/49e9cf4c2ac5b8a3c5de720cc8502a34 to your computer and use it in GitHub Desktop.
Save jeremy-farrance/49e9cf4c2ac5b8a3c5de720cc8502a34 to your computer and use it in GitHub Desktop.
Sample RazorHost C# to convert Dnn Page to PDF
@using System;
@using System.Collections.Generic;
@using System.Linq;
@using System.Web;
@using System.Web.UI;
@using System.Web.UI.WebControls;
@using DotNetNuke.Common
@* the following is a DLL added to the /bin folder, see http://selectpdf.com/community-edition/ *@
@using SelectPdf;
@* @inherits DotNetNuke.Web.Razor.DotNetNukeWebPage<dynamic> *@
@{
string productQS = Request.QueryString["slug"];
string techsheetQS = Request.QueryString["techsheet"];
string pdfAction = Request.QueryString["pdf"];
// IPs that can see the demo/experiments
// in order; 309SN, client's office, staff1, localhost, and this server's WAN IP
string[] safeIPs = { "192.241.63.162", "127.0.0.1", "204.9.79.162" };
}
@if (!String.IsNullOrEmpty(techsheetQS) && techsheetQS == "1")
{
string url = String.Format("https://abbv.accuraty.co/Shop/Product/{0}/techsheet/1", productQS);
@* we don't instantiate and convert/create the PDF unless /pdf/something is in the URL *@
if (pdfAction == "download" || pdfAction == "view" || pdfAction == "save")
{
// from here we started with code from this sample, http://selectpdf.com/html-to-pdf/demo/convert-url-to-pdf.aspx
// read parameters from the webpage
string savePath = Server.MapPath("~/Portals/0/PDF/TechSheets/");
string saveFilename = String.Format("TechSheet_{0}.pdf", productQS);
// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();
// set converter options
converter.Options.CssMediaType = (HtmlToPdfCssMediaType)Enum.Parse(typeof(HtmlToPdfCssMediaType), "Print", true);
converter.Options.PdfPageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), "Letter", true);
converter.Options.PdfPageOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), "Portrait", true);
converter.Options.WebPageWidth = 820;
converter.Options.WebPageHeight = 0;
// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);
// get conversion result (contains document info from the web page)
HtmlToPdfResult result = converter.ConversionResult;
// set the document properties
doc.DocumentInformation.Title = result.WebPageInformation.Title;
doc.DocumentInformation.Author = "[ client or username ]";
doc.DocumentInformation.CreationDate = DateTime.Now;
switch (pdfAction)
{
case "download":
doc.Save(HttpContext.Current.Response, false, saveFilename);
// thread aborted error, WHY
break;
case "view":
doc.Save(HttpContext.Current.Response, true, saveFilename);
// thread aborted error, WHY
break;
// THIS ONE DOES NOT WORK
case "save": // save pdf document
doc.Save(savePath + saveFilename); // .Save(Response, false, "Sample.pdf");
break;
default:
// do nothing, shouldn't happen
break;
}
// close pdf document
doc.Close();
}
else
{
<p class="no-print text-right" style="margin-bottom: 0;">
<span class="btn btn-primary" onClick="window.print();">Print Page</span>
<a class="btn btn-primary" href="@String.Format("{0}/pdf/view", url)">View PDF</a>
<a class="btn btn-primary" href="@String.Format("{0}/pdf/download", url)">Download PDF</a>
@*<a class="btn btn-primary" href="@String.Format("{0}/pdf/save", url)">save (doesn't work)</a>*@
</p>
<style type="text/css">
.no-print .btn {
margin: 0 5px 0 0;
padding: 6px 14px;
font-size: 16px;
}
/* override "visited" color on buttons */
.btn-primary.active, .btn-primary.focus, .btn-primary:active, .btn-primary:focus, .btn-primary:hover {
background-color: #960208 !important;
}
</style>
}
}
@functions {
//private static string ParseTLDFromHost(string fullHost) // expecting Uri.Host to be passed in !!
//{
// string[] splitHost = fullHost.Split('.');
// return splitHost[splitHost.Count() - 1];
//}
private bool IsAdminOrHost(DotNetNuke.Entities.Users.UserInfo userInfo)
{
return userInfo.IsSuperUser || userInfo.IsInRole(DotNetNuke.Entities.Portals.PortalSettings.Current.AdministratorRoleName);
}
private static string ValueOut(string name, string value, string tag)
{
return String.Format("<{0}>{1} = {2}</{0}>", tag, name, value);
}
private static string LinkOut(string tag, string text, string link)
{
return String.Format("<{0}><a href=\"{1}\">{2}</a></{0}>", tag, link, text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment