I'm using iTextSharp v. 5.5.6 to convert HTML to PDF. HTML converts to PDF just fine without img tags, but sometimes HTML contains img tags with Base64 data as the source and these do not appear on PDF.
Here is example of HTML with Base64 img tag:
<td>
<center>
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD..."></img>
</center>
</td>
Base64 data decodes properly with an online decoder.
Here is the code I use to render PDF:
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using iTextSharp.tool.xml;
protected void BuildPdf(string html, string example_css, string stocknumber, string makemodel)
{
Byte[] bytes;
using (var ms = new MemoryStream())
{
using (var doc = new Document())
{
using (var writer = PdfWriter.GetInstance(doc, ms))
{
doc.Open();
using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
{
using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
{
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
}
}
doc.Close();
}
}
bytes = ms.ToArray();
}
Response.Clear();
MemoryStream msi = new MemoryStream(bytes);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + stocknumber + " - " + makemodel + ".pdf");
Response.Buffer = true;
msi.WriteTo(Response.OutputStream);
Response.End();
}
Can someone please tell me how to get iTextSharp to include Base64 images in PDF? I can use javascript to work with img tag on the front-end or pass it to the code-behind and use C# to manipulate it in the back-end.