1

I am developing MVC application, and i have problem with XMLWorker and ImageProvider. My images are not shown in pdf afrer rendering html that it receives.

Here is the code:

        var bytes = Encoding.UTF8.GetBytes(html);
        string cssPath = HttpContext.Current.Server.MapPath("~/Content");

        using (MemoryStream input = new MemoryStream(bytes))
        {
            MemoryStream output = new MemoryStream();
            using (Document pdfDoc = new Document(PageSize.LETTER, 50, 50, 50, 50))
            {
                using (PdfWriter pdfWrt = PdfWriter.GetInstance(pdfDoc, output))
                {
                    pdfWrt.CloseStream = false;
                    pdfWrt.PageEvent = new PdfWriterEvents("watermark");
                    pdfDoc.Open();
                    HtmlPipelineContext context = new HtmlPipelineContext(null);
                    context.SetTagFactory(iTextSharp.tool.xml.html.Tags.GetHtmlTagProcessorFactory());
                    context.SetImageProvider(new MyImageProvider());
                    var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
                    cssResolver.AddCssFile(cssPath + "\\main.css", true);
                    IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(context, new PdfWriterPipeline(pdfDoc, pdfWrt)));
                    var worker = new XMLWorker(pipeline, true);
                    var parser = new XMLParser();
                    parser.AddListener(worker);


                    using (TextReader sr = new StringReader(html))
                    {
                        parser.Parse(sr);
                    }
                    pdfDoc.Close();
                    byte[] test = output.GetBuffer();
                    File.WriteAllBytes(HttpContext.Current.Server.MapPath("~/Content/PDFs/test.pdf"), test);

                }
            }
        }

And here is my ImageProvider implementation:

public class MyImageProvider : AbstractImageProvider
{
    public override string GetImageRootPath()
    {
        return HttpContext.Current.Server.MapPath("~/Content/Images");
    }
} 

Any idea?

user1797770
  • 149
  • 1
  • 1
  • 10
  • 1
    To help you debug, instead of parsing directly to the `Document` object try writing to a custom `IElementHandler` so you can walk each object. See [the second and third code blocks here](http://stackoverflow.com/a/24498296/231316) for how to do that. Also, instead of `output.GetBuffer();` always use `output.ToArray();`. The former actually includes uninitialized (junk) bytes that will corrupt your PDF in certain common scenarios. – Chris Haas Sep 10 '14 at 14:44
  • Thank you for your reply. I already tried with custom IElementHandler, it did`t work. Anyway, i will probably remove images from HTML and create them as pdf images – user1797770 Sep 11 '14 at 08:03
  • I suggested the `IElementHandler` only so you could put a breakpoint on your `for` loop and see what iTextSharp is actually doing. – Chris Haas Sep 11 '14 at 13:16
  • I tried again with custom IElementHandler and it seems that the img tag is not recognized as instance of IWritable interface. When i put div or span as my html it works, and add method is executed. For img tags it is not the case, add method is not firing. Any idea why? – user1797770 Sep 12 '14 at 07:27
  • Thank you very much for your help. The problem was in image url. ImageProvider was not returning correct url. – user1797770 Sep 12 '14 at 07:53

1 Answers1

0

I had to change the image source tags to include the entire url to get them to work.

hogarth45
  • 3,387
  • 1
  • 22
  • 27