-1

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

aggy
  • 163
  • 1
  • 2
  • 14

2 Answers2

5

For local resources you have to use fully qualifed URLs prefixed by file:///. In your case your HTML the image tag should be:

<img src="file:///C:/Users/ingyadav/Desktop/icons/icons/logo_160.png" alt="Symphony Summit"/>
EvoPdf
  • 523
  • 3
  • 9
  • i tried it in your live demo but,still its not loading the image – aggy Sep 15 '16 at 09:28
  • The live demo runs on the EvoPdf server and cannot access local file from your computer. You have to download the library package from http://www.evopdf.com/download.aspx and try the demo on your computer. Let me know how this works for you – EvoPdf Sep 15 '16 at 09:49
1

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" />
SilentTremor
  • 4,747
  • 2
  • 21
  • 34