Convert HTML to PDF in MVC with iTextSharp in MVC Razor

后端 未结 9 1262
情深已故
情深已故 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条回答
  •  旧时难觅i
    2020-12-02 16:42

    This is how to do it using MVC:

    [Route("ABCDD")]
    [HttpGet]
    public void ABCDD() {
        WebClient wc = new WebClient();
        // string url = HttpContext.Current.Request.Url.AbsoluteUri;
        string url = "http://localhost:3042/Reports/COAListing";
        string fileContent = wc.DownloadString(url);
    
        List tableContents = GetContents(fileContent, table_pattern);
    
        string HTMLString = String.Join(" ", tableContents.ToArray());
    
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(new Paragraph("Welcome to dotnetfox"));
        List htmlarraylist = HTMLWorker.ParseToList(new StringReader(HTMLString), null);
        for (int k = 0; k < htmlarraylist.Count; k++) {
            pdfDoc.Add((IElement) htmlarraylist[k]);
        }
    
        pdfDoc.Close();
        HttpContext.Current.Response.ContentType = "pdf/application";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;" +
                "filename=sample.pdf");
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Write(pdfDoc);
        HttpContext.Current.Response.End();
    }
    

提交回复
热议问题