title: Create PDF from WebAPI response
body: <p>Hey guys hope you're all doing well, well my problem here is that i want to return a PDF file from my WebAPI response, this is the code im using to get the responses to create the pdf file.</p>
  
  <pre><code>[Route("Reporte")]
      public async Task&lt;HttpResponseMessage&gt; createReport(RequestIdSugeridoModel request)
      {
          Thread.CurrentThread.CurrentCulture = culture;
          ResponseEncabezadoSugeridoModel encabezado = new ResponseEncabezadoSugeridoModel();
          List&lt;ResponseDetalleSugeridoModel&gt; detalle = new List&lt;ResponseDetalleSugeridoModel&gt;();
          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.Applicat‌​ion.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);
          }
      }
  </code></pre>
  
  <p>Right now the only thing i get in my response when im testing it with postman is this:</p>
  
  <pre><code>{
  "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
  </code></pre>
  
  <p>}</p>
  
  <p>How can i achieve this? </p>
  
  <p>Thanks in advance. I have updated the code the way it is now, with the answer provided by @ckuri</p>