2012-07-19 15 views
5

Jaki jest najlepszy sposób wygenerowania strony html z danych w widoku? Mam szablon HTML ze wszystkimi tabelami itp. Nie chcę używać szablonów takich jak JqueryTemplate.Powrót Dokument w formacie PDF z kontrolera Asp.net MVC

+0

Dlaczego nie chcesz korzystać z kompilacji w mVC synrax brzytwa? Czy musisz zrobić to po stronie klienta? –

+0

dlaczego musisz robić to po stronie klienta? Dlaczego nie wygenerować strony serwera pdf i zwrócić gotowy pdf jako bajty? –

Odpowiedz

2

Po prostu utwórz stronę serwera pdf i zwróć plik zamiast widoku HTML. nie jaką dostawcy pdf używasz ale to rozwiązanie dla iTextSharp:

How to return PDF to browser in MVC?

+0

Cóż, zazębiłeś mnie. Zacznijmy od początku. Jaki jest twój prawdziwy przypadek? co chcesz robić? Istnieje wiele różnych wysyłanych danych z kontrolera na stronę html z lub bez kontrolera –

6

Spróbuj tego podejścia z wykorzystaniem hiqpdf html to pdf converter, produkt handlowy:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     ViewBag.Message = "Welcome to ASP.NET MVC!"; 
     Session["MySessionVariable"] = "My Session Variable Value assigned in Index"; 

     return View(); 
    } 

    public ActionResult About() 
    { 
     return View(); 
    } 

    public string RenderViewAsString(string viewName, object model) 
    { 
     // create a string writer to receive the HTML code 
     StringWriter stringWriter = new StringWriter(); 

     // get the view to render 
     ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, viewName, null); 
     // create a context to render a view based on a model 
     ViewContext viewContext = new ViewContext(
       ControllerContext, 
       viewResult.View, 
       new ViewDataDictionary(model), 
       new TempDataDictionary(), 
       stringWriter 
       ); 

     // render the view to a HTML code 
     viewResult.View.Render(viewContext, stringWriter); 

     // return the HTML code 
     return stringWriter.ToString(); 
    } 

    [HttpPost] 
    public ActionResult ConvertThisPageToPdf() 
    { 
     // get the HTML code of this view 
     string htmlToConvert = RenderViewAsString("Index", null); 

     // the base URL to resolve relative images and css 
     String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri; 
     String baseUrl = thisPageUrl.Substring(0, thisPageUrl.Length - "Home/ConvertThisPageToPdf".Length); 

     // instantiate the HiQPdf HTML to PDF converter 
     HtmlToPdf htmlToPdfConverter = new HtmlToPdf(); 

     // hide the button in the created PDF 
     htmlToPdfConverter.HiddenHtmlElements = new string[] { "#convertThisPageButtonDiv" }; 

     // render the HTML code as PDF in memory 
     byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, baseUrl); 

     // send the PDF file to browser 
     FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); 
     fileResult.FileDownloadName = "ThisMvcViewToPdf.pdf"; 

     return fileResult; 
    } 

    [HttpPost] 
    public ActionResult ConvertAboutPageToPdf() 
    { 
     // get the About view HTML code 
     string htmlToConvert = RenderViewAsString("About", null); 

     // the base URL to resolve relative images and css 
     String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri; 
     String baseUrl = thisPageUrl.Substring(0, thisPageUrl.Length - "Home/ConvertAboutPageToPdf".Length); 

     // instantiate the HiQPdf HTML to PDF converter 
     HtmlToPdf htmlToPdfConverter = new HtmlToPdf(); 

     // render the HTML code as PDF in memory 
     byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, baseUrl); 

     // send the PDF file to browser 
     FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); 
     fileResult.FileDownloadName = "AboutMvcViewToPdf.pdf"; 

     return fileResult; 
    } 
} 

Źródło ten przykładowy kod: How to convert HTML to PDF using HiQPDF

+0

szukałem pliku PDF przez 3 dni. Ale to rozwiązanie jest lepsze. Dzięki – MustafaP

Powiązane problemy