2012-07-19 20 views
5

से पीडीएफ दस्तावेज़ लौटाएं डेटा से एचटीएमएल पेज जेनरेट करने का सबसे अच्छा तरीका क्या है? मेरे पास सभी टेबल और आदि के साथ एक HTML टेम्पलेट है। JqueryTemplate जैसे किसी भी templating का उपयोग नहीं करना चाहते हैं।एएसपीनेट एमवीसी नियंत्रक

+0

आप एमवीसी synrax रेज़र में निर्माण का उपयोग क्यों नहीं करना चाहते हैं? या आपको इसे क्लाइंटसाइड करने की ज़रूरत है? –

+0

आपको इसे क्लाइंट साइड करने की आवश्यकता क्यों है? क्यों पीडीएफ सर्वर पक्ष उत्पन्न नहीं करते हैं और बाइट्स के रूप में तैयार पीडीएफ वापस आते हैं? –

उत्तर

2

बस पीडीएफ सर्वर पक्ष बनाएं और HTML व्यू के बजाय फ़ाइल लौटाएं। मैं पीडीएफ प्रदाता आप किस तरह का उपयोग नहीं करते हैं, लेकिन इस 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

ठीक है तुमने मुझे याद किया। हमें शुरू से करना चाहिए। आपका असली मामला क्या है? आप क्या करना चाहते हैं? कंट्रोलर से एचटीएमएल पेज पर नियंत्रक –

6

hiqpdf html to pdf converter, एक व्यावसायिक उत्पाद का उपयोग इस दृष्टिकोण की कोशिश करो यह नमूना कोड: How to convert HTML to PDF using HiQPDF

+0

के बिना किसी भी प्रकार का डेटा भेजता है, मैं 3 दिनों के लिए पीडीएफ फ़ाइल दिखाने की खोज कर रहा था। लेकिन यह समाधान बेहतर है। धन्यवाद – MustafaP

संबंधित मुद्दे