我正在尝试通过单击按钮生成详细 HTML 页面的 pdf,但我无法执行此操作。我不知道如何在 HTMLcontent 标签中给出页面的视图,下面是代码以更好地理解一个问题。
一切正常,但我不知道在字符串 HTMLcontent 行中写什么以获取 pdf 格式的视图。
我是新来的,请帮助我。
下面是代码: 控制器
public ActionResult PDF(int Id)
{
detail.DownloadPDF(Id);
return View();
}
PlayerDetails类:
`public void DownloadPDF(int Id)
{
using (var context = new cricket_db_23Entities1())
{
var result = context.player_details.Where(x => x.id == Id).Select(x => new Player()
{
id = x.id,
player_code = x.player_code,
player_name = x.player_name,
dob = x.dob,
city = x.city,
pincode = x.pincode,
status = x.status,
mobile_no = x.mobile_no
}).FirstOrDefault();
string HTMLContent = "result"; //error is here
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/pdf";
`HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + "PDFfile.pdf");`
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.BinaryWrite(GetPDF(HTMLContent));
HttpContext.Current.Response.End();
}
}
public byte[] GetPDF(string pHTML)
{
byte[] bPDF = null;
MemoryStream ms = new MemoryStream();
TextReader txtReader = new StringReader(pHTML);
// 1: create object of a itextsharp document class
Document doc = new Document(PageSize.A4, 25, 25, 25, 25);
/* 2: we create a itextsharp pdfwriter that listens to the document and directs a XML- stream to a file */
PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);
// 3: we create a worker to parse the document
HTMLWorker htmlWorker = new HTMLWorker(doc);
// 4: we open the document and start the worker on the document
doc.Open();
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;
}`
一切正常,但我不知道在字符串 HTMLcontent 行中写什么以获取 pdf 格式的视图。