Home > Software design >  Asp.Net Core MVC - Image does not load/display while converting view into pdf
Asp.Net Core MVC - Image does not load/display while converting view into pdf

Time:05-11

This is related to my last question. I am trying to convert a view into pdf. The view is getting converted into a pdf file as expected but the image inside view doesn't get loaded/displayed when the view is getting converted to pdf. If I run the view independetly on the browser, it displays the image, but when I put it through this conversion, it doesn't load/display image.

Here is the code for the controller method :

public async Task<IActionResult> Receipt(string rId)
        {
            //Receipt Data-----------------------------------------------------------------
            ParentForApply ParentVM = new ParentForApply();
            ParentVM.DonateDataList = _db.ApplicantDonation.Where(i => i.ReceiptId == rId);
            WC.ReceiptId = ParentVM.DonateDataList.First().ReceiptId;
            WC.DateOfD = ParentVM.DonateDataList.First().DateOfD;
            WC.SchAmount = ParentVM.DonateDataList.First().SchAmount;
            WC.FullName = ParentVM.DonateDataList.First().FullName;
            WC.ApplicantContact = ParentVM.DonateDataList.First().ApplicantContact;
            WC.ApplicantEmail = ParentVM.DonateDataList.First().ApplicantEmail;
            WC.ApplicantCity = ParentVM.DonateDataList.First().ApplicantCity;
            WC.ApplicantState = ParentVM.DonateDataList.First().ApplicantState;
            WC.DonorFullName = ParentVM.DonateDataList.First().DonorFullName;
            WC.ContactNo = ParentVM.DonateDataList.First().ContactNo;
            WC.EmailId = ParentVM.DonateDataList.First().EmailId;
            //Receipt Data-----------------------------------------------------------------

            using (var stringWriter = new StringWriter())
            {
                var viewResult = _compositeViewEngine.FindView(ControllerContext, "ViewReceipt", false);
                if (viewResult == null)
                {
                    throw new ArgumentNullException("View Can't Be Found");
                }
                var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
                var viewContext = new ViewContext(

                    ControllerContext,
                    viewResult.View,
                    viewDictionary,
                    TempData,
                    stringWriter,
                    new HtmlHelperOptions()
                    );
                await viewResult.View.RenderAsync(viewContext);

                var htmlToPdf = new HtmlToPdf(1000, 1000);
                htmlToPdf.Options.DrawBackground = true;

                var pdf = htmlToPdf.ConvertHtmlString(stringWriter.ToString());
                var pdfbytes = pdf.Save();

                return File(pdfbytes, "application/pdf");
            }
        }

Here is the code for view page where image is placed :

<div  style="background-color:white;">
            <table>
                <tr>
                    <td><img src="~/images/logo.png"  id="headImg" style="height:100px;width:100px;" /></td>
                    <td>
                        <h1>Derawala Education & Charitable Trust</h1>
                        <h6>Building Generation, Building Nation</h6>
                    </td>
                </tr>

            </table>
        </div>

CodePudding user response:

Your image src needs to use the address of the full accessible path for it to work properly.

Like :

<img id="mainImage" style="width:200px;height:160px" src="https://localhost:5001/Images/ProductImages/d0f8bbb9-effd-4f93-b5bc-b28fdfe4a233Capture.PNG">

It works for me.

enter image description here

  • Related