1

I wanna get a pdf file from API in ASP.Net core, but I don't know how to do this. what I do is send a model to the api and I receive a pdf file

This is the method in my API that returns the file (In Postman it works)

[HttpPost]
public ActionResult getPDF([FromBody] Models.Request.InvoiceRequest model) {
   if (Verify(model.Token))  // This is a method that validate access token
   {
      byte[] file = pdfConf(model);       // This is a method that converts html file to pdf
      return File(file, "application/pdf");  // This returns the pdf file
   }

   return Ok("Invalid action");
}

This is my code in the web application, but it doesn't work.

    [HttpGet]
    public FileResult getPDF(int factura) 
    {

        this.GetSession();      

        // Object sent to the web api

        FacturaRequest oFacturaRequest = new FacturaRequest();
        oFacturaRequest.Token = oUserSession.accessToken;
        oFacturaRequest.IdFactura = factura;

        RequestUtil oRequestUtil = new RequestUtil();


        JavaScriptSerializer js = new JavaScriptSerializer();   
        string json = JsonConvert.SerializeObject(oFacturaRequest); // Convert model to json format

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Constants.Url.GETFACTURA);
        request.Method = "POST";
        request.PreAuthenticate = false;
        request.Accept = "application/json";
        request.MediaType = "application/json";
        request.ContentType = "application/json; charset=utf-8";
        request.Timeout = 60000;

        using (var oStreamWriter = new StreamWriter(request.GetRequestStream()))
        {
            oStreamWriter.Write(json);
            oStreamWriter.Flush();
        }

        HttpWebResponse oHttpResponse = (HttpWebResponse)request.GetResponse();
        var result = "";

        using (var oStreamReader = new StreamReader(oHttpResponse.GetResponseStream()))
        {
            result = oStreamReader.ReadToEnd();
        }

        return File(result,"application/pdf","Factura");
    }
2
  • Do you want to create a pdf file whose contents are based on the JSON contents returned to you from the "Factura" web request? If I understand you right, you will need to build the pdf file from scratch using the received JSON contents. A free library that can help with this itextpdf7. Here is a link to an example on the official site- itextpdf.com/en/resources/books/itext-7-jump-start-tutorial-net/… Commented May 21, 2020 at 11:57
  • no, what I want is to receive the pdf that comes from the api Commented May 21, 2020 at 18:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.