Generate PDF documents from a HTML page using ASP.NET
-
I used iTextSharp.dll to create pdf. but that works only for text HTML content. if i use images in my page it throughs exception that images are not found... my Design file
<asp:Panel ID="pdfPannel" runat="server">
Sample Text
<img src="../Images/image1.png"/>
</asp:Panel>
<asp:Button ID="btnSave" runat="server" Text="Save As PDF" onclick="btnSave_Click" />
my method: protected void btnSave_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=print.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); pdfPannel.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } when i click that save button im getting error Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\Images\image1.png'. Please tell me is there any alternate solution is there to create pdf. Thanks in Advance..
-
I used iTextSharp.dll to create pdf. but that works only for text HTML content. if i use images in my page it throughs exception that images are not found... my Design file
<asp:Panel ID="pdfPannel" runat="server">
Sample Text
<img src="../Images/image1.png"/>
</asp:Panel>
<asp:Button ID="btnSave" runat="server" Text="Save As PDF" onclick="btnSave_Click" />
my method: protected void btnSave_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=print.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); pdfPannel.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } when i click that save button im getting error Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\Images\image1.png'. Please tell me is there any alternate solution is there to create pdf. Thanks in Advance..
Look at ITextSharp. Some sample code: http://am22tech.com/s/22/Blogs/post/2011/09/28/HTML-To-PDF-using-iTextSharp.aspx[^]
Parwej Ahamad
-
I used iTextSharp.dll to create pdf. but that works only for text HTML content. if i use images in my page it throughs exception that images are not found... my Design file
<asp:Panel ID="pdfPannel" runat="server">
Sample Text
<img src="../Images/image1.png"/>
</asp:Panel>
<asp:Button ID="btnSave" runat="server" Text="Save As PDF" onclick="btnSave_Click" />
my method: protected void btnSave_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=print.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); pdfPannel.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } when i click that save button im getting error Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\DevServer\Images\image1.png'. Please tell me is there any alternate solution is there to create pdf. Thanks in Advance..
In which of the lines do you get that error? And is that
img src="../Images/image1.png"
ever shown correctly on your page? -
In which of the lines do you get that error? And is that
img src="../Images/image1.png"
ever shown correctly on your page?image is showing correctly in the page. but the error comes when i crteating pdf. Error comes when i parse the html elements. it takes wrong url path for images.**
htmlparser.Parse(sr);**
-
image is showing correctly in the page. but the error comes when i crteating pdf. Error comes when i parse the html elements. it takes wrong url path for images.**
htmlparser.Parse(sr);**
And what about using a full URL, i.e.
img src="http://www.yourserver.com/images/myimage.jpg"
? -
And what about using a full URL, i.e.
img src="http://www.yourserver.com/images/myimage.jpg"
?nouw im developing in Localhost