I have been trying to create Pdf file using ITextSharp. I am able to pass html content via ajax but pdf contains some images which are saved as base64 encoded data in database.
How would i be able to get those data and convert them as an image and insert in my table rows.
I would like to achieve something like this:
<table>
<tr>
<td>{image 1} </td><pre><td>{image 1} </td>
</tr>
</table>
I am able to create the pdf files but instead of image i am getting base64 data
This is my code:
public ActionResult CreatePdf(string htmlData)
{
string mytblhtml = htmlData;
mytblhtml += GetImage();
string fileName = "MyPdf.pdf";
using(var ms = new MemoryStream())
{
using(var doc = new Document(PageSize.A4, 25, 25, 25, 25))
{
using(var writer = PdfWriter.GetInstance(doc, ms))
{
doc.Open();
using(var srHtml = new StringReader(mytblhtml))
{
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
}
doc.Close();
}
}
// bPDF = ms.ToArray();
TempData[fileName] = ms;
return Json(new { success = true, fileName }, JsonRequestBehavior.AllowGet);
}
}
private string GetImage()
{
string imgHtml = "<table><tr>";
using(EmpDb db = new EmpDb())
{
var emps = db.Employees.ToList();
foreach(var e in emps)
{
if(!String.IsNullOrWhiteSpace(e.Image))
{
imgHtml += $@"<td>data:{e.Image}</td>";
}
}
imgHtml += "</tr></table>";
}
return imgHtml;
}
public ActionResult DownloadCertPdf(string fileName)
{
var ms = TempData[fileName] as MemoryStream;
if(ms == null)
{
return new EmptyResult();
}
TempData[fileName] = null;
return File(ms.ToArray(), "application/pdf", fileName);
}