Reputation: 1
I have a query related to PDF generation from HTML code. I have a table written in HTML code and I want to parse it in a PDF writer using iTextSharp.dll, I have been using the same code with different html content in another section of my website and it is working absolutely fine.
Here is my code below please do comment.
StringBuilder sb = new StringBuilder();
sb.Append("<table width='100%' cellspacing='5' cellpadding='3'>");
//sb.Append("<tr><td colspan=4><img src='logo.png' width='250px' height = '70px'/></td></tr>");
sb.Append("<tr><td></td><td colspan=2 align=center>JK Cement Works, Jharli</td><td></td></tr>");
sb.Append("<tr><td>Duplicate Tickets</td><td></td><td></td><td></td></tr>");
sb.Append("<tr><td>Print DateTime:</td><td>");
sb.Append(printDT.Text.ToString());
sb.Append("</td><td></td><td></td></tr>");
sb.Append("<tr><td></td><td></td><td></td><td></td></tr>");
sb.Append("<tr><td>Transaction Number</td><td>");
sb.Append(tNum.Text.ToString());
sb.Append("</td><td></td><td></td></tr>");
sb.Append("<tr><td>Truck Number</td><td>");
sb.Append(TruckNum.Text.ToString());
sb.Append("</td><td></td><td></td></tr>");
sb.Append("<tr><td>Product Code</td><td>");
sb.Append(pCode.Text.ToString());
sb.Append("</td><td>Product Name</td><td>");
sb.Append(pName.Text.ToString());
sb.Append("</td></tr>");
sb.Append("<tr><td>Supplier Code</td><td>");
sb.Append(sCode.Text.ToString());
sb.Append("</td><td>Supplier Name</td><td>");
sb.Append(sName.Text.ToString());
sb.Append("</td></tr>");
sb.Append("<tr><td>Transporter Code</td><td>");
sb.Append(tCode.Text.ToString());
sb.Append("</td><td>Transporter Name</td><td>");
sb.Append(tName.Text.ToString());
sb.Append("</td></tr>");
sb.Append("<tr><td>Challan Number</td><td>");
sb.Append(challanNum.Text.ToString());
sb.Append("</td><td>Challan DateTime</td><td>");
sb.Append(challanDT.Text.ToString());
sb.Append("</td></tr>");
sb.Append("<tr><td></td><td></td><td></td><td></td></tr>");
sb.Append("<tr><td colspan=2>Delivery Note Number</td><td></td><td></td></tr>");
sb.Append("<tr><td>Shift</td><td>");
sb.Append(shift.Text.ToString());
sb.Append("</td><td></td><td></td></tr>");
sb.Append("<tr><td></td><td></td><td></td><td></td></tr>");
sb.Append("<tr><td>IN DateTime</td><td>");
sb.Append(InDT.Text.ToString());
sb.Append("</td><td>Gross Weight</td><td>");
sb.Append(gWeight.Text.ToString());
sb.Append(" Tons</td></tr>");
sb.Append("<tr><td>Out DateTime</td><td>");
sb.Append(OutDT.Text.ToString());
sb.Append("</td><td>Tare Weight</td><td>");
sb.Append(tWeight.Text.ToString());
sb.Append(" Tons</td></tr>");
sb.Append("<tr><td></td><td></td><td>Net Weight</td><td>");
sb.Append(nWeight.Text.ToString());
sb.Append(" Tons</td></tr></table>");
StringReader sr = new StringReader(sb.ToString());
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=InvoiceSJ.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
Upvotes: 0
Views: 743
Reputation: 96064
You first write the PDF to the Response.OutputStream
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
only to immediately thereafter clear the Response
(and so throw the PDF away)
Response.Clear();
and eventually output pdfDoc
Response.Write(pdfDoc);
which does not make sense because the iTextSharp Document
class does not hold the PDF document, it merely is a front end class via which you provide content for the PDF. This content is forwarded to a PdfWriter
which as soon as possible writes the content in PDF form to its output stream.
Please try, therefore, something along these lines:
[... create StringReader sr ...]
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=InvoiceSJ.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.End();
Depending on the very browser and version, a Content-Length
header might be required. If the above does not work for you yet, therefore, please try writing the PDF to a MemoryStream
and get the PDF as a byte[]
, then initialize the Response
, set a Content-Length
header with the length of the array and then write that array.
That been said, the HTMLWorker
indeed has been deprecated a long time ago; for serious development you should use the XMLWorker
instead, just like Amedee commented.
Upvotes: 1