Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I know this is a slightly old question, however I couldn't find a better solution for this. I'm facing an issue with iTextSharp when converting an HTML string to a PDF. The HTML contains an img tag with a base64-encoded image, but the image is not displaying in the resulting PDF. I'd appreciate any help or insights on how to resolve this problem.

What I have tried:

C#
strimg CEMDrawSignpath = "data:image/png;base64,iVBOR...II="
string htmlTable = @"
        <div class='content'>
          <table width='100%' style='" + pagecss.ToString() + @"'>
            <tr style='position: relative;'>
			   <td>
			      <img src='"+ CEMDrawSignpath + @"' />
               </td>
	        </tr>
          </table>
        </div>";

    hdnhtmltable.Value = htmlTable;
    MemoryStream memoryStream = new MemoryStream();
    PdfWriter pdfWriter = PdfWriter.GetInstance(doc, memoryStream);
    doc.Open();
    string CompanyName = lblTopCompanyname.Text;
    string htmlTable = hdnhtmltable.Value;
    XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, doc, new 
    StringReader(htmlTable));
    doc.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", 
       "attachment;filename=EngagementLetter.pdf");
    Response.BinaryWrite(memoryStream.ToArray());
    Response.End();
Posted
Updated 25-Oct-23 7:27am
v5
Comments
Richard Deeming 16-Oct-23 3:47am    
Firstly, I've removed the massive Base64 dump from your question, since the actual content is not relevant. It's enough to know that it's a valid Base64 representation of an image.

Secondly, the HTML you're generating does not have an img tag with a Base64-encoded image; it has an img tag with its src set to a variable you haven't described (lblcempath), followed by a Base64 string outside of the img tag. Start by explaining what's in that unknown variable, and why you would want to display a raw Base64 string to the user.
Member 12926744 16-Oct-23 5:11am    
I have updated my code , as 'lblcempath' is not actually required and CEMDrawSignpath is the byte64 encoded image as it is taking from database.
Richard Deeming 16-Oct-23 5:19am    
As far as I can see, you need a custom tag processor for the image tag - there's a VB.NET example in this SO question[^] which might point you in the right direction.
[no name] 16-Oct-23 11:23am    
One has nothing to do with the other. You should be dealing with an actual "image" (converted or otherwise), and PDF "code"; not some "encoding".

1 solution

I assume the issue is converting htmltext with image to pdf

Try changing the following line

XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, doc, new StringReader(htmlTable));

with
using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlTable)))
{                                
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, doc, htmlMemoryStream, System.Text.Encoding.UTF8);
}

and if you are using external css
using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
{
using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlTable)))
{ 
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, doc, htmlMemoryStream, cssMemoryStream);
}
}


Hope this help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900