如何解决如何在带有Hangfire的BackgroundJob中使用Rotativa获取ControllerContext来构建PDF
我必须在后台应用程序中发送邮件,为此,我使用了 Hangfire ,但是我必须将文档与收件人一起附加到每封邮件中,要附加的文档信息在我用旋转刀转换的桌子。
我担心的是,当我要将文档转换为byte[]
时,总是得到HttpContext
是null
的信息,这显然是因为没有HTTP请求可以触发操作,而所有操作都是在后台。
这是我的实际代码:
var Avis_views = new Viewaspdf("~/Views/AvisAutomatique/ExportPdfDebit.cshtml",printa)
{
PageMargins = new Rotativa.Options.Margins(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);
有人可以帮助我从控制器初始化httpcontext吗?
解决方法
看起来Rotativa lib中的一个问题是必须获取控制器上下文,github.com / webgio / Rotativa / pull / 88
您将无法将列出的上下文传递给hangfire后台进程。
一种变通方法(如果文件不是太大)可能是您的hangfire进程在您的网站上调用了一个URL,该URL执行电子邮件发送代码,而不是在hangfire后台进程中执行Rotativa和电子邮件发送。
,我是Rotativa的作者,也是Rotativa.io云服务的所有者。
在rotativa的SaaS版本中,您可以使用Nuget库,该库使您可以从剃刀视图构建PDF,而无需引用Asp.net:
https://rotativa.io/site/blog/instructions/2020/07/31/create-pdf-in-net-core-without-aspnet.html
,您可以按照Giogio Bozio的建议检查云服务,或者,如果您打算使用当前的Rotativa DLL,则可以尝试我在评论中建议的解决方案。
您的代码应如下所示(您提供的代码中嵌入了示例解决方案):
var Avis_views = new ViewAsPdf("~/Views/AvisAutomatique/ExportPdfDebit.cshtml",printa)
{
PageMargins = new Rotativa.Options.Margins(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);
// Create an instance of your controller class (in example,I guess you are using mentioned quotesController)
QuotesController controllerInstance = new QuotesController();
// Generate RouteData based in your controller (even if you are not using on in this situation,you can reference an existent one)
RouteData route = new RouteData();
// Simply change "MyAction" and "QuotesController" for the name of real action name and controller,respectively
route.Values.Add("action","MyAction");
route.Values.Add("controller","QuotesController");
// Generate controller context,to use in the next step
ControllerContext newContext = new ControllerContext(new HttpContextWrapper(System.Web.HttpContext.Current),route,controllerInstance);
byte[] abc = Avis_views.BuildFile(newContext); // Here you send the generated controller context
//System.IO.File.WriteAllBytes(path,abc);
MemoryStream ms = new MemoryStream(abc);
sendemail(email,fileName,abc,emailc);
请检查它是否对您有用,并且不要忘记添加必需的参考文献:
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
,
经过一周的轮转尝试,我用Itextsharp bellow资助了另一个解决方案:
1首先全部安装软件包ItextSharp和ItextSharp XMLWorker
2-我使用css在html页面中设计了模板,然后将它们放在
文件夹中(@“ C:\ Templates \ Credit.html”; @“ C:\ Templates \ pdfview.css”;)
3-使用此方法阅读html和CSS
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;
}
}
并使用此方法转换为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;
}
谢谢您希望能对某人有所帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。