Convert HTML to PDF in MVC with iTextSharp in MVC Razor

后端 未结 9 1227
情深已故
情深已故 2020-12-02 15:56

I am trying to convert HTML to PDF with iTextSharp in MVC Razor, but everything I have tried has not worked. Does anyone know how to accomplish this?

9条回答
  •  旧巷少年郎
    2020-12-02 16:40

    public virtual void printpdf(string html)    
    {
         String htmlText = html.ToString();
         Document document = new Document();
         string filePath = HostingEnvironment.MapPath("~/Content/Pdf/");
         PdfWriter.GetInstance(document, new FileStream(filePath + "\\pdf-"+Filename+".pdf", FileMode.Create));
    
         document.Open();    
         iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);       
         hw.Parse(new StringReader(htmlText));    
         document.Close();    
    }
    

    just pass html string in to parameter that string you will get by renderpartialview text = viewname....

提交回复
热议问题