简体   繁体   中英

while exporting html to pdf image is not displaying in itextsharp pdf document in asp.net

i want to add company logo on the top using img control. but while downloading image in pdf it is not displaying. while giving remote url link for image its displaying , but while giving server path of folders its not displaying image

 <table style="border-collapse: collapse;border: 1px solid;width:666px" align="center">


      <tr style="border: 1px solid;">
            <td style="border: 1px solid;width:50%" colspan="2">

              <img src="img/kst.png" />
             </td>
            <td style="border: 1px solid;width:50%" colspan="2">Hyderabad, India</td>
            <br />
            <br />
        </tr>

code behind

string Projname = "Payslip";
                Response.Clear();
                Response.AddHeader("content-disposition", string.Format("attachment;filename=\"{0}.pdf\"", Projname));
                // Response.AddHeader("content-disposition", "attachment;filename=WeeklyReport.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                design.RenderControl(htmlWrite);

                string myText = stringWrite.ToString();
                StringReader sr = new StringReader(myText.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
                Response.ContentType = "application/pdf";
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                pdfDoc.Open();
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
                pdfDoc.Close();
                Response.End();
                pdfDoc.Dispose();
                design.Visible = false;

使用绝对网址作为src="img/kst.png"图像源尝试类似src="http://yourdomain.com/img/kst.png"

I Use this:

<img src="@System.Web.HttpContext.Current.Server.MapPath("~/Images/????.???")" />

It works for me.

And you haven't to changed the PATH if you release the project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM