2

I'm trying to convert a simple HTML to PDF using iTextSharp. Consider the below scenario, where HTML div width will not fit within PDF Page size, content is blank in PDF.

TextReader reader = new StringReader(html);
using (MemoryStream memoryStream = new MemoryStream())
{
    using (Document document = new Document(PageSize.A4, 0, 0, 36, 0))
    {
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destPath, FileMode.Create));
        writer.ViewerPreferences = PdfWriter.PageModeUseOutlines | PdfWriter.PageLayoutSinglePage;
        writer.CloseStream = false;
        document.Open();
        //writer.PageEvent = new HeaderFooterHelper();
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser parser = new XMLParser(true, worker, Encoding.Unicode);
        parser.Parse(reader);
        worker.Close();
        document.Close();
    }
}

HTML Input :

<html>
<head>
</head>
<body>
    <div style="width:1000px">
        Blood is needed for emergencies and for people who have cancer, blood disorders,sickle cell, anemia and other illnesses. Some people need regular blood transfusions to live. For nearly 5 million people who receive blood transfusions every year, your donation can make the difference between life and death        
    </div>
</body>
</html>

We are migrating PDFs generated from EVO to iTextSharp. This is one of the issue faced during migration process. So content cannot be changed/manipulated based on pdf page size. In specific, looking for a solution with PageSize.A4 option.

  • *How to resolve this issue?* - simply don't try to squeeze too broad contents into a small page. I.e. use wider pages or smaller divs. – mkl Dec 29 '15 at 22:55
  • One **possible** solution is to upgrade to the latest versions of `iTextSharp` and `XML Worker`. Using your example code and the HTML input you show above, 5.4.0.0 outputs a blank PDF file. However, 5.5.8 outputs a PDF with the `
    ` element's text node intact. (although almost touching both left and right edges of the PDF)
    – kuujinbo Dec 30 '15 at 06:07
  • currently using 5.5.7 version – user1411884 Dec 30 '15 at 06:29

0 Answers0