stighy
stighy

Reputation: 7170

iTextSharp: impossible to create PDF. Why?

I'm trying to convert an HTML to a PDF using iTextSharp. This is my function:

 public static Byte[] ConvertToPdf_iTextSharp(string html)
    {
        Byte[] res;

        StringReader sr = new StringReader(html);

        Document pdfDoc = new Document(PageSize.A4);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        using (MemoryStream memoryStream = new MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
            pdfDoc.Open();

            htmlparser.Parse(sr);
            pdfDoc.Close();

            res = memoryStream.ToArray();
            memoryStream.Close();                
        }

        return res;
    }

I got an error on HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

Error is: The remote server returned an error: (401) Unauthorized.

I found that the problem is when there are images in my HTML.

Images are standard html tag <img src="http://mywebsite/images/some.png" />

I found that if i "force" local path name ( something like this <img src="c:/inetpub/wwwroot/mysite/images/some.png" />) it works.

What can it be the problem ?

Thanks

Upvotes: 0

Views: 132

Answers (1)

just.another.programmer
just.another.programmer

Reputation: 8815

This sounds like an authentication problem. Can you verify that the images are hosted on a website that doesn't require you to login? The easiest way to check is to open a new incognito window and try to access an image. If you get a access denied page, you know the problem.

To fix it, either move the images to a site that doesn't require authentication or setup iTextSharp with your login credentials to the site.

Upvotes: 3

Related Questions