0

I have an API that opens a pdf file in a browser, I want to call that programmatically from another WinForms project(Both in the same solution).

Here is the Controller and setups:

app.UseCors();

[EnableCors("*")]
[Route("api/pdfcreator")]
[ApiController]
public class PdfCreatorController : Controller
{
    private IConverter _converter;
    public PdfCreatorController(IConverter converter)
    {
        _converter = converter;
    }
    [HttpGet]
    public IActionResult CreatePDF()
    {
        var globalSettings = new GlobalSettings
        {
            ColorMode = ColorMode.Color,
            Orientation = Orientation.Portrait,
            PaperSize = PaperKind.A4,
            Margins = new MarginSettings { Top = 10 },
            DocumentTitle = "PDF Report",
            //Out = @"D:\PDFCreator\Employee_Report.pdf"
        };
        var objectSettings = new ObjectSettings
        {
            PagesCount = true,
            HtmlContent = TemplateGenerator.GetHTMLString(),
            WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") },
            HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true },
            FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" }
        };
        var pdf = new HtmlToPdfDocument()
        {
            GlobalSettings = globalSettings,
            Objects = { objectSettings }
        };
        //_converter.Convert(pdf);
        var file = _converter.Convert(pdf);
        //return Ok("Successfully created PDF document.");
        return File(file, "application/pdf");
    }
}

Here is how I want to call it:

getApi("http://localhost:44344/api/pdfcreator");



public string GetApi(string ApiUrl)
    {
        var responseString = "";
        var request = (HttpWebRequest)WebRequest.Create(ApiUrl);
        request.Method = "GET";
        request.ContentType = "application/pdf";

        using (var response1 = request.GetResponse())
        {
            using (var reader = new StreamReader(response1.GetResponseStream()))
            {
                responseString = reader.ReadToEnd();
            }
        }
        return responseString;

    }

But the response here is a string, while we are interested in calling the API only!

How to do this?

Considering this, It is not clear how to send the response to the browser again after parsing it!

TiyebM
  • 2,684
  • 3
  • 40
  • 66
  • 1
    Are you asking how to get the response as bytes so you can parse the PDF? You can probably pass the `Stream` from `GetResponseStream` on its own as the response stream to the browser as long as the content type is still "application/pdf". – IllusiveBrian Oct 07 '21 at 23:36
  • Of course it's string - `public string GetApi(string ApiUrl)` - what do you really want as the output? A file? Bytes as @IllusiveBrian mentions? What do you mean by ` interested in calling the API only`? Do you want nothing returned? – Ermiya Eskandary Oct 08 '21 at 09:00
  • The API does what it is supposed to do, which is showing the pdf file in a browser when we type the URL in the address bar, I want to trigger that action or event in a winform application. I have two options:1-Make the api returns byte[] as suggested by @IllusiveBrian, but how then could we show that file in browser? 2- Try to simulate the event of typing in the browser! – TiyebM Oct 08 '21 at 09:04
  • I changed the title to better illustrate the problem. – TiyebM Oct 08 '21 at 09:18
  • What do you want to happend in the Winforms application when you get the file from the API? – Magnus Oct 08 '21 at 09:25
  • @Magnus Thank you, simply show the file in browser. – TiyebM Oct 08 '21 at 09:27
  • Doesn't the API require authentication? – CodeCaster Oct 08 '21 at 09:33
  • @CodeCaster In this case no. – TiyebM Oct 08 '21 at 09:35

1 Answers1

3

So from your comment in the question I assume you want to show the file in the browser from the winforms application.

Then you can do this. It will launch the default browser and go to the file.

System.Diagnostics.Process.Start("http://localhost:44344/api/pdfcreator");
Magnus
  • 45,362
  • 8
  • 80
  • 118