Home > Blockchain >  Create a PDF from a user-edited ASP.NET form?
Create a PDF from a user-edited ASP.NET form?

Time:06-25

So I have a basic ASPX page that I can push into a PDF. Based on overriding the Render() method so that it creates the PDF, using the iText7 package. Works fine. This behaves so that the initial page being rendered just displays the PDF. I'll include the sample code below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Html2pdf;

namespace Dch_Take_In_Form
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void Render(HtmlTextWriter writer)
        {
            MemoryStream mem = new MemoryStream();
            StreamWriter twr = new StreamWriter(mem);
            HtmlTextWriter myWriter = new HtmlTextWriter(twr);
            base.Render(myWriter);
            myWriter.Flush();
            myWriter.Dispose();
            StreamReader strmRdr = new StreamReader(mem);
            strmRdr.BaseStream.Position = 0;
            string pageContent = strmRdr.ReadToEnd();
            strmRdr.Dispose();
            mem.Dispose();
            writer.Write(pageContent);            
            CreatePDFDocument(pageContent);
        }

        public void CreatePDFDocument(string strHtml)
        {
            string strFileName = HttpContext.Current.Server.MapPath("test.pdf");            
            HtmlConverter.ConvertToPdf(strHtml, File.Create(strFileName));
            ShowPdf(strFileName);
        }

        public void ShowPdf(string strFileName)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "inline;filename="   strFileName);
            Response.ContentType = "application/pdf";
            Response.WriteFile(strFileName);
            Response.Flush();
            Response.Clear();
        }
    }
}

My goal is to have a web form, with various textboxes, checkboxes, etc. all populated by the enduser. So when the enduser is done they can click a button control and then the PDF is generated based on that input. The PDF will silently save off on the server-end, transparent to the enduser. Any suggestions or code snippets on how to most effectively accomplish reading the current web form into an HTML string?

The iText7 method that facilitates the PDF creation takes (among other things) a string that represents the HTML page code. So that's what I'd need to provide, by reflecting on what the current web form contains.

CodePudding user response:

Here is what I did to accomplish this.

  1. Added EnableEventValidation="false" to the ASPX page header.

  2. Added an empty method override for VerifyRenderingInServerForm to the code-behind.

  3. Added the following method for scraping the current page to extract the HTML code.

    public string ScrapePage() { var sw = new StringWriter(); var hw = new HtmlTextWriter(sw); this.Page.RenderControl(hw); var strHtml = sw.ToString(); return strHtml; }

So now I can push the HTML code into the iText PDF writer method and everything works as expected.

  • Related