Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Generate PDF documents from a HTML page using ASP.NET

Generate PDF documents from a HTML page using ASP.NET

Scheduled Pinned Locked Moved ASP.NET
csharphtmlasp-netdesignsysadmin
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GopalakrishnanPS
    wrote on last edited by
    #1

    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..

    P B 2 Replies Last reply
    0
    • G GopalakrishnanPS

      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..

      P Offline
      P Offline
      Parwej Ahamad
      wrote on last edited by
      #2

      Look at ITextSharp. Some sample code: http://am22tech.com/s/22/Blogs/post/2011/09/28/HTML-To-PDF-using-iTextSharp.aspx[^]

      Parwej Ahamad

      1 Reply Last reply
      0
      • G GopalakrishnanPS

        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..

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        In which of the lines do you get that error? And is that img src="../Images/image1.png" ever shown correctly on your page?

        G 1 Reply Last reply
        0
        • B Bernhard Hiller

          In which of the lines do you get that error? And is that img src="../Images/image1.png" ever shown correctly on your page?

          G Offline
          G Offline
          GopalakrishnanPS
          wrote on last edited by
          #4

          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);**

          B 1 Reply Last reply
          0
          • G GopalakrishnanPS

            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);**

            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #5

            And what about using a full URL, i.e. img src="http://www.yourserver.com/images/myimage.jpg"?

            G 1 Reply Last reply
            0
            • B Bernhard Hiller

              And what about using a full URL, i.e. img src="http://www.yourserver.com/images/myimage.jpg"?

              G Offline
              G Offline
              GopalakrishnanPS
              wrote on last edited by
              #6

              nouw im developing in Localhost

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups