C# 如何使用ASP.NET核心MVC生成PDF文档#
我正在做最后一年的项目,C#MVC的概念对我来说仍然是新概念。我想直接从应用程序生成PDF。我正在使用SelectPDF。在我看来,我有一个按钮,用于生成PDF。当点击生成PDF时,问题是它是空白的,没有内容。这是控制器中的代码C# 如何使用ASP.NET核心MVC生成PDF文档#,c#,asp.net-core-mvc,C#,Asp.net Core Mvc,我正在做最后一年的项目,C#MVC的概念对我来说仍然是新概念。我想直接从应用程序生成PDF。我正在使用SelectPDF。在我看来,我有一个按钮,用于生成PDF。当点击生成PDF时,问题是它是空白的,没有内容。这是控制器中的代码 public ActionResult GeneratePdf( string html) { html = html.Replace("StrTag","<").Replace(&q
public ActionResult GeneratePdf( string html)
{
html = html.Replace("StrTag","<").Replace("EndTag",">");
HtmlToPdf oHtmlToPdf = new HtmlToPdf();
PdfDocument oPdfDocument = oHtmlToPdf.ConvertHtmlString(html);
byte[] pdf = oPdfDocument.Save();
oPdfDocument.Close();
return File(pdf,"application/pdf","Financial Report.pdf");
}
using System.IO;
using System.Reflection.Metadata;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = Element.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
public FileResult Export()
{
string html = "StrTag Messgae EndTag";
string ExportData = "This is first pdf generat";
using (MemoryStream stream = new System.IO.MemoryStream())
{
StringReader reader = new StringReader(ExportData);
Document PdfFile = new iTextSharp.text.Document(PageSize.A3);
PdfWriter writer = PdfWriter.GetInstance(PdfFile, stream);
PdfFile.Open();
PdfPCell cell = null;
PdfPTable table = null;
Phrase phrase2 = null;
Phrase phrase1 = null;
phrase2 = new Phrase();
var titleFont = FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD);
table = new PdfPTable(1);
table.TotalWidth = 800f;
table.LockedWidth = true;
table.SpacingBefore = 10;
table.SpacingAfter = 10;
table.HorizontalAlignment = Element.ALIGN_LEFT;
PdfFile.Add(table);
phrase1 = new Phrase();
html = html.Replace("StrTag", "<").Replace("EndTag", ">");
phrase1.Add(new Chunk(html, FontFactory.GetFont("Arial", 14, Font.BOLD)));
cell = PhraseCell(phrase1, PdfPCell.ALIGN_CENTER);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
PdfFile.Add(table);
PdfFile.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, PdfFile, reader);
PdfFile.Close(); return File(stream.ToArray(), "application/pdf", "Financial Report.pdf");
}
}
public ActionResult GeneratePdf(字符串html)
{
html=html.Replace(“StrTag”,”);
HtmlToPdf oHtmlToPdf=新的HtmlToPdf();
PdfDocument oPdfDocument=oHtmlToPdf.ConvertHtmlString(html);
字节[]pdf=optdfdocument.Save();
oPdfDocument.Close();
返回文件(pdf,“application/pdf”、“Financial Report.pdf”);
}
下面是视图中的javascript代码
<script type="text/javascript">
$("#PDF").click(function () {
var html = $("#PdfContainer").html();
html = html.replace(/</g, "StrTag").replace(/>/g, "EndTag");
window.open('../Account/GeneratePdf?html=' + html, '_blank');
});
</script>
$(“#PDF”)。单击(函数(){
var html=$(“#PdfContainer”).html();
html=html.replace(//g,“EndTag”);
open(“../Account/GeneratePdf?html=”+html,“\u blank”);
});
如果有任何建议,我愿意接受。基本上,我想要的就是能够直接从应用程序生成PDF。在查看页面中使用下面的代码
<a href="@Url.Action("Export", "ControllerName")" style="border: 1px solid #e7eaec; padding: 5px;" class="pull-right">Download PDF</a>