Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a HTML code with some text and an image, which is to be converted to PDF. But when generating PDF, text is generated perfectly but the image is being ignored. I am using Itext 5.5.13 and itextsharp.xmlworker 5.5.13.When converted to pdf, the expected result was that, the text with image should have come at the bottom, but no image is shown.
Below is the c# code to generate pdf :

C#
using (var ms = new MemoryStream())
{
//Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
using (var doc = new Document(PageSize.A4, 40, 40, 215, 180))
{
//Create a writer that's bound to our PDF abstraction and our stream
using (var writer = PdfWriter.GetInstance(doc, ms))
{
writer.PageEvent = new ITextEvents();
//Open the document for writing
doc.Open();    
string Report_Html_Text = " <div>
 <p align="center" style="margin-bottom:.0001pt; text-  align:center;       margin:0in 0in 10pt">
 <span style="font-size:11pt">
 <span style="font-family:&quot;Arial&quot;,&quot;sans- serif&quot;">CT Scan Of Brain</span>
 </u></b></span>
 </p>
 <p style="margin-bottom:.0001pt; margin:0in 0in  10pt;font-size:11pt;line-height:115%;font-family:Calibri,sans-serif">
 <b><u><span style="font-size:12.0pt">
 <span style="font-family:&quot;Arial&quot;,&quot;sans-serif& quot;">Procedure </span></span>
 </u></b>
 </span>
 </p>
 <p style="margin-bottom:.0001pt; margin:0in 0in   10pt;font-size:11pt;line-height:115%;font- family:Calibri,sans-serif;">
 <span style="font-family:&quot;Arial&quot;,&quot;sans-serif&quot;">CT Scan of brain &ndash; 
 <b>Plain and contrast</b> study.</span></span>
 </p>
 <ul>
 <li style="margin-top:0in; margin-right:0in; margin- bottom:.0001pt; margin:0in 0in 10pt;font-size:11pt;line-height:115%;font-family:Calibri,sans-serif;">
 <span style="font-family:&quot;Arial&quot;,&quot;sans- serif&quot;">No detectable abnormalities are present in 
 <b>posterior fossa</b> structures including <b>fourth ventricle.</b></span>
 </li>
 <li style="margin-top:0in; margin-right:0in; margin- bottom:.0001pt; margin:0in 0in 10pt;font- size:11pt;line-height:115%;line-height:115%;">
 <span style="font-family:&quot;Arial&quot;,&quot;sans-serif&quot;">Sylvian fissures cortical sulci and basal subarachnoid spaces are widened.</span>
 </li>
 </ul>
 <ul>
 <li style="margin-top:0in; margin-right:0in; margin-bottom:.0001pt; margin:0in 0in 10pt"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,sans-serif"><b>
 <span style="line-height:115%"><span style="font-family:&quot;Arial&quot;,&quot;sans-serif&quot;">Supratentorial ventricles</span></span></b>
 <span style="line-height:115%"><span style="font-family:&quot;Arial&quot;,&quot;sans-serif&quot;"> are dilated with septum in midline.</span></span></span> </span></span>
 </li>
 <li style="margin-top:0in; margin-right:0in; margin-bottom:.0001pt; margin:0in 0in 10pt"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,sans-serif"><b><span style="line-height:115%"><span style="font-family:&quot;Arial&quot;,&quot;sans-serif&quot;">No focal</span></span></b><span style="line-height:115%">
 <span style="font-family:&quot;Arial&quot;,&quot;sans-serif&quot;"> abnormal area is present in the supratentorial brain parenchyma.</span></span></span> </span></span>
 </li>
 <img alt="" src="http://localhost:4119/upload/ReportImages/files/photo-x-ray-head-and-brain.jpg" style="width: 1023px; height: 682px;" />

</div> ";

C#
using (var srHtml = new StringReader(Report_Html_Text))
{
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
}
doc.Close();
}
}
Byte[] bytes = ms.ToArray();
return bytes;
}


What I have tried:

Previously I uses Htmlworker, but as it is depreciated, I used XMLWorker instead to genrate pdf. Xmlworker is respecting the html tags and indentation, but ignored the image, and I have used the absolute Url .
Posted
Updated 17-Jan-19 22:53pm
Comments
Richard Deeming 18-Jan-19 10:06am    
From what I can see, you'd need an IImageProvider implementation to load remote images.

Have you tried replacing the image URL with the local path to the image?
Kautilya Kumar 21-Jan-19 1:14am    
No sir I haven't use local path, because this project is an asp application and when hosted, the image would be retrieved from server.

1 solution

while working with xmlworker we need to be more careful about html, it should be validated html string with proper open and close tag, in your html string last ul tag is not properly closed.
 
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