Code snippet for creating a PDF from an HTML page using abcpdf

11 comments

This function calls an html page, creates a pdf from it, saves it to a month stamped folder 

 

 

public ActionResult PDF() {
	Server.ScriptTimeout = 60 * 5; //5 mins
	return Redirect(Web.Root + BuildPdf_ABCPDF("minutes"));
}

		/// 
		/// note: to add page break, add  around the place
		/// 
		/// file name of pdf created
		public string BuildPdf_ABCPDF()
		{
			//create document
			//String htmlText = "";
			var newFileName = "Invoice-"+this.OrderRef+"-"+Fmt.DateTimeCompressed(DateTime.Now)+".pdf";
			var fileroot=Web.Server.MapPath("~");
			if(fileroot.LastIndexOf('\\')!=fileroot.Length-1)fileroot+="\\";
			var folderPath = fileroot + "attachments\\invoices\\"+Fmt.MonthYear(DateTime.Now)+"\\"; //must end in 			FileSystem.CreateFolder(folderPath);
			var fullServerPath = folderPath + newFileName;
			//var url = Web.BaseUrl+"admin/policy/view/?pdf=abc&encryptedID="+Crypto.EncryptID(this.PolicyID);
			var root = Web.BaseUrlNoSSL;
			if(!Util.ServerIsDev)
			{
				root = Util.GetSetting("WebsiteBaseUrl");
			}
			var url = root+"cart/invoice?pdf=abc&cartID="+Crypto.EncryptID(this.ID)+"";
			
			//Beweb.SendEMail.SimpleSendEmail("jeremy@beweb.co.nz",url,"");
			//var url = "http://www.beweb.co.nz";
			
			try {
				var theDoc = new Doc();
				theDoc.MediaBox.String = "A4"; // sets page to A4 & removes scrollbar appearing

				//clear caching?
				theDoc.HtmlOptions.PageCacheEnabled = false;
				theDoc.HtmlOptions.UseNoCache = true;
				theDoc.HtmlOptions.PageCacheClear();
				theDoc.HtmlOptions.PageCachePurge();
				theDoc.HtmlOptions.UseResync = true;
				
				var id = theDoc.AddImageUrl(url);

				//add more pages if more than 1 page
				for (; theDoc.Chainable(id); ) {
					theDoc.Page = theDoc.AddPage();
					id = theDoc.AddImageToChain(id);
				}

				theDoc.Save(fullServerPath);
				theDoc.Clear();
			}
			catch (Exception ex) {
				throw new BewebException(ex + "\n\n" + url + "\n\n" + fullServerPath);
			}
			//Web.Write(url);
			//Web.End();

			return newFileName;
		} 			


Comments


Leave a Comment