'Trying to download text from a server as a pdf in ASP.NET MVC

this is the method in the controller

RuleText is a string so I convert it to a byte array

public ActionResult DownloadFileFromDatabase(int id)
       {
        var file =  db.Rule.FirstOrDefault(x => x.RuleId == id);
        var content = file.RuleText;
        var bytes = Encoding.UTF8.GetBytes(content);
        var result = new FileContentResult(bytes, "application/pdf;");
        result.FileDownloadName = "myfile.pdf";
        return result;
       }  

when I try to open the downloaded pdf I get "Error Failed to load PDF document."

As a side note, when I change the content type to "text/plain" and file name to "myfile.txt" it works -- it downloads a text file with the correct text

The downloaded pdf won't open in adobe either. How can I make this method work as intended?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source