HTML轉換爲PDF我試圖將HTML轉換成PDF格式,但它給錯誤:在ASP .NET MVC
Failed to load PDF document.
我想生成一個簡單的PDF格式,而無需使用任何.dll文件。有沒有什麼辦法可以在不使用iTextSharp的情況下生成PDF,例如旋轉w7?
代碼:
public ActionResult EventoVisualizarPDF()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["UrlAPI"]);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var id = Session["intCodigoGrupoUsuario"];
var intUsuarioId = Session["intUsuarioId"];
string url = "";
url = "api/usuario/GetBuscaUsuario/" + intUsuarioId;
HttpResponseMessage resposta = client.GetAsync(url).Result;
var usuario = resposta.Content.ReadAsAsync<Usuario>().Result;
url = "api/evento/GetEventoByUsuario/" + id;
HttpResponseMessage response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
var eventos = response.Content.ReadAsAsync<IEnumerable<Evento>>().Result;
string htmlText = this.RenderRazorViewToString("RelatorioEventoPDF", eventos);
byte[] buffer = System.Text.Encoding.Default.GetBytes(htmlText);
return File(buffer, "application/pdf");
}
else
{
string msg = response.IsSuccessStatusCode.ToString();
throw new Exception(msg);
}
}
public string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}