locked
formatting issue with iTextSharp RRS feed

  • Question

  • User-2084247921 posted

    Hello
     
    I have a requirement to write pdfdocument using html contents in an ASP.NET MVC Project. I am writing the HTML code inside a string variable and then I am giving that string for rendering it as pdf bytes using the below code. The returned bytes I am saving it as pdf on the server. However I am getting too many formatting issues in the pdf generated.
     
    When I saved the html contents which I passed to the string, formattings are good but somehow when I am trying to create a pdf out of those contents I am getting issues. Any help will be greatly appreciated.
    public byte[] Render(string htmlText)
    {
    byte[] renderedBuffer;
     

    using (var outputMemoryStream = new MemoryStream())
    {
    using (var pdfDocument = new Document())
    {
    PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
    pdfWriter.CloseStream = false;
    //pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle };
    pdfDocument.Open();
    using (var htmlViewReader = new StringReader(htmlText))
    {
    using (var htmlWorker = new HTMLWorker(pdfDocument))
    {
    htmlWorker.Parse(htmlViewReader);
    }
    }
    }
     
    renderedBuffer = new byte[outputMemoryStream.Position];
    outputMemoryStream.Position = 0;
    outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
    }
     
    return renderedBuffer;
    }

    Tuesday, February 26, 2013 1:12 AM

All replies

  • User197322208 posted

    I am getting issues.

    What issues?

    Tuesday, February 26, 2013 1:56 AM
  • User-2084247921 posted

    The format of the HTML contents are not getting rendered properly in pdf. I should be able to see the pdf exactly how that html file is looking and thats my problem

    Tuesday, February 26, 2013 2:36 AM
  • User485493498 posted

    You need to provide in line style for the HTML tags before sending the html string to pdf & then try again to create the pdf. Hopefully it will reslove your issue of formating

    Tuesday, February 26, 2013 3:10 AM
  • User-2084247921 posted

    Your suggestion seems to be a solution but I am stuck here because there are lot many styles been used from an external css file. So inorder for my pdf to get generated everything should work properly with almost no changes on the htm file as the same html file is been used for all other requirements as well.

    Friday, March 1, 2013 12:07 AM
  • User485493498 posted

    Then your html file will not use any css classs becasue the formatting will not applied, so you need to make that page to use inline style instead of css classes. And it will reslove your problem.

    Friday, March 1, 2013 9:47 PM