Asp.net mvc 4 使用iTextSharp将动态cshtml转换为pdf?
我正在使用html转换为pdf。我的全部目标是将收据以pdf格式发送给客户emailid。它在本地运行良好,但部署在Asp.net mvc 4 使用iTextSharp将动态cshtml转换为pdf?,asp.net-mvc-4,pdf-generation,itextsharp,Asp.net Mvc 4,Pdf Generation,Itextsharp,我正在使用html转换为pdf。我的全部目标是将收据以pdf格式发送给客户emailid。它在本地运行良好,但部署在GoDaddy服务器中时不受支持。因此,我计划使用iTextSharp生成pdf(还邀请观众发表意见) 为在Rotativa中生成pdf而编写的代码: public ActionResult GetPdfReceipt(int RegId) { var actionPDF = new Rotativa.ActionAsPdf("GetPdfReceipt", new {
GoDaddy服务器中时不受支持。因此,我计划使用iTextSharp
生成pdf(还邀请观众发表意见)
为在Rotativa中生成pdf而编写的代码:
public ActionResult GetPdfReceipt(int RegId)
{
var actionPDF = new Rotativa.ActionAsPdf("GetPdfReceipt", new { RegId = regId })
{
FileName = "Receipt.pdf"
};
//Dynamic student receipt pdf
var byteArrayDynamic = actionPDF.BuildPdf(ControllerContext);
}
public ActionResult GetPdfReceipt(int RegId)
{
try
{
var _mdlReceiptPdf = new ReceiptPdfVM
{
...
...
};
return View("Receipts", _mdlReceiptPdf);
}
catch (Exception ex)
{
return View("");
}
}
我已经阅读了一些使用iTextSharp生成pdf的代码,如下所示
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=LoginReportPerDay.pdf");
StringWriter sWriter = new StringWriter();
HtmlTextWriter hTWriter = new HtmlTextWriter(sWriter);
GridView1.RenderControl(hTWriter);
StringReader sReader = new StringReader(sWriter.ToString());
Document pdf = new Document(PageSize.A4);
HTMLWorker worker = new HTMLWorker(pdf);
PdfWriter.GetInstance(pdf, HttpContext.Current.Response.OutputStream);
pdf.Open();
worker.Parse(sReader);
pdf.Close();
HttpContext.Current.Response.Write(pdf);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
如何使用上面的代码呈现动态生成的收据视图pdf数组字节。是您的问题,“如何将pdf作为字节数组获取,而不是直接写入响应
对象的流”?如果是这样,则不要使用HttpContext.Current.Response.OutputStream
而是使用System.IO.MemoryStream
并在完成后调用ToArray()
。有关为什么不再使用HTMLWorker
的示例和说明,请参阅。感谢您的回复。我的问题是,我无法将示例\u html
的值设置为前面提到的静态字符串或路径。它是在运行时动态创建的。因此,如何将动态生成的视图
解析为字符串
。这有帮助吗?您的问题是,“如何将PDF作为字节数组获取,而不是直接写入Response
对象的流”?如果是这样,则不要使用HttpContext.Current.Response.OutputStream
而是使用System.IO.MemoryStream
并在完成后调用ToArray()
。有关为什么不再使用HTMLWorker
的示例和说明,请参阅。感谢您的回复。我的问题是,我无法将示例\u html
的值设置为前面提到的静态字符串或路径。它是在运行时动态创建的。因此,如何将动态生成的视图
解析为字符串
。这有帮助吗?