1

I was using Rotativa for the conversion of html to pdf. My whole aim was to send receipt as pdf to customers emailid. It was working well locally but when deployed in GoDaddy Server its not supported. So I am planning to generate the pdf using iTextSharp(Viewers Opinion is also invited).

Code written for the generation of pdf in Rotativa:

 public ActionResult GetPdfReceipt(int RegId)
 {
    var actionPDF = new Rotativa.ActionAsPdf("GetPdfReceipt", new { RegId = regId })
    {
      FileName = "Receipt.pdf"
    };
     //Dynamic student receipt pdf
     var byteArrayDynamic = actionPDF.BuildPdf(ControllerContext);
 }

 public ActionResult GetPdfReceipt(int RegId)
 {
    try
    {
        var _mdlReceiptPdf = new ReceiptPdfVM
        {
                ...
                ...
        };
        return View("Receipts", _mdlReceiptPdf);
    }
    catch (Exception ex)
    {
        return View("");
    }
}

I have gone through some of the codes for generating pdf using iTextSharp and it was as follows

HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = "application/pdf";

        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=LoginReportPerDay.pdf");
        StringWriter sWriter = new StringWriter();
        HtmlTextWriter hTWriter = new HtmlTextWriter(sWriter);

        GridView1.RenderControl(hTWriter);
        StringReader sReader = new StringReader(sWriter.ToString());
        Document pdf = new Document(PageSize.A4);
        HTMLWorker worker = new HTMLWorker(pdf);
        PdfWriter.GetInstance(pdf, HttpContext.Current.Response.OutputStream);
        pdf.Open();
        worker.Parse(sReader);
        pdf.Close();
        HttpContext.Current.Response.Write(pdf);
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();

How can I use the above code to render dynamically generated Receipt View pdf array bytes.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
ksg
  • 3,927
  • 7
  • 51
  • 97
  • Is your question, "How do I get a PDF as a byte array instead of writing directly to `Response` object's stream"? If so, instead of using `HttpContext.Current.Response.OutputStream` use a `System.IO.MemoryStream` and call `ToArray()` on it when you're done. See [this](http://stackoverflow.com/a/25164258/231316) for a sample as well as an explanation on why not to use `HTMLWorker` any more. – Chris Haas Jan 20 '16 at 15:10
  • Thanks for the reply . My problem is I cannot set value of `example_html` as a static string or path as mentioned the [example](http://stackoverflow.com/questions/25164257/how-to-convert-html-to-pdf-using-itextsharp/25164258#25164258).Its is dynamically created at runtime.So how can I parse dynamically generated `view` to `string`. – ksg Jan 21 '16 at 05:24
  • Do this help? [Render a view as a string](http://stackoverflow.com/q/483091/231316) – Chris Haas Jan 21 '16 at 14:27

0 Answers0