英文:
How to generate PDF in NET core using iText7 library
问题
I'm using NET Core 6 and I'm playing with the PDF documents or to be more precise how to create PDF documents using iText7 nuget library but it seems that I'm doing something wrong here since I'm getting this response in swagger:
And simple sample of the code for generating PDF is this:
private static byte[] CreateInvoiceDocument()
{
using var memoryStream = new MemoryStream();
var writer = new PdfWriter(memoryStream);
var pdf = new PdfDocument(writer);
pdf.SetDefaultPageSize(PageSize.A4);
var document = new Document(pdf);
// Header table
var headerTable = new Table(1);
headerTable.SetWidth(UnitValue.CreatePercentValue(100));
var test = new Paragraph("dwawwa");
var headerCell = new Cell().Add(new Paragraph("Invoice"));
headerCell.SetTextAlignment(TextAlignment.CENTER);
headerTable.AddCell(headerCell);
document.Add(headerTable);
// Invoice details table
var detailsTable = new Table(2);
detailsTable.SetWidth(UnitValue.CreatePercentValue(100));
detailsTable.SetMarginTop(20);
detailsTable.AddCell("Invoice Number");
detailsTable.AddCell("12");
detailsTable.AddCell("Date");
detailsTable.AddCell(DateTime.Now.ToString("dd/MM/yyyy"));
// Add more details as needed
document.Add(detailsTable);
// Footer
var footerTable = new Table(1);
footerTable.SetWidth(UnitValue.CreatePercentValue(100));
footerTable.SetMarginTop(20);
var footerCell = new Cell().Add(new Paragraph("Thank you for your business!"));
footerCell.SetTextAlignment(TextAlignment.CENTER);
footerTable.AddCell(footerCell);
document.Add(footerTable);
document.Close();
return memoryStream.ToArray();
}
And this should be API response:
public async Task<IActionResult> GetInvoicePDF()
{
var response = CreateInvoiceDocument();
if (response == null)
return BadRequest("Failed to generate the PDF.");
var stream = new MemoryStream(response);
return new FileStreamResult(stream, "application/pdf");
}
Please, what I'm doing wrong and thank you in advance.
英文:
I'm using NET Core 6 and I'm playing with the PDF documents or to be more precise how to create PDF documents using iText7 nuget library but it seems that I'm doing something wrong here since I'm getting this response in swagger:
And simple sample of the code for generating PDF is this:
private static byte[] CreateInvoiceDocument()
{
using var memoryStream = new MemoryStream();
var writer = new PdfWriter(memoryStream);
var pdf = new PdfDocument(writer);
pdf.SetDefaultPageSize(PageSize.A4);
var document = new Document(pdf);
// Header table
var headerTable = new Table(1);
headerTable.SetWidth(UnitValue.CreatePercentValue(100));
var test = new Paragraph("dwawwa");
var headerCell = new Cell().Add(new Paragraph("Invoice"));
headerCell.SetTextAlignment(TextAlignment.CENTER);
headerTable.AddCell(headerCell);
document.Add(headerTable);
// Invoice details table
var detailsTable = new Table(2);
detailsTable.SetWidth(UnitValue.CreatePercentValue(100));
detailsTable.SetMarginTop(20);
detailsTable.AddCell("Invoice Number");
detailsTable.AddCell("12");
detailsTable.AddCell("Date");
detailsTable.AddCell(DateTime.Now.ToString("dd/MM/yyyy"));
// Add more details as needed
document.Add(detailsTable);
// Footer
var footerTable = new Table(1);
footerTable.SetWidth(UnitValue.CreatePercentValue(100));
footerTable.SetMarginTop(20);
var footerCell = new Cell().Add(new Paragraph("Thank you for your business!"));
footerCell.SetTextAlignment(TextAlignment.CENTER);
footerTable.AddCell(footerCell);
document.Add(footerTable);
document.Close();
return memoryStream.ToArray();
}
And this should be API response:
public async Task<IActionResult> GetInvoicePDF()
{
var response = CreateInvoiceDocument();
if (response == null)
return BadRequest("Failed to generate the PDF.");
var stream = new MemoryStream(response);
return new FileStreamResult(stream, "application/pdf");
}
Please, what I'm doing wrong nad thank you in advance.
答案1
得分: 1
尝试使用 FileContentResult,它继承自 IActionResult,但有助于Swagger识别它为SwaggerUI中的文件。
public async Task<FileContentResult> GetInvoicePDF()
{
var response = CreateInvoiceDocument();
if (response == null)
return BadRequest("生成PDF失败。");
var stream = new MemoryStream(response);
FileContentResult file = new FileContentResult(stream.ToArray(), "application/pdf")
{
FileDownloadName = Path.GetFileName("yourfilename.pdf")
};
return file;
}
英文:
Try to use FileContentResult which inherits from IActionResult but helps Swagger to recognize it as a file in SwaggerUI.
public async Task<FileContentResult> GetInvoicePDF()
{
var response = CreateInvoiceDocument();
if (response == null)
return BadRequest("Failed to generate the PDF.");
var stream = new MemoryStream(response);
FileContentResult file = new FileContentResult(stream.ToArray(), "application/pdf")
{
FileDownloadName = Path.GetFileName("yourfilename.pdf")
};
return file;
}
答案2
得分: 0
After adding MediaTypeNames.Application.Pdf into FileContentResult everything work as expected. Thank you all and hope that code below will help you in the future
在将MediaTypeNames.Application.Pdf添加到FileContentResult后,一切都按预期运行。谢谢大家,希望下面的代码对您有帮助。
英文:
Ok, after a while I finally figure it out.After adding MediaTypeNames.Application.Pdf into FileContentResult everything work as expected.
Thank you all and hope that code below will help you in the future
public async Task<FileContentResult> GetInvoicePDF()
{
var response = CreateInvoiceDocument();
if (response == null)
return BadRequest("Failed to generate the PDF.");
var stream = new MemoryStream(response);
var result = new FileContentResult(response, MediaTypeNames.Application.Pdf)
{
FileDownloadName = "test.pdf"
};
return result;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论