2012-07-19 9 views
5

ビューからデータからhtmlページを生成する最適な方法は何ですか?私はすべてのテーブルなどでHTMLテンプレートを持っています.JqueryTemplateのようなテンプレートを使用したくないです。Asp.net MVCコントローラからPDF文書を返す

+0

なぜmvc synrax razorでビルドを使用したくないのですか?または、クライアント側で行う必要がありますか? –

+0

なぜあなたはそれをクライアント側で行う必要がありますか?なぜpdfサーバ側を生成し、バイトとしてpdfを返すのでしょうか? –

答えて

2

ちょうどpdfサーバー側を作成し、HTMLビューの代わりにファイルを返します。 私は、PDFプロバイダの種類は、あなたが使用しませんが、このiTextSharpのためのソリューション:

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; 
    } 
} 

出典:

How to return PDF to browser in MVC?

+0

あなたは私を噛みました。始めから始めることができます。あなたの本当の事は何ですか?何をしたいですか?コントローラーの有無にかかわらず、コントローラーからhtmlページへのデータの送信には多くの種類があります –

関連する問題