govardhan govardhan - 13 days ago 8
C# Question

Cant able to load image into pdf with Evopdf

i was trying with evopdf to convert html to pdf and in my html there is an image with absolute path of it but image is unable to load it into pdf.here "sb" is string builder in which i append the html string.

PdfConverter pdf = new PdfConverter();
byte[] outPdfBuffer = pdf.GetPdfBytesFromHtmlString(sb.ToString());

HttpContext.Current.Response.BinaryWrite(outPdfBuffer);

HttpContext.Current.Response.End();


as i have given full path i didnt giving base url,here is my image in html:

<img src="C:/Users/ingyadav/Desktop/icons/icons/logo_160.png" alt="Symphony Summit"/>


can anyone help me on this

Answer

That's easy, the source of your Image need be from http, if you have a console app and want to fix that you can transform the image into base64,

public string GetImage(string path)
{
  using (Image image = Image.FromFile(path))
    {                 
        using (MemoryStream m = new MemoryStream())
        {
            image.Save(m, image.RawFormat);
            byte[] imageBytes = m.ToArray();

            // Convert byte[] to Base64 String
            return Convert.ToBase64String(imageBytes);                
        }                  
    }
}

and

sb.Append("<img src=" + @"data:image/jpeg;base64," + GetImage(path) + "' />");

If you have a webapp, I strongly recommend using relative path, or a CDN path.

<img alt="Image with Full URL" src="http://www.domain.com/Images/Logo.jpg" />

or

<img alt="Image with Relative path" src="/Images/Logo.jpg" />