I have to send mails in my background application, for this I use Hangfire, but I have to attach a document to each mail with its recipient, the information of the document to be attached is in a table that I convert with rotativa.
My concern is when I want to convert the document into byte[]
I always get that the HttpContext
is null
, obviously because there is no HTTP request which triggers the action while everything is in the background.
Here is my actual code:
var Avis_views = new ViewAsPdf("~/Views/AvisAutomatique/ExportPdfDebit.cshtml", printa)
{
PageMargins = new Rotativa.Options.Margins(0, 0, 0, 0),
MinimumFontSize = 14,
/*ageHeight=60,*/
PageOrientation = Rotativa.Options.Orientation.Portrait,
PageSize = Rotativa.Options.Size.A4,
};
var fileName = "DEBITADVISE_" + Numcpt + "_" + _GetTransDateTime() + ".pdf";
//var path = Path.Combine(@"C:\DocumentPdf\", fileName);
byte[] abc = Avis_views.BuildFile(quotesController.ControllerContext);
//System.IO.File.WriteAllBytes(path, abc);
MemoryStream ms = new MemoryStream(abc);
sendemail(email, fileName, abc, emailc);
Can someone can help me initialize the httpcontext from my controller?
after some week with trying with rotativa i fund another solution with Itextsharp bellow how i did:
1-first all installing the package ItextSharp and ItextSharp XMLWorker
2- i designed my template in html page with the css put them in a
folder(@"C:\Templates\Credit.html"; @"C:\Templates\pdfview.css";)
3-read the html and the css with this methode
public string RenderViewToString(string viewPath)
{
var file = new FileInfo(viewPath);
using (StreamReader streamReader = file.OpenText())
{
string viewLine = "";
var result = string.Empty;
while ((viewLine = streamReader.ReadLine()) != null)
{
result = result + viewLine;
}
return result;
}
}
and use this methode to convert to pdf:
public byte[] GetPDF(string param1,string param2,etc...)
{
byte[] bPDF = null;
try
{
pHTML = pHTML.Replace("{{parameter form the html file}}", param1);
pHTML = pHTML.Replace("{{parameter form the html file}}", param2);
MemoryStream ms = new MemoryStream();
TextReader txtReader = new StringReader(pHTML.ToString());
var image = @"C:\Templates\avis.jpg";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(image);
jpg.SpacingBefore = 10f;
jpg.ScaleToFit(50, 50);
jpg.Alignment =iTextSharp.text.Image.ALIGN_RIGHT;
doc.Open();
doc.Add(jpg);
using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(csstemp)))
{
using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(pHTML)))
{
XMLWorkerHelper.GetInstance().ParseXHtml(oPdfWriter, doc, htmlMemoryStream, cssMemoryStream);
}
}
//htmlWorker.StartDocument();
//// 5: parse the html into the document
//htmlWorker.Parse(txtReader);
//// 6: close the document and the worker
//htmlWorker.EndDocument();
//htmlWorker.Close();
doc.Close();
bPDF = ms.ToArray();
return bPDF;
}
catch (Exception ex)
{
var mess = ex.StackTrace;
var mess2 = ex.Message;
}
return bPDF;
}
Thanks your hope that will help someone