0

I am working on displaying an image on a webpage that is exported to a pdf using the IronPDF library. The problem I am having is that changing the img tag in cshtml is causing the image to either display in the PDF or the webpage but not both.

Image file is stored in wwwroot/images folder.

Current img tag:

<img src="../images/Logo.png" alt="Logo" align="right" style="width:150px">

This displays the image in the PDF but not on the webpage. If I change the src to be .../images/Logo.png, that displays on the webpage but not the pdf.

PDF create method:

public string CreateAddendumPDF(string html, string agreementID)
{
    var render = new ChromePdfRenderer();
    render.RenderingOptions.MarginLeft = 17;
    render.RenderingOptions.MarginRight = 15;
    render.RenderingOptions.MarginTop = 14;
    render.RenderingOptions.FirstPageNumber = 1;

    var pdf = render.RenderHtmlAsPdf(html,@"wwwroot/images");

    string rootPath = _webHostEnvironment.WebRootPath;

    string path = Path.Combine(rootPath, ReportConstants.Temp + "Addendums\\" + agreementID + "_" +"Addendum.pdf");

    pdf.SaveAs(path);

    return path;
}

I tried different image src values and could not get one to work in both the webpage and the pdf document. Next solution is to use Jquery to change the image tag when the user clicks the download pdf button.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Try to use full urls for the images and not relative ones. Because the PDF tool doesn't know where the image is stored. – D A Jul 19 '23 at 09:13

1 Answers1

0

If your "Logo.png" is in wwwroot/images/ you could try something like this:

<img src="~/wwwroot/images/Logo.png" alt="Logo" align="right" style="width:150px">

or

<img src="~/images/Logo.png" alt="Logo" align="right" style="width:150px">

with this you should be able to get your image.

I hope it helps.

F205
  • 93
  • 1
  • 1
  • 11