嘿伙计们希望你们都做得很好,我的问题就是我想从我的WebAPI响应中返回一个PDF文件,这是我用来获取创建pdf文件的响应的代码。
[Route("Reporte")]
public async Task<HttpResponseMessage> createReport(RequestIdSugeridoModel request)
{
Thread.CurrentThread.CurrentCulture = culture;
ResponseEncabezadoSugeridoModel encabezado = new ResponseEncabezadoSugeridoModel();
List<ResponseDetalleSugeridoModel> detalle = new List<ResponseDetalleSugeridoModel>();
SolicitudSugeridoController solicitud;
Document document;
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
try
{
using (SqlConnection CN_SGE = new SqlConnection(CNSGE))
{
solicitud = new SolicitudSugeridoController();
await CN_SGE.OpenAsync();
await solicitud.GetObtenEncabezadoSugerido(CN_SGE, encabezado, request);
await solicitud.GetObtenDetSugerido(CN_SGE, detalle, request);
}
//start with pdf creation
document = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.AddTitle("Reporte de Compras");
document.AddCreator("Disisa");
document.Open();
// set standard font type and size
iTextSharp.text.Font _standardFont = new iTextSharp.text.Font(iTextSharp.text.Font.HELVETICA, 9, iTextSharp.text.Font.NORMAL, Color.BLACK);
//write document header
document.Add(new Paragraph(encabezado.razonsocial));
//document.Add(Chunk.NEWLINE);
document.Add(new Paragraph("COMPRAS SUGERIDAS"));
//document.Add(Chunk.NEWLINE);
document.Add(new Paragraph(encabezado.nombresucursal));
//document.Add(Chunk.NEWLINE);
// Create table for pdf
PdfPTable tableHeader = new PdfPTable(2);
PdfPCell cellFolio = new PdfPCell(new Phrase("FOLIO: " + encabezado.folio + " " + "SEMANA: " + encabezado.semana, _standardFont));
PdfPCell cellFecha = new PdfPCell(new Phrase("FECHA: " + encabezado.fecha, _standardFont));
// set border and aligment for the cells
cellFolio.Border = 0;
cellFolio.HorizontalAlignment = 0; // 0=left, 1=center, 2=right
cellFecha.Border = 0;
cellFecha.HorizontalAlignment = 2; // 0=left, 1=center, 2=right
// Add cells to the table header
tableHeader.AddCell(cellFolio);
tableHeader.AddCell(cellFecha);
PdfPTable mainTable = new PdfPTable(8);
mainTable.HorizontalAlignment = 1;
// here we will add table headers for report details
PdfPCell cellProducto = new PdfPCell(new Phrase("NOMBRE DEL PRODUCTO", _standardFont));
PdfPCell cellExistencia = new PdfPCell(new Phrase("EXISTENCIA", _standardFont));
PdfPCell cellMes1 = new PdfPCell(new Phrase(encabezado.mes1, _standardFont));
PdfPCell cellMes2 = new PdfPCell(new Phrase(encabezado.mes2, _standardFont));
PdfPCell cellMes3 = new PdfPCell(new Phrase(encabezado.mes3, _standardFont));
PdfPCell cellMes4 = new PdfPCell(new Phrase(encabezado.mes4, _standardFont));
PdfPCell cellSugerido = new PdfPCell(new Phrase("SUGERIDO", _standardFont));
PdfPCell cellObservacion = new PdfPCell(new Phrase("OBSERVACIONES", _standardFont));
mainTable.AddCell(cellProducto);
mainTable.AddCell(cellExistencia);
mainTable.AddCell(cellMes1);
mainTable.AddCell(cellMes2);
mainTable.AddCell(cellMes3);
mainTable.AddCell(cellMes4);
mainTable.AddCell(cellSugerido);
mainTable.AddCell(cellObservacion);
foreach (var item in detalle)
{
mainTable.AddCell(item.nombreproducto);
mainTable.AddCell(item.existencia.ToString());
mainTable.AddCell(item.mes1.ToString());
mainTable.AddCell(item.mes2.ToString());
mainTable.AddCell(item.mes3.ToString());
mainTable.AddCell(item.mes4.ToString());
mainTable.AddCell(item.sugerido.ToString());
mainTable.AddCell(item.observacion);
}
// Add tableHeader to document
document.Add(tableHeader);
// Add mainTable to document
document.Add(mainTable);
//return Request.CreateResponse(System.Net.HttpStatusCode.OK, document);
memoryStream.Position = 0;
HttpResponseMessage response = new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StreamContent(memoryStream)
{
Headers = {
ContentType = new MediaTypeHeaderValue(System.Net.Mime.MediaTypeNames.Application.Pdf),
ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "PdfFileName.pdf" }
}
}
};
// close document and writer
document.Close();
writer.Flush();
return response;
}
catch (ApplicationException ex)
{
ErrorModel _errors = new ErrorModel();
_errors.message = ex.Message;
return Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, _errors);
}
catch (Exception ex)
{
ErrorModel _errors = new ErrorModel();
_errors.message = ex.Message;
return Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, _errors);
}
}
现在,当我用邮递员测试它时,我唯一得到的回答是:
{
"PageNumber": 0,
"LeftMargin": 36,
"RightMargin": 36,
"TopMargin": 36,
"BottomMargin": 36,
"Left": 36,
"Right": 576,
"Top": 756,
"Bottom": 36,
"PageSize": {
"Type": 30,
"Chunks": [],
"Width": 612,
"Height": 792,
"Rotation": 0
},
"JavaScript_onLoad": null,
"JavaScript_onUnLoad": null,
"HtmlStyleClass": null
}
我怎样才能做到这一点?
提前致谢。我已经按照现在的方式更新了代码,并提供了@ckuri提供的答案