Convert html to pdf in asp.net
We can convert HTML or .aspx using iTextSharp liberary.
you can download here iTextSharp.dll.
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
=================.CS code=========================================
protected void LinkButtonDownloadPdf_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=PrintTicket.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.RenderControl(hw);
pnlPerson.Style.Add("text-decoration", "none");
pnlPerson.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
pnlPerson.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
==========================.aspx code=================================
<asp:LinkButton ID="LinkButtonDownloadPdf" runat="server" Text="Download PDF" OnClick="LinkButtonDownloadPdf_Click" />
</div>
<asp:Panel ID="pnlPerson" runat="server">
<table width="100%" class="Paratext">
<tr>
<td style="width: 15%">
<b>Hotel Name</b>
</td>
<td style="width: 40%">
<asp:Label ID="lblHotel" runat="server"></asp:Label>
</td>
<td style="width: 15%">
<b>Telephone</b>
</td>
<td style="width: 30%">
<asp:Label ID="lblHotelPhone" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td valign="top">
<b>Address</b>
</td>
<td colspan="3">
<asp:Label ID="lblHotelAddress" runat="server"></asp:Label>
</td>
</tr>
</table>
</asp:Panel>
We can convert HTML or .aspx using iTextSharp liberary.
you can download here iTextSharp.dll.
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
=================.CS code=========================================
protected void LinkButtonDownloadPdf_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=PrintTicket.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.RenderControl(hw);
pnlPerson.Style.Add("text-decoration", "none");
pnlPerson.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
pnlPerson.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
==========================.aspx code=================================
<asp:LinkButton ID="LinkButtonDownloadPdf" runat="server" Text="Download PDF" OnClick="LinkButtonDownloadPdf_Click" />
</div>
<asp:Panel ID="pnlPerson" runat="server">
<table width="100%" class="Paratext">
<tr>
<td style="width: 15%">
<b>Hotel Name</b>
</td>
<td style="width: 40%">
<asp:Label ID="lblHotel" runat="server"></asp:Label>
</td>
<td style="width: 15%">
<b>Telephone</b>
</td>
<td style="width: 30%">
<asp:Label ID="lblHotelPhone" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td valign="top">
<b>Address</b>
</td>
<td colspan="3">
<asp:Label ID="lblHotelAddress" runat="server"></asp:Label>
</td>
</tr>
</table>
</asp:Panel>