0

In C# I am trying to generate and download automatic the PDF from HTML but it's not getting generated.

byte[] bytes = null;
MemoryStream memoryStream = new MemoryStream();
RetailerInvoice Data = new RetailerInvoice();
try
{
    var _ResultId = dataTypeConverter.ConvertToGuid(RetailerId);
    string TempHtml = "<html><head><title></title></head><bodyHello World</body></html>";
    StringReader Sr = new StringReader(TempHtml);
    Document pdfDoc = new Document(PageSize.A4,10f,10f,10f,0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter writter = PdfWriter.GetInstance(pdfDoc, memoryStream);
    pdfDoc.Open();
    htmlparser.Parse(Sr);
    pdfDoc.Close();
    pdfDoc.Dispose();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-dispostion", "attachment;filename-Invoive.pdf");
    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
    Response.Write(pdfDoc);
    Response.End();
    bytes = memoryStream.ToArray();
    memoryStream.Write(bytes, 0, bytes.Length);
    memoryStream.Position = 0;
}
catch (Exception ex)
{
    Data = null;
}
return new FileStreamResult(memoryStream, "application/pdf");
halfer
  • 19,824
  • 17
  • 99
  • 186
  • You write `pdfDoc` to `Response`. This does not make sense as the `Document pdfDoc` does not contain the full document data, it merely is a facade for building a document. And doing anything after `Response.End()` does not make sense, either. – mkl Apr 23 '18 at 11:10
  • Could you please help me out from the same – Amit Dhumal Apr 23 '18 at 11:26

0 Answers0