在我的 ASP.NET Core MVC 项目中,我想以PDF格式输出页面中包含的一些数据。我尝试了几个库,但由于某种原因它不起作用。有谁能帮助我吗?如果您支持直接工作的示例或项目,我将非常高兴。
我尝试的最后一种方法是使用 iText 库。这有效,但空白页来了。
.cshtml使用 jQuery 库:
<button id="btnPdf">Generate PDF</button>
$('#btnPdf').click(function () {
window.open('../Home/PrintStudent?param=1');
});
家庭控制器:
public class HomeController : Controller
{
private readonly IWebHostEnvironment _oHostEnvironment;
[Obsolete]
private IHostingEnvironment _env;
private string projectRootPath;
private string outputPath;
private string storagePath;
List<string> lstFiles;
[Obsolete]
public HomeController(IWebHostEnvironment oHostEnvironment, IHostingEnvironment env)
{
_env = env;
_oHostEnvironment = oHostEnvironment;
projectRootPath = _env.ContentRootPath;
outputPath = Path.Combine(projectRootPath, "wwwroot/Uploads/viewer");
storagePath = Path.Combine(projectRootPath, "wwwroot/Uploads");
}
[Route("PrintStudent")]
public ActionResult PrintStudent(int param)
{
List<PdfModel> oStudents = new List<PdfModel>();
for (int i = 1; i < 10; i++)
{
PdfModel oStudent = new PdfModel();
oStudent.Id = i;
oStudent.Name = "Student" + i;
oStudent.Address = "Address" + i;
oStudents.Add(oStudent);
}
StudentReport rpt = new StudentReport(_oHostEnvironment);
return File(rpt.Report(oStudents), "application/pdf");
}
}
学生报告.cs
using Helpers.CommonModels;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.AspNetCore.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace DenizAnalizMain.Reports
{
public class StudentReport
{
private readonly IWebHostEnvironment _oHostEnvironment;
public StudentReport(IWebHostEnvironment oHostEnvironment)
{
_oHostEnvironment = oHostEnvironment;
}
int _maxColumn = 3;
Document _document;
Font _fontStyle;
PdfPCell _pdfCell;
PdfPTable _pdfTable = new PdfPTable(3);
MemoryStream _memoryStream = new MemoryStream();
List<PdfModel> _oStudents = new List<PdfModel>();
public byte[] Report(List<PdfModel> oStudents)
{
_oStudents = oStudents;
_document = new Document();
_document.SetPageSize(PageSize.A4);
_document.SetMargins(5f, 5f, 20f, 5f);
_pdfTable.WidthPercentage = 100;
_pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
_fontStyle = FontFactory.GetFont("Tahoma", 8f, 1);
PdfWriter docWrite = PdfWriter.GetInstance(_document, _memoryStream);
_document.Open();
float[] sizes = new float[_maxColumn];
for (int i = 0; i < _maxColumn; i++)
{
if (i == 0) sizes[i] = 20;
else sizes[i] = 100;
}
_pdfTable.SetWidths(sizes);
this.ReportHeader();
this.EmptyRow(2);
this.ReportBody();
_pdfTable.HeaderRows = 2;
_document.Add(_pdfTable);
_document.Close();
return _memoryStream.ToArray();
}
private void ReportHeader()
{
_pdfCell = new PdfPCell(this.AddLogo());
_pdfCell.Colspan = 1;
_pdfCell.Border = 0;
_pdfTable.AddCell(_pdfCell);
_pdfCell = new PdfPCell(this.SetPageTitle());
_pdfCell.Colspan = _maxColumn -1;
_pdfCell.Border = 0;
_pdfTable.AddCell(_pdfCell);
_pdfTable.CompleteRow();
}
private PdfPTable AddLogo()
{
int maxColumn = 1;
PdfPTable pdfPTable = new PdfPTable(maxColumn);
string path = _oHostEnvironment.WebRootPath + "/images";
string imgCombine = Path.Combine(path, "denizyatirimTransparent.png");
Image img = Image.GetInstance(imgCombine);
_pdfCell = new PdfPCell(img);
_pdfCell.Colspan = maxColumn;
_pdfCell.HorizontalAlignment = Element.ALIGN_LEFT;
_pdfCell.Border = 0;
_pdfCell.ExtraParagraphSpace = 0;
pdfPTable.AddCell(_pdfCell);
pdfPTable.CompleteRow();
return pdfPTable;
}
private PdfPTable SetPageTitle()
{
int maxColumn = 3;
PdfPTable pdfPTable = new PdfPTable(maxColumn);
_fontStyle = FontFactory.GetFont("Tahoma", 18f, 1);
_pdfCell = new PdfPCell(new Phrase("Student Information", _fontStyle));
_pdfCell.Colspan = maxColumn;
_pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
_pdfCell.Border = 0;
_pdfCell.ExtraParagraphSpace = 0;
pdfPTable.AddCell(_pdfCell);
pdfPTable.CompleteRow();
_fontStyle = FontFactory.GetFont("Tahoma", 14f, 1);
_pdfCell = new PdfPCell(new Phrase("School Name", _fontStyle));
_pdfCell.Colspan = maxColumn;
_pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
_pdfCell.Border = 0;
_pdfCell.ExtraParagraphSpace = 0;
pdfPTable.AddCell(_pdfCell);
pdfPTable.CompleteRow();
return pdfPTable;
}
private void EmptyRow(int nCount)
{
for (int i = 1; i < nCount; i++)
{
_pdfCell = new PdfPCell(new Phrase("", _fontStyle));
_pdfCell.Colspan = _maxColumn;
_pdfCell.Border = 0;
_pdfCell.ExtraParagraphSpace = 10;
_pdfTable.AddCell(_pdfCell);
_pdfTable.CompleteRow();
}
}
private void ReportBody()
{
var fontStyleBold = FontFactory.GetFont("Tahoma", 9f, 1);
_fontStyle = FontFactory.GetFont("Tahoma", 9f, 0);
#region Detail Table Header
_pdfCell = new PdfPCell(new Phrase("Id", fontStyleBold));
_pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
_pdfCell.VerticalAlignment = Element.ALIGN_MIDDLE;
_pdfCell.BackgroundColor = BaseColor.Gray;
_pdfTable.AddCell(_pdfCell);
_pdfCell = new PdfPCell(new Phrase("Name", fontStyleBold));
_pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
_pdfCell.VerticalAlignment = Element.ALIGN_MIDDLE;
_pdfCell.BackgroundColor = BaseColor.Gray;
_pdfTable.AddCell(_pdfCell);
_pdfCell = new PdfPCell(new Phrase("Address", fontStyleBold));
_pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
_pdfCell.VerticalAlignment = Element.ALIGN_MIDDLE;
_pdfCell.BackgroundColor = BaseColor.Gray;
_pdfTable.AddCell(_pdfCell);
_pdfTable.CompleteRow();
#endregion
#region Detail table body
foreach (var oStudent in _oStudents)
{
_pdfCell = new PdfPCell(new Phrase(oStudent.Id.ToString(), fontStyleBold));
_pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
_pdfCell.VerticalAlignment = Element.ALIGN_MIDDLE;
_pdfCell.BackgroundColor = BaseColor.Gray;
_pdfTable.AddCell(_pdfCell);
_pdfCell = new PdfPCell(new Phrase(oStudent.Name.ToString(), fontStyleBold));
_pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
_pdfCell.VerticalAlignment = Element.ALIGN_MIDDLE;
_pdfCell.BackgroundColor = BaseColor.Gray;
_pdfTable.AddCell(_pdfCell);
_pdfCell = new PdfPCell(new Phrase(oStudent.Address.ToString(), fontStyleBold));
_pdfCell.HorizontalAlignment = Element.ALIGN_CENTER;
_pdfCell.VerticalAlignment = Element.ALIGN_MIDDLE;
_pdfCell.BackgroundColor = BaseColor.Gray;
_pdfTable.AddCell(_pdfCell);
_pdfTable.CompleteRow();
}
#endregion
}
}
}