Results 1 to 5 of 5

Thread: HTML to PDF???

  1. #1

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Question HTML to PDF???

    How do I convert a HTML page to a .PDF?

    I have the iTextSharp.dll on my pc, don't know if that will have the functionality I need though.

  2. #2
    New Member
    Join Date
    May 2011
    Posts
    3

    Re: HTML to PDF???

    I don't know if this is helpful but try,

    http://www.cutepdf.com/products/cutepdf/writer.asp

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: HTML to PDF???

    Hello,

    If you are talking about programmatically creating a PDF, then yes, iTextSharp is definitely what you are after. If you search these forums then you will find a number of examples of doing just what you are after.

    Gary

  4. #4
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: HTML to PDF???

    This will do it, just add the reference to iTextSharp and modify the folder path as required
    Code:
    public void GeneratePDFDoc(string htmlText)
        {
              Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
    
             PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("FOLDERPATH").Trim()) +"/" +"FileName.pdf", FileMode.OpenOrCreate));
       
          
            doc.AddDocListener(writer);
            doc.Open();
    
    
    
    
    
    // step 4: we Add some content
    
                //make an arraylist ....with STRINGREADER since its no IO reading file...
              
       
    
          
            List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText),null);
           
                //add the collection to the document
                for (int k = 0; k < htmlarraylist.Count; k++)
                {
    
                    doc.Add((IElement)htmlarraylist[k]);
                }
    
                
                doc.Close();
         
        }

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: HTML to PDF???

    Nice sample!

    The only slight change that I would make, personally, is if Document and PdfWriter implement IDisposable would be to wrap them both in a using/Using block.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width