Hugoware

The product of a web developer with a little too much caffeine

iTextSharp – Simplify Your HTML to PDF Creation

with 185 comments

Update: A second version that works in .NET 2.0 has been included at the end of this post.

If you’ve ever had to generate PDFs on the fly then you may have run into iTextSharp, which is a port of iText for C#. It isn’t as straight forward as some people might like, but it is certainly a powerful tool once you figure it out.

Normally, if you wanted to take some HTML and turn it into a PDF you would write a lot of extra code to recreate the document in PDF format, but lucky for all of us, iTextSharp also supports HTML. Again, it isn’t exactly straight forward, but to help I wrote a quick wrapper today to help with the process.

HtmlToPdfBuilder allows you to build a PDF using HTML and hides the complexities of working with iTextSharp. You still need iTextSharp to get this project to run, so make sure to include it.

To start, create a new HtmlToPdfBuilder object. As part of the constructor you’ll need to set the document size. It’s actually a Rectangle, but iTextSharp has predefined sizes already available in the PageSize class (constants)

//Page sizes are found in iTextSharp.text.PageSize
HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.LETTER);

After you have the builder you can add as many pages as you would like using the .AddPage() method. You can also access each of the pages by their index on the builder you create.

HtmlPdfPage first = builder.AddPage();
//also found at builder[0]

HtmlPdfPage second = builder.AddPage();

Once you’ve added your pages you can start adding your HTML with .AppendHtml().

first.AppendHtml("<h1>Hello World</h1>");

//you can also use params for formatting
second.AppendHtml("<h1>{0}</h1><span>{0}</span>", "Hello Second Page", "Another Param");

Next, you’re going to want to apply some styles to your PDF document, you can use a couple methods. First, the .AddStyle() let’s you add a single style to your page. The first parameter is the selector, such as "H1" or ".totals", the second parameter is a single line of CSS such as "color:#F00;font-weight:bold;".

There is also a method called .ImportStylesheet() that accepts an absolute path (not relative) to a stylesheet and adds all of the styles it finds. I was pretty pleased with the method because I was able to do the entire thing with a single Regular Expression.

//add individual styles
builder.AddStyle("H1", "color:#F00");
builder.AddStyle("p", "font-weight:bold;text-decoration:underline;");

//import an entire sheet
builder.ImportStylesheet("c:\\stylesheets\\pdf.css");

It’s worth mentioning that all of my efforts to set heights, widths, paddings and margins didn’t go so well. I’m not sure what the rules are when it comes to that part so be warned.

Finally, you’re ready to save your document. Use the .RenderPdf() method to get the bytes of your PDF.

byte[] file = builder.RenderPdf();
File.WriteAllBytes("c:\\output\\final.pdf", file);

If you’ve worked with iTextSharp before, or you want a little more control over the rendering process, the builder has two events named BeforeRender and AfterRender that give you access to the iTextSharp classes PdfWriter and Document.

Hopefully, this helps simplify working with HTML and PDFs with iTextSharp. Share your thoughts or suggestions!

Source Code

Download HtmlToPdfBuilder.cs

.

Below is a test version for .NET 2.0 – Please let me know if you discover any bugs. Since I am using Visual Studio 2008 I’m not always aware when there is code that won’t work in previous versions.

Download HtmlToPdfBuilder.cs (for .NET 2.0)

Don’t forget, you still need to download iTextSharp and add it as a reference in your project.

Written by hugoware

May 8, 2009 at 3:04 am

185 Responses

Subscribe to comments with RSS.

  1. […] R­ead­ m­­or­e h­er­e:  iTe­xtShar­p – Simplify Yo­­u­r­ HTML to­­ PDF C­r&#1… […]

    • Peter, great post i try now using your code and it work great, i did not read the code just use him.
      i have some problems with some fonts.
      i know how to use the itextsharp.
      can you tell me what font the code support ?

      dose the code can build tables ?

      Thanks.

      David

      June 17, 2009 at 4:33 pm

    • I am trying to compile the class but it is giving me the error:

      expected ; )

      where public event RenderEvent BeforeRender = (writer, document) => { };

      Any ideas?

      Anonymous

      January 31, 2011 at 12:31 pm

  2. Great post. All your post very important. I like content about open source, i need it. Thanks.

    Open Source Release

    May 8, 2009 at 1:50 pm

  3. This indeed is a very useful post…and I totally have to agree with you that itextsharp is a powerful yet terribly documented tool.
    Therefore I’m hoping you can help me with the following.

    I want to create a PDF with say 10 pages.
    The first 5 pages have background image X and the other 5 have background image Y (the image may vary).

    Is it possible with the HTML builder to do this and by defining the background image does that also mean the images arent inserted individually so the PDF doesnt get too large?

    AND do you perhaps have a code example?

    btw I made a first non-HTML builder attempt here, but I got stuck: http://forums.asp.net/p/1426760/3184974.aspx

    Peter

    May 25, 2009 at 6:25 am

    • Peter, I’ll check it out – I wasn’t having much luck with setting backgrounds to elements, but I’m willing to bet there is at least some way to do it.

      There is the BeforeRender and AfterRender events that you might be able to link into to make changes to the background (using the PdfWriter and Document).

      I’ll let you know what I find out.

      webdev_hb

      May 26, 2009 at 1:57 pm

      • Awesome! The generation of PDF is the final step in my application…hope you figure it out! It would be of great help!

        Peter

        May 26, 2009 at 6:18 pm

      • Ow, and one more thing 🙂

        Im generating images on the fly,not stored on disk, can I also insert those?

        My code looks like this:
        Dim imgOutput As New Bitmap(400, 40, PixelFormat.Format32bppArgb)
        Dim g As Graphics = Graphics.FromImage(imgOutput)
        g.Clear(Color.White)
        g.DrawString(“testbarcode”, New Font(“Free 3 of 9 Extended”, 20, FontStyle.Regular), SystemBrushes.WindowText, New PointF(0, 0))

        Thanks for your effort!!

        Peter

        May 27, 2009 at 2:30 pm

      • Peter, there is a way of adding background to anything and that is with the pdfStamper. I use it to GetOverContent method in the class and also the GetUnderContent with is what I use for background.

        Check it out you will find it useful.

        Jorge

        November 22, 2009 at 10:30 am

  4. Hi, im writing a page in visual basic.net, is there a vb version of this class? This will definitely help, i just want to import all my css files to the itextsharp. I also tried to convert the “import css” method and paste it in my code… still it cant read my styles.. is there a way we can apply css styles in itextsharp the normal way? like we are doing in the usual markup?

    Thanks in advance,
    red

    Red

    May 28, 2009 at 2:56 am

    • I translated it, do test it though:

      Imports System
      Imports System.Collections.Generic
      Imports System.Linq
      Imports System.Text
      Imports iTextSharp.text
      Imports System.IO
      Imports iTextSharp.text.pdf
      Imports iTextSharp.text.html.simpleparser
      Imports System.Collections
      Imports iTextSharp.text.html
      Imports System.Text.RegularExpressions

      Namespace PDFBuilder

      #Region “HtmlToPdfBuilder Class”

      ”’
      ”’ Simplifies generating HTML into a PDF file
      ”’
      Public Class HtmlToPdfBuilder

      #Region “Constants”

      Private Const STYLE_DEFAULT_TYPE As String = “style”
      Private Const DOCUMENT_HTML_START As String = “”
      Private Const DOCUMENT_HTML_END As String = “”
      Private Const REGEX_GROUP_SELECTOR As String = “selector”
      Private Const REGEX_GROUP_STYLE As String = “style”

      ‘amazing regular expression magic
      Private Const REGEX_GET_STYLES As String = “(?[^\{\s]+\w+(\s\[^\{\s]+)?)\s?\{(?[^\}]*)\}”

      #End Region

      #Region “Constructors”

      ”’
      ”’ Creates a new PDF document template. Use PageSizes.{DocumentSize}
      ”’
      Public Sub New(ByVal size As Rectangle)
      Me.PageSize = size
      Me._Pages = New List(Of HtmlPdfPage)()
      Me._Styles = New StyleSheet()
      End Sub

      #End Region

      #Region “Delegates”

      ”’
      ”’ Method to override to have additional control over the document
      ”’
      ”’

      ‘TODO
      ‘ Public Event BeforeRender As RenderEvent = Function(writer, document) Do
      ‘ End Function

      ””
      ”” Method to override to have additional control over the document
      ””
      ‘ Public Event AfterRender As RenderEvent = Function(writer, document) Do
      ‘ End Function

      #End Region

      #Region “Properties”

      ”’
      ”’ The page size to make this document
      ”’
      Private _PageSize As Rectangle
      Public Property PageSize() As Rectangle
      Get
      Return _PageSize
      End Get
      Set(ByVal value As Rectangle)
      _PageSize = value
      End Set
      End Property

      ”’
      ”’ Returns the page at the specified index
      ”’
      Default Public ReadOnly Property Item(ByVal index As Integer) As HtmlPdfPage
      Get
      Return Me._Pages(index)
      End Get
      End Property

      ”’
      ”’ Returns a list of the pages available
      ”’
      Public ReadOnly Property Pages() As IEnumerable(Of HtmlPdfPage)
      Get
      Return Me._Pages.AsEnumerable()
      End Get
      End Property

      #End Region

      #Region “Members”

      Private _Pages As List(Of HtmlPdfPage)
      Private _Styles As StyleSheet

      #End Region

      #Region “Working With The Document”

      ”’
      ”’ Appends and returns a new page for this document
      ”’
      Public Function AddPage() As HtmlPdfPage
      Dim page As New HtmlPdfPage()
      Me._Pages.Add(page)
      Return page
      End Function

      ”’
      ”’ Removes the page from the document
      ”’
      Public Sub RemovePage(ByVal page As HtmlPdfPage)
      Me._Pages.Remove(page)
      End Sub

      ”’
      ”’ Appends a style for this sheet
      ”’
      Public Sub AddStyle(ByVal selector As String, ByVal styles As String)
      Me._Styles.LoadTagStyle(selector, HtmlToPdfBuilder.STYLE_DEFAULT_TYPE, styles)
      End Sub

      ”’
      ”’ Imports a stylesheet into the document
      ”’
      Public Sub ImportStylesheet(ByVal path As String)

      ‘load the file
      Dim content As String = File.ReadAllText(path)

      ‘use a little regular expression magic
      For Each match As Match In Regex.Matches(content, HtmlToPdfBuilder.REGEX_GET_STYLES)
      Dim selector As String = match.Groups(HtmlToPdfBuilder.REGEX_GROUP_SELECTOR).Value
      Dim style As String = match.Groups(HtmlToPdfBuilder.REGEX_GROUP_STYLE).Value
      Me.AddStyle(selector, style)

      Next
      End Sub

      #End Region

      #Region “Document Navigation”

      ”’
      ”’ Moves a page before another
      ”’
      Public Sub InsertBefore(ByVal page As HtmlPdfPage, ByVal before As HtmlPdfPage)
      Me._Pages.Remove(page)
      Me._Pages.Insert(Math.Max(Me._Pages.IndexOf(before), 0), page)
      End Sub

      ”’
      ”’ Moves a page after another
      ”’
      Public Sub InsertAfter(ByVal page As HtmlPdfPage, ByVal after As HtmlPdfPage)
      Me._Pages.Remove(page)
      Me._Pages.Insert(Math.Min(Me._Pages.IndexOf(after) + 1, Me._Pages.Count), page)
      End Sub

      #End Region

      #Region “Rendering The Document”

      ”’
      ”’ Renders the PDF to an array of bytes
      ”’
      Public Function RenderPdf() As Byte()

      ‘Document is inbuilt class, available in iTextSharp
      Dim file As New MemoryStream()
      Dim document As New Document(Me.PageSize)
      Dim writer As PdfWriter = PdfWriter.GetInstance(document, file)

      ‘TODO
      ”allow modifications of the document
      ‘If TypeOf Me.BeforeRender Is RenderEvent Then
      ‘ Me.BeforeRender(writer, document)
      ‘End If

      ‘header
      document.Add(New Header(Markup.HTML_ATTR_STYLESHEET, String.Empty))
      document.Open()

      ‘render each page that has been added
      For Each page As HtmlPdfPage In Me._Pages
      document.NewPage()

      ‘generate this page of text
      Dim output As New MemoryStream()
      Dim html As New StreamWriter(output, Encoding.UTF8)

      ‘get the page output
      html.Write(String.Concat(HtmlToPdfBuilder.DOCUMENT_HTML_START, page._Html.ToString(), HtmlToPdfBuilder.DOCUMENT_HTML_END))
      html.Close()
      html.Dispose()

      ‘read the created stream
      Dim generate As New MemoryStream(output.ToArray())
      Dim reader As New StreamReader(generate)

      ‘TODO
      ‘For Each item In DirectCast(HTMLWorker.ParseToList(reader, Me._Styles), IEnumerable)
      ‘ document.Add(DirectCast(item, IElement))
      ‘Next

      ‘cleanup these streams
      html.Dispose()
      reader.Dispose()
      output.Dispose()

      generate.Dispose()
      Next

      ‘TODO
      ”after rendering
      ‘If TypeOf Me.AfterRender Is RenderEvent Then
      ‘ Me.AfterRender(writer, document)
      ‘End If

      ‘return the rendered PDF
      document.Close()

      Return file.ToArray()
      End Function

      #End Region

      End Class

      #End Region

      #Region “HtmlPdfPage Class”

      ”’
      ”’ A page to insert into a HtmlToPdfBuilder Class
      ”’
      Public Class HtmlPdfPage

      #Region “Constructors”

      ”’
      ”’ The default information for this page
      ”’
      Public Sub New()
      Me._Html = New StringBuilder()
      End Sub

      #End Region

      #Region “Fields”

      ‘parts for generating the page
      Friend _Html As StringBuilder

      #End Region

      #Region “Working With The Html”

      ”’
      ”’ Appends the formatted HTML onto a page
      ”’
      Public Overridable Sub AppendHtml(ByVal content As String, ByVal ParamArray values As Object())
      Me._Html.AppendFormat(content, values)
      End Sub

      #End Region

      End Class

      #End Region

      #Region “Rendering Delegate”

      ”’
      ”’ Delegate for rendering events
      ”’
      Public Delegate Sub RenderEvent(ByVal writer As PdfWriter, ByVal document As Document)

      #End Region

      End Namespace

      Peter

      May 28, 2009 at 6:38 am

      • Nice job!

        Anyone who uses it might want to compare it to the C# version to make sure all of the > < brackets came through (notice at the top for constant DOCUMENT_HTML_START)

        webdev_hb

        May 28, 2009 at 1:21 pm

      • Hi Peter,

        HeadingLine 1Line 2Line 3Line 4

        I have this html string… When I render it on the web its exactly the way is scripted. But when I generate the PDF It genereates it with spaces.

        Any clue y it has so much of space between lines…

        Thanks
        Deryl

        Deryl

        May 4, 2010 at 11:29 am

      • Hi Peter,

        HeaderLine 1Line 2Line 3Line 4
        I have this html string… When I render it on the web its exactly the way is scripted. But when I generate the PDF It genereates it with spaces.

        Any clue y it has so much of space between lines…

        Thanks
        Deryl

        Deryl

        May 4, 2010 at 12:54 pm

    • You are able to apply inline styles when you use the AppendHtml commands.

      document.AppendHtml(“<h1 style=’color:#f00′ >Some text</h1>”);

      webdev_hb

      May 28, 2009 at 1:26 pm

  5. Peter, I’ve looked into this some and I haven’t found any solutions just yet.

    It seems at first glance that the html parser in iTextSharp just doesn’t use the property.

    webdev_hb

    May 29, 2009 at 9:57 pm

    • mmm…shame.
      But I’ve been looking around in the meantime as well. I think the general iTextsharp support is terrible! My questions are never answered!
      I’ve now switched to PDFsharp, it provides more options and has a much more active forum!
      Not sure if the HTML creation as you have so well demonstrated here is supported but I’ve put my faith there!
      Thanks for your effort!

      Peter

      May 29, 2009 at 10:08 pm

  6. Hi,

    anybody has problems with anidated tables in html to process with iTextSharp?.

    HtmlToPdfBuilder run with “dirty” html?

    Thanks.

    Fabian

    June 5, 2009 at 12:51 pm

  7. Thanks for this great piece of work !

    Is there a way to insert line-style-graphics ? Sucha as bordered html tables or an ? I cant get it working.

    Arjan

    June 8, 2009 at 7:24 pm

    • I’m not sure about that – you might be able to use something like …

      <table border=’1′ >… etc…

      I don’t think that the iTextSharp HTML parser is as good as I expected. I’ve been working on my own version of it lately, but I don’t see myself finished for a while.

      webdev_hb

      June 8, 2009 at 7:29 pm

      • Unfortunately it is not working.

        Does anyone have a tip how to draw a line ?

        Arjan

        June 17, 2009 at 5:56 pm

  8. i have included this in an assembly and tried to build it and seem to get issues with:-

    Cannot convert type ‘var’ to ‘iTextSharp.text.IElement’ line 222 and some syntax issues with BeforeRender and AfterRender delegates.

    System.Collections.Generic.List’ does not contain a definition for ‘AsEnumerable’

    got any ideas?

    dom

    June 11, 2009 at 8:44 am

    • Can you post some more of the code? I’m not sure that I understand the problem entirely.

      Thanks!

      webdev_hb

      June 11, 2009 at 2:26 pm

    • You can cast it like this

      (IEnumerable)this._Pages;

      Dave

      February 1, 2011 at 5:32 am

  9. Hi!

    I would like to add a header and a footer the my pdf. You say that I can reach Document width BeforeRender but how?
    Or do you have tip on how to create header and footer?

    Fredrik

    June 30, 2009 at 9:00 am

    • There are two events on the HtmlToPdfBuilder class called BeforeRender and AfterRender. You should be able to do something like…

      HtmlToPdfBuilder pdf = new HtmlToPdfBuilder();
      pdf.BeforeRender += new RenderEvent(HandleBeforeRender);

      //elsewhere in your code
      void HandleBeforeRender(iTextSharp.text.pdf.PdfWriter writer, Document document) {
      //etc…
      }

      webdev_hb

      June 30, 2009 at 10:24 am

      • And how do go on from there? Could you post some kind of example? Im kind of a lamer about iTextSharp 😦

        Fredrik

        July 2, 2009 at 6:48 am

  10. Hello, I imported the htmltopdfbuider to my web project, get multiple errors, such as this._Pages.AsEnumerable() … does not contain a definition for AsEnumerable.

    Can you let me know why and how to fix it? thanks

    xun

    June 30, 2009 at 2:14 pm

    • If you don’t have a reference to System.Data.Linq in your project then you won’t have access to the .AsEnumerable() method.

      You might be able to rewrite that function to not require Linq, but that is most likely your issue.

      webdev_hb

      June 30, 2009 at 2:23 pm

      • Thanks. I included System.Data.Linq. However this line keeps giving me error

        public event RenderEvent BeforeRender = (writer, document) => { };

        Error message says ) expected

        Can you advise again? Thanks so much

        xun

        June 30, 2009 at 3:31 pm

  11. Are you using .NET 2.0?

    I think you should use …

    public event RenderEvent BeginRender = delegate(PdfWriter writer, Document document) { };

    Probably not the best way to do this, but it should get you going.

    webdev_hb

    June 30, 2009 at 3:50 pm

    • I am using vs 2008, framework 3.5. I changed that line of code, still gets a lot of errors. How should I change my settings? I am sorry keep getting back to you with all of these trivial errors,

      Error 1 ‘System.Collections.Generic.List’ does not contain a definition for ‘AsEnumerable’ C:\Inetpub\DoverMetal\App_Code\HtmlToPdfBuilder.cs
      Error 2 ‘PDFBuilder.HtmlToPdfBuilder’ does not contain a definition for ‘BeforeRender’ C:\Inetpub\DoverMetal\App_Code\HtmlToPdfBuilder.cs
      Error 3 ‘PDFBuilder.HtmlToPdfBuilder’ does not contain a definition for ‘BeforeRender’ C:\Inetpub\DoverMetal\App_Code\HtmlToPdfBuilder.cs
      Error 4 The type or namespace name ‘var’ could not be found (are you missing a using directive or an assembly reference?) C:\Inetpub\DoverMetal\App_Code\HtmlToPdfBuilder.cs
      Error 5 Cannot convert type ‘var’ to ‘iTextSharp.text.IElement’ C:\Inetpub\DoverMetal\App_Code\HtmlToPdfBuilder.cs

      xun

      June 30, 2009 at 4:32 pm

  12. xun, please leave a comment and fill out the e-mail address and I’ll e-mail you directly so I can help you.

    My best guess is that you don’t have the System.Data.Linq namespace added to the top of your file, but that’s just a guess.

    I’ll e-mail you once I get your address. Thanks!

    webdev_hb

    June 30, 2009 at 4:55 pm

    • Hi there,
      I was getting the same problems as Xun, I need to try and run this in a 2.0 project. Any chance you could mail me whatever you sent him please?
      Thanks

      MJ

      July 17, 2009 at 6:58 am

      • I never heard back from Xun so I couldn’t help him.

        I’m thinking that the problem is that in the code I wrote an event like this…

        public event RenderEvent BeforeRender = (writer, document) => { };

        I was thinking I’d just define it like that so it wouldn’t even be null (not sure why I thought that made sense)

        In any case, you could set that to null and then find the places where that event is called and change it so that it checks to see if it is null before it call the method

        if (this.BeforeRender is RenderEvent) {
        //…
        }

        webdev_hb

        July 17, 2009 at 9:20 am

  13. Thanks. I did add System.Data.Linq both in the header part, and the add reference part.

    Appreciate your tireless and timely help

    xun

    June 30, 2009 at 5:00 pm

  14. did you get my email? Thanks a lot

    xun

    June 30, 2009 at 5:13 pm

  15. Hi,

    i have some trouble adding images from a relative path. i’ve a webpage which includes images an stylesheets just by using “includes/style.css” or “images/image1.jpg”. But this doesn’t work because the HTMLBuilder always tries to read out from an absolute path like “c:\includes/style.css”. Any idea how to fix it?

    Thanks a lot – and up to this little problem great work!! 🙂
    Greets from Germany
    Seb916

    Seb916

    July 8, 2009 at 3:51 pm

    • That is an interesting problem, I’m not sure I have a great solution for you though…

      You might try using a regular expression similar to…

      src=\”(?[^\”]+\” (untested…)

      That will find all of your URLs. At that point, you could extract the path out and use Server.MapPath() to try and convert it.

      After you have your new string, just replace it in the document.

      Heads up, if you’re doing Regex replacements, loop backwards – if you change a string in the middle of looping it messes up all the remaining indexes.

      webdev_hb

      July 9, 2009 at 7:28 pm

      • sir i am using iTextSharp to creat a pdf document but i am not able to add images in Cell kindly help me out my code is
        string html1 = html.Replace(“/Namfus/Teachers/img/”, “E:/Skuli_Pdf_Report/img/”);
        if (html1 != null)
        {
        ArrayList elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(html1), null);
        if (elements.Count > 0)
        {

        Chunk ch;
        iTextSharp.text.Image img;

        if (elements[0] is Paragraph)
        foreach (Object obj in (Paragraph)elements[0])
        {

        ch = (Chunk)obj;

        ch.Font = FontFactory.GetFont(Fontstyle.ToString(), fontsize);

        img = ch.GetImage();

        if (img != null)
        {

        img.ScalePercent(imgWidth * 100 / img.Width);
        img.Alignment = iTextSharp.text.Image.ALIGN_RIGHT | iTextSharp.text.Image.ALIGN_TOP;
        elements.Add(img);
        }

        }

        return elements[0] as IElement;
        }

        i am getting error path not found ex.

        Manoj

        October 7, 2009 at 3:30 am

  16. Hi

    I am new to this. I have used this example and it works fine. The only problem i have now is to create the .pdf docs on the fly and open it in a new IE or Mozilla window. I don’t want to write it to the disk.

    Please HEEEEEEELLLLLLLLLLLLLLLLP.

    SolidSnake

    July 13, 2009 at 4:52 am

    • It depends on what you’re using, either WebForms or MVC.

      MVC is super-easy, just do a “File” call from your controller and specify the byte array generated as your file to download.

      If you’re using WebForms then it is going to be only slightly more complicated. You’re going to need to write that byte array to the output stream, change the content type and then set the “content-disposition” for your response. That way it will appear as a download.

      Just look up that phrase and you’ll find all the information you need.

      Good luck

      webdev_hb

      July 13, 2009 at 8:44 am

      • Thanx. for the help

        SolidSnake

        July 16, 2009 at 8:20 am

  17. The wornderful post and was very useful,

    Naga

    July 16, 2009 at 8:05 am

  18. Hi Peter,

    I am getting this working in VB, however I am getting “font size too small: 0″…any ideas? I have looked thru the itext documentation and it talks about setting a font size…but its set in my css…

    thanks!

    Paul

    July 23, 2009 at 4:14 am

    • Interesting – I can’t say I can think of anything off the top of my head. Can you paste some of your code?

      webdev_hb

      July 23, 2009 at 9:53 am

  19. hi, i want to add table in pdf file, for this i m using html tag. but its giving the err.
    please help me how can i insert table in the pdf file by using AppendHtml() method.

    e.g. htm.AppendHtml(” “);

    amit malviya

    July 23, 2009 at 9:20 am

    • The basic structure of the document is created for you as in the html, body, head, etc tags. You only need to append the actual HTML you plan to display when you use AppendHtml()

      Hope that helps

      webdev_hb

      July 23, 2009 at 9:52 am

      • thnx bro… its working.

        Anonymous

        July 23, 2009 at 11:27 pm

  20. Hello Buddy
    It’s great to publish this code

    But as i am using .net2.0 so i am getting error

    for System.Linq namespace is not found.

    and i can’t migrate to .net3.0 so is there any

    way to integrate this cs file into my project.

    If yes then please let me know .

    arun k

    August 1, 2009 at 3:32 am

    • I’ve gotten a couple comments about this – I’m planning on formatting it so it is at least compatible with .NET 2.0.

      Thanks!

      webdev_hb

      August 3, 2009 at 1:52 pm

  21. Hi

    I’ve been trying to convert my HTML to PDF since yesterday. We have a third party toolkit which can be used for converting to PDF, but it’s way more trouble than it’s worth.

    So I looked at iTextSharp. Mine works fine, but as soon as there is a table tag in my html I get this error:
    Unable to cast object of type ‘iTextSharp.text.Paragraph’ to type ‘iTextSharp.text.Table’.

    Does anyone have any idea on what needs to be done to get it to work with a table tag?

    Thanks.

    NeCroFire

    August 5, 2009 at 7:25 am

    • Can you post some code? Are you using the HTML parser that comes with iTextSharp?

      webdev_hb

      August 5, 2009 at 7:42 am

      • HTML parser that comes with iTextSharp.

        This is what my Test html file looks like:

        Untitled Page

        some text

        some more text

        if you remove the table tags it works, but not with the table tags. As long as I don’t use table tags everything works.

        I’m going to download HtmlToPdfBuilder now to make things a little easier.

        Thanks for the speedy reply 🙂

        NeCroFire

        August 5, 2009 at 7:53 am

      • great… the html tags don’t show.

        NeCroFire

        August 5, 2009 at 7:56 am

      • Don’t include the tags like html or body — Just the markup for the document itself.

        webdev_hb

        August 5, 2009 at 8:05 am

      • I tried that, still get same error. Guess I’ll just have to redo it all without using tables. Getting everything to display as nice a table does will be tricky.

        Thanks again.

        NeCroFire

        August 5, 2009 at 8:32 am

      • Strange, I’ve used tables before and I got them to work. I don’t think you can use things like paragraphs with floats to simulate a table.

        Good luck! If you want to post some code I’ll check it out.

        webdev_hb

        August 5, 2009 at 8:46 am

      • NeCroFire

        August 5, 2009 at 9:02 am

      • aahh… now that I’m using your HtmlToPdfBuilder it works. Although I now have a new problem. It does not export the images and every table cell is now in a new line in the pdf file.

        Any ideas??

        NeCroFire

        August 5, 2009 at 10:07 am

      • I hadn’t used images with my PDFs before, but I gave a suggestion to another visitor how he might be able to load them into the document. It is purely a guess but iTextSharp might want to see an “absolute” path instead of “relative”. If you were to parse the document and switch out the paths it might fix that problem.

        For the table, I’m not sure why you’re having a problem… Are you just doing a simple table like so?

        <table>
        <tr>
        <td>Cell #1</td>
        <td>Cell #2</td>
        </tr>
        </table>

        The iTextSharp HTML Parser isn’t that awesome – I was playing around with making my own but hadn’t gotten that far into it.

        webdev_hb

        August 5, 2009 at 10:13 am

      • Yes, that’s what my table is like.

        I get the images from a web service. I have to send a code through and it returns an image URL. I will see what I can do there.

        What I have is a asp:Repeater that lists information about different vehicles. I only want to export everything that’s in the repeater at that moment to PDF file. Getting this data I can do, it’s teh whole HTML to PDF that won’t work like I’d like it to.

        Anyway, here in South Africa it’s already 17:30. Will check in tomorrow.

        Thanks for the help

        NeCroFire

        August 5, 2009 at 10:28 am

  22. hello webdev_hb

    hv u made it compatible with .net2.0 ?

    actually i hv seen this and found it very useful for me but already i have told you ma prob that i can not navigate to .net3.0 or greater version
    so please let me know if you have formatted it with .net2.0.

    waiting for your reply..

    arun k

    August 5, 2009 at 7:49 am

    • I have actually worked on it – I was going to do a blog post this week or next with the code (if it is working :)) — If you leave a comment with your e-mail address I can e-mail you a preview when I finish.

      Thanks!

      webdev_hb

      August 5, 2009 at 8:06 am

      • ya sure buddy
        let me knw the working code

        ma email is aruninnice@gmail.com

        waiting for your mail

        arun k

        August 5, 2009 at 8:21 am

      • Also I’m very interessed in the 2.0 version. Thanks great job!

        karlItaly

        August 22, 2009 at 8:24 am

  23. Hi dude,
    Hope all will be fine. i am using ur class name is ‘HtmlToPdfBuilder.cs’, by this class i m generating PDF successfully. But my problem is i m not able to format tag, the main problem is, not able to give proper height and width to proper cell of table.
    i tried all possibilities, like
    ,,.

    please give me solution, if u have.
    thnx in advance…. Amit Malviya

    amit malviya

    August 10, 2009 at 12:02 am

    • I’ve noticed what you’re talking about – The HTML parser used by iTextSharp isn’t really as good as it could be. Many styles that adjust the document sizes or positions don’t work at all.

      I had been investigating writing my own parser, but for now I’m using the built in version.

      webdev_hb

      August 10, 2009 at 7:59 am

  24. Love the class !!!

    Have you gotten the code adapted to .Net 2.0 without the LINQ reference being required ?

    Joel

    Joel F

    August 11, 2009 at 11:49 am

    • I have a blog post I’m writing about another topic tonight and then I will post it for a fact Friday — sorry for the delay!

      webdev_hb

      August 11, 2009 at 4:42 pm

  25. Great..thanks..

    I was wondering whether you have had any feedback about the ImportStylesheet method, I have traced the code in debug mode and it seems to find the stylesheet and the styles referenced but they don’t seem to be applied when looking at the resulting pdf stream. I am not persisting the pdf to the file system, I am doing a Response.BinaryWrite.

    Thanks for your help

    Joel F

    August 12, 2009 at 8:13 am

    • I know that many of the styles seem to not be supported by the iTextSharp html parser. I’ve only gotten attributes some styles to actually work (like font-color for example)

      Is nothing showing up or is that many of them are missing?

      webdev_hb

      August 12, 2009 at 8:48 am

  26. hello Hugo

    i tried your updated version which is for .net2.0

    but in that i am getting error in this line

    foreach (var item in (IEnumerable)HTMLWorker.ParseToList(reader, this._Styles))
    {
    document.Add((IElement)item);
    }

    Error “The type or namespace name ‘var’ could not be found (are you missing a using directive or an assembly reference?)”

    arun

    August 24, 2009 at 1:54 am

    • I made a change to the code and uploaded to new version if you’d like to give it a try – I targeted .NET 2.0 in the project file, but I guess Visual Studio 2008 fixed that part for me when it compiled 😐

      webdev_hb

      August 24, 2009 at 7:13 am

      • Thx for your reply buddy

        but as i already told you that i am using VS2005

        so its giving me error

        As u r using VS2008
        it obvious it’d give u error…
        🙂

        so how can i resolve this ?

        arun

        August 24, 2009 at 7:17 am

      • I uploaded new code as of this morning – Did you try that yet?

        webdev_hb

        August 24, 2009 at 7:44 am

  27. yes buddy i tried that code

    but still i am getting the error…

    in my today posts i hv mentioned where i am getting the error.

    arun

    August 24, 2009 at 7:57 am

    • I meant that I already did a fix for the error you posted this morning and uploaded the new code – that line that was giving you trouble before should read something like

      foreach (object item in HTMLWorker.ParseToList(reader, this._Styles)) {
      ...

      Is this the same in your version?

      webdev_hb

      August 24, 2009 at 8:31 am

      • hello Hugo,

        I tried with your updated code and

        i was getting error that

        ‘The number of columns in PdfPTable constructor must be greater than zero.’

        when i append the html table to the HtmlToPdfBuilder.

        arun k

        August 28, 2009 at 4:54 am

      • Are you doing only the HTML for the body or are you including the <html>, <body> tags as well? If so, that part is done automatically, only use the HTML for the document itself.

        webdev_hb

        August 28, 2009 at 1:24 pm

  28. hello,

    does somebody know if i could use css inheritance? i couldnt make it work…

    thanks!!

    Rothariger

    August 28, 2009 at 1:22 pm

    • Thats a good question – I doubt that the iTextSharp HTML parser is going to do well with that but I’ll have to check it out.

      The HTML parser in iTextSharp excludes a lot of styles like height, width, margins, etc… if the style has to do with positioning or sizes, chances are it doesn’t work anyways.

      webdev_hb

      August 28, 2009 at 1:25 pm

      • ouchh…

        i guess almost all the css i apply is for positioning and sizing…

        im trying to export a page made it with http://www.extjs.com to pdf… 😦

        thanks anyway!!!

        Rothariger

        August 28, 2009 at 1:42 pm

  29. Thanks for your great effort.I tried to reproduce below html code passing as an html string.But it was not working. Can you advice me how to generate PDF file with using this HTML.

    Title First Name Surname

    House Number Street Name

    Town

    County

    Postcode

    dd/mm/yyyy

    contact us

    New Business & Renewals
    0870 121 7590
    Mon – Fri 8.30am –5.30pm,
    Sat 9am – 12pm

    If you need to make a claim
    Davies Managed Systems (DMS)
    0870 420 1261

    Visit us online
    http://www.test.co.uk

    Quote Policy number
    Insert Policy Num

    Dear Title Surname

    Main Heading

    Thank you for choosing us for your activity insurance needs.
    Please check your policy documents

    Your activity equipment schedule
    – which outlines your cover, please ensure it meets with your requirements.

    Your insurance certificate
    – includes the policy wording, which forms part of your contract of insurance.

    We have also included
    – a policy summary, demands and needs statement and our terms of business.

    You are responsible for the accuracy of your
    travel cover so please notify us if any of the information
    is incorrect
    as failure to do so could jeopardise a claim.

    Please call us on 0870 121 7590 to advise us of any changes.
    What you need to know
    From time to time we may decide to change the insurer for the
    insurance products we offer. In such circumstances we will
    write to you no less than 21 days before your insurance expires
    with details of any changes to your insurance.
    Yours sincerely
     

    ABC.SilvsaBusiness Manager

    regulated by the Financial Services Authority.
    Registered office:

    Waiting for your help.

    Prasad

    September 7, 2009 at 1:21 am

    • Sorry.I saw my HTML code has not sent correctly.This is my C# HTML string.

      “\r\n”
      +” \r\n”
      +” \r\n”
      +” Title First Name Surname\r\n”
      +” \r\n”
      +” House Number Street Name\r\n”
      +” \r\n”
      +” Town\r\n”
      +” \r\n”
      +” County\r\n”
      +” \r\n”
      +” Postcode\r\n”
      +” \r\n”
      +” \r\n”
      +” \r\n”
      +” dd/mm/yyyy\r\n”
      +” \r\n”
      +” \r\n”
      +” \r\n”
      +” \r\n”
      +” \r\n”
      +” contact us\r\n”
      +” \r\n”
      +” New Business & Renewals\r\n”
      +” 0870 121 7590\r\n”
      +” Mon – Fri 8.30am –5.30pm, \r\n”
      +” Sat 9am – 12pm\r\n”
      +” \r\n”
      +” If you need to make a claim\r\n”
      +” Davies Managed Systems (DMS) \r\n”
      +” 0870 420 1261\r\n”
      +” \r\n”
      +” Visit us online\r\n”
      +” http://www.test.co.uk\r\n”
      +” Quote Policy number\r\n”
      +” Insert Policy Num\r\n”
      +” \r\n”
      +” \r\n”
      +” Dear Title Surname\r\n”
      +” \r\n”
      +” Main Heading\r\n”
      +” \r\n”
      +” Thank you for choosing us for your activity insurance needs.\r\n”
      +” Please check your policy documents\r\n”
      +” ♦\r\n”
      +” \r\n”
      +” Your activity equipment schedule\r\n”
      +” – which outlines your cover, please ensure it meets with your requirements.\r\n”
      +” \r\n”
      +” \r\n”
      +” ♦\r\n”
      +” \r\n”
      +” Your insurance certificate\r\n”
      +” – includes the policy wording, which forms part of your contract of insurance. \r\n”
      +” \r\n”
      +” \r\n”
      +” ♦\r\n”
      +” \r\n”
      +” We have also included\r\n”
      +” – a policy summary, demands and needs statement and our terms of business.\r\n”
      +” \r\n”
      +” You are responsible for the accuracy of your \r\n”
      +” travel cover so please notify us if any of the information\r\n”
      +” is incorrect
      as failure to do so could jeopardise a claim. \r\n”
      +” \r\n”
      +” Please call us on 0870 121 7590 to advise us of any changes. \r\n”
      +” What you need to know\r\n”
      +” From time to time we may decide to change the insurer for the \r\n”
      +” insurance products we offer. In such circumstances we will \r\n”
      +” write to you no less than 21 days before your insurance expires\r\n”
      +” with details of any changes to your insurance. \r\n”
      +” Yours sincerely\r\n”
      +”  \r\n”
      +” \r\n”
      +” ABC.SilvsaBusiness Manager\r\n”
      +” \r\n”
      +” \r\n”
      +” \r\n”
      +” \r\n”
      +” \r\n”
      +” \r\n”
      +” \r\n”
      +” regulated by the Financial Services Authority. \r\n”
      +” Registered office: \r\n”
      +” \r\n”
      +” \r\n”
      +” “;

      Prasad

      September 7, 2009 at 1:23 am

  30. Sorry. Both my codes were not sent perfectly.Can I have your E mail Address. I will send my code with my Issue.

    Thank You.

    Prasad

    September 7, 2009 at 1:26 am

    • If you’re using new lines and tabs then this class isn’t going to help you out very much. You need to use HTML markup to generate your content… so for example…


      HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.LETTER);
      HtmlPdfPage first = builder.AddPage();
      first.AppendHtml("<p>Some Content</p>");

      If you’re still stuck then you can contact me here – good luck!

      webdev_hb

      September 7, 2009 at 10:11 am

      • Hi..Can I add entire html page source with AppendHtml method?

        Also can I add dynamic image source as parameter for AppendHtml method?

        Thanks

        Bhupendra

        Bhupendra

        May 21, 2010 at 3:03 pm

  31. No Feedback?

    Prasad

    September 7, 2009 at 11:37 pm

  32. i am using your code to convert my html code to pdf. but i am getting only the content but not the controls on that page (like textboxs,etc).

    i need your help. its urgent

    Anonymous

    September 8, 2009 at 7:13 am

  33. Great class! however I have a problem with Polish letters (ą, ż, ź, ć, ó, ę, ł). I have the example working well with Polish letters, but It doesn’t work with tables (similarly how wrote “NeCroFire”).

    Is It possible to add the embleded font into Your class?

    It is a code which works with Polish letters

    BaseFont arial = BaseFont.CreateFont(@”C:\WINDOWS\Fonts\arial.ttf”, “iso-8859-2”, BaseFont.EMBEDDED);

    //creation of a document-object
    Document document = new Document();
    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Load(“../../Raport/test.html”);
    try
    {
    PdfWriter.GetInstance(document, new FileStream(“generowanyDokument.pdf”, FileMode.Create));

    //header
    document.Add(new Header(Markup.HTML_ATTR_STYLESHEET, string.Empty));
    document.Open();

    //create a parser
    ITextHandler xmlHandler = new ITextHandler(document, new HtmlTagMap(), arial);
    xmlHandler.Parse(xmlDocument);//”generowanyDokument.xml”);
    document.Close();
    }
    catch (Exception e)
    {
    }

    Thanks

    felek

    September 16, 2009 at 8:07 am

  34. Hello,
    Let me thank you first for the great piece of code and having will to share it.
    I am facing stylesheet no-effect issue.
    I used your code as this, my strURL= “http://localhost/Test/Common/UserControls/Lab/HtmltoPDF/HTMLPage.htm”
    its content is

    Test message
    address

    where .addressbook
    {
    width: 600px;
    font: 82% arial,helvetica, “Nimbus Sans L” ,sans-serif;
    border: solid 1px #666;
    margin-left: 30px;
    text-decoration: underline;
    color: #CC0000;
    }

    NOTE: I am unable to get the underline and red color of the font.
    I made changes in..HtmlToPdfBuilder as
    public void ImportStylesheet(string path) {
    //load the file
    string content = “”;
    try
    {
    content = File.ReadAllText(path);
    }
    catch
    {
    WebClient objclient = new WebClient();
    content = objclient.DownloadString(path);
    content= content.Replace(“\n”,””);
    content= content.Replace(“\r”,””);
    content= content.Replace(“P”,””);
    content= content.Replace(“\””,””);
    content = content.Replace(“\t”, “”);
    }

    //use a little regular expression magic
    foreach (Match match in Regex.Matches(content, HtmlToPdfBuilder.REGEX_GET_STYLES)) {
    string selector = match.Groups[HtmlToPdfBuilder.REGEX_GROUP_SELECTOR].Value;
    string style = match.Groups[HtmlToPdfBuilder.REGEX_GROUP_STYLE].Value;
    this.AddStyle(selector, style);
    }

    }

    But still no luck…Infact it is adding the style tags in the document running without any prob but just no effect of style sheet. Will you suggest what might be the reason.

    Final code is here:

    GeneratePDFwithStyleSheet(string strURL)
    {
    string strBodycontent= GetBodyContent(strURL);
    strBodycontent = strBodycontent.Replace(“\n”, “”);
    strBodycontent = strBodycontent.Replace(“\r”, “”);
    string a = “\””;
    strBodycontent = strBodycontent.Replace(a, “‘”);
    strBodycontent = strBodycontent.Replace(“P”, “”);
    strBodycontent = strBodycontent.Replace(“\t”, “”);

    PDFBuilder.HtmlToPdfBuilder builder = new PDFBuilder.HtmlToPdfBuilder(PageSize.LETTER);
    PDFBuilder.HtmlPdfPage first = builder.AddPage();
    first.AppendHtml(strBodycontent);
    //builder.ImportStylesheet(“http://localhost/OnActionC/Common/_assets/css/addressbook.css”);
    builder.ImportStylesheet(@”C:\inetpub\wwwroot\onactionc\Common\_assets\css\addressbook.css”);
    byte[] file = builder.RenderPdf();
    File.WriteAllBytes(@”c:\pdf\Chap084.pdf”, file);
    }

    Prashanth

    September 23, 2009 at 6:55 am

  35. sorry please read it as

    <img alt=”” src=”http://localhost/onactionc/common/_assets/img/column-bg-lefts.png” />
    Test message
    <span class=”addressbook”> address </span>

    Prashanth

    September 23, 2009 at 6:58 am

  36. Hello,
    I think I got the solution, the contents of css has
    “” and we need to remove “.” in css contents I used this. now stylesheet is implemented successfully.

    content = content.Replace(“”, “”);
    content = content.Replace(“.”, “”);
    content = content.Substring(3,content.Length-3);

    Thanks once again.

    Prashanth

    September 23, 2009 at 8:21 am

    • Prashanth,

      I’m not really sure about the different character types. It could be a iTextSharp thing since I don’t believe I’m making any changes to the encoding of the text being used.

      webdev_hb

      September 24, 2009 at 9:34 pm

    • HiPrashanth,
      do you want to say that you could render pdf from html with css stylesheet? For me it is not working 😐
      I use itextsharp.dll 5.0.0 and HtmlToPdfBuilder_2_0.cs . 😕

      Adrian

      November 3, 2010 at 5:55 am

  37. This:

    byte[] file = builder.RenderPdf();
    File.WriteAllBytes(@”c:\\output\\final.pdf”, file);

    Causes this error: “Could not find a part of the path ‘c:\output\final.pdf’.”

    Is there a way to cause this to load in a browser? I don’t need to save anything.

    Thanks…

    Jaden

    September 24, 2009 at 1:01 pm

    • If you’re using ASP.NET MVC then you can simply return the bytes using the controller method “File”

      If not you can always use the Response.OutputStream and write the content directly.

      webdev_hb

      September 24, 2009 at 9:33 pm

  38. I keep this error when running…
    Could not load type ‘Voucher.Core.PdfBuilder.HtmlToPdfBuilder’ from assembly ‘Voucher.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null’ because the method ‘get_PageSize’ has no implementation (no RVA).

    Where I’ve loaded the PdfBuilder into my own Custom C# Class Library for the application I’m trying to use the PdfBuilder.

    Regards,
    Neil

    Neil

    September 24, 2009 at 4:09 pm

    • You have loaded iTextSharp into your project as well?

      webdev_hb

      September 24, 2009 at 9:30 pm

      • Yes I have loaded the dll as a reference into the main project as well.

        Neil

        September 25, 2009 at 8:53 am

      • Any updates?

        Neil

        September 30, 2009 at 2:31 pm

  39. Fantastic code this has really helped me to make use of itextsharp using an ajax html editor. I am using it for a mailing application that i am building. Many thanks for sharing this.

    I am looping though a table with a datareader to generate letters is there a way i can continuously append the html to multiple pages then insert a page break after each database record. (hope that makes sense). Any help would be greatly appreciated also how easy would it be to stream the pdf instead of creating a physical file.

    justanotherdotnetblog

    October 1, 2009 at 1:53 am

  40. I really like how easy this wrapper is to use.
    I am having difficulty displaying images in here.
    Is it easier to build the HTML from an XMLHTTP request or just form it inline here? Pathing of images seems to be an issue I’m having. Any thoughts?

    John

    October 12, 2009 at 2:49 pm

  41. Hi:

    i am using “HtmlToPdfBuilder.cs” to convert html to pdf successfully, but i got some problem and need your help, when my html contain Traditional(Simple) Chinese, how to modify code to get the result correct…great thanks

    shiun

    October 14, 2009 at 1:50 am

  42. Can someone give me a hand with this? My stylesheet has no effect on the pdf. Is there a trick to this? I’m using builder.ImportStylesheet(my style)

    It loads but no effect to my document. CSS is

    .style1
    {
    border: thin solid #C0C0C0;
    width: 100%;
    }

    .style6
    {
    width: 100%;
    background-color: #7DA5E0;
    }

    .style3
    {
    width: 444px;
    border: thin solid #C0C0C0;
    }

    .style7
    {

    border: thin solid #C0C0C0;
    }

    .style4
    {
    width: 119px;
    border: thin solid #C0C0C0;
    }
    .style5
    {
    border: thin solid #C0C0C0;
    width: 142px;
    }
    .style8
    {
    border: thin solid #C0C0C0;
    width: 160px;
    }

    Larry

    October 22, 2009 at 10:20 am

    • did you find any solution for this? I am getting the same problem 😦

      Burhan

      December 17, 2009 at 6:41 am

  43. I use iTextSharp.dll to generate pdf from html in ap.net 2.0. Its successfully generate pdf but not same as my html.

    In my html there is table, but generated pdf layout is not same as html layout.
    How is it possible to make pdf same as my html file.

    Need you advice

    rajesh

    October 30, 2009 at 8:51 am

  44. first of thank you for this tool.
    I use it and like so much.

    but I’ll be crazy. I cann’t change the encoding to iso-8859-9. I made some modification but the result same 😦

    help me please

    engin kırmacı

    November 8, 2009 at 10:25 am

  45. Hello first of thanks , my only problem is to set heights, widths any news about that ?

    ricardo

    November 23, 2009 at 2:14 pm

  46. This is a fantastic article and a really useful class.

    Thanks.

    Jonny Pioww

    December 1, 2009 at 7:02 am

  47. the regular expression in the code is wrong. The one in the sorce code do not consider the tag with one letter like “p” or “a”.
    the correct(real magic) one is :
    private const string REGEX_GET_STYLES = @”(?[^\{\s]*\w*(\s\[^\{\s]*)?)\s?\{(?[^\}]*)\}”;

    Alessio

    December 10, 2009 at 11:26 am

  48. Dear webdev_hb,

    thank you for such useful article, its make very easy to convert the html page into pdf, with almost all features, the only problem i get is image on fly, i am generating a barcode image and inserted in html, its working fine in html but not adding in pdf, please if you know the solution, let me know.

    i am using neodynamic barcode image.

    thank you very much 🙂

    Qureshi

    December 20, 2009 at 2:21 am

  49. Hi,

    Thank you for your effort. Is it possible to add an image as the background. I tried as below. But seems not working.

    builder.AddStyle(“body”, “background-image: url(‘http://www.google.com/logos/holiday09_3.gif’);background-repeat: no-repeat;height: 161px;width: 267px;margin: 0;”);

    can you advice me on this.

    tnx.

    Prasad

    December 23, 2009 at 8:12 am

    • Not an answer for my one?

      Prasad

      January 5, 2010 at 12:27 am

  50. Hi,
    Thnaks dear its a really nice code and helpfull.

    Umang Soni

    December 30, 2009 at 6:26 am

  51. hi all i am using iTextSharp to create a pdf report from database and using htmlworker class to parse it it works good but the problume in images when text is before the images in database so that its overlapping inside the cell my code is to parse html is below

    public static IElement GetLinSapcing(string html, float imgWidth, int fontsize, string Fontstyle, Document document, int sizeofline)// new added
    {
    int height = 0;
    try
    {

    string path = HttpContext.Current.Server.MapPath(“.”) + “/img/”;
    html = html.Replace(“/Namfus/Teachers/img/”, HttpContext.Current.Server.HtmlEncode(path).Trim());
    html = html.Replace(“%C3%”, “”);
    string parttern = “”;
    if (Regex.IsMatch(html, parttern))
    {
    html = Regex.Replace(html, parttern, PdfHelper.Creatline(sizeofline));
    }
    StreamReader sReader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(html)), Encoding.UTF8);
    ArrayList elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sReader, null);

    Paragraph elements2 = new Paragraph();

    foreach (Paragraph ar in elements)
    {
    foreach (iTextSharp.text.Chunk chk in ar.Chunks)
    {

    elements2.Add(chk);

    }

    }
    ArrayList elements3 = new ArrayList();
    elements3.Add(elements2 as IElement);
    IElement objIElement = elements3[0] as IElement; ;
    if (elements.Count > 0)
    {

    foreach (IElement element in elements3)
    {
    Chunk ch;
    Phrase phras;
    iTextSharp.text.Image img;

    if (element is Phrase)
    {
    Phrase ph = (Phrase)element;
    // ph.Leading = 50;
    elements.Add(ph);

    foreach (Object obj in (Phrase)(element))
    {

    ch = (Chunk)obj;
    ph.Font = FontFactory.GetFont(Fontstyle.ToString(), fontsize, ch.Font.Style);

    img = ch.GetImage();
    // ch.SetCharacterSpacing(10.2f);

    if (img != null)
    {
    int width = int.Parse(img.Width.ToString());
    img.BackgroundColor = BaseColor.WHITE;
    img.ScaleToFit(((img.Width + 6) / 2), ((img.Height + 6) / 2));
    img.Alignment = iTextSharp.text.Image.ALIGN_RIGHT | iTextSharp.text.Image.TEXTWRAP|iTextSharp.text.Image.UNDERLYING;

    // img.ScalePercent(50f);
    //img.Top = 0.1f;
    }

    }
    }

    }
    return objIElement;
    }
    }
    catch { }

    return GetSimpleText(html, fontsize);
    }

    if any one find solution kindly reply thank

    regards manoj barmola

    manoj

    December 30, 2009 at 6:39 am

  52. Dear Sir,

    i work on pdf and found that its not support
    style=”border-width: 1px;”

    its support
    border=”1″

    so what may be the next option for that ?

    thanks & regards,
    zubair

    zubair

    December 31, 2009 at 5:15 pm

  53. Great post Hugo!

    Has anyone figured out the margin issue? My left and right margins are messed up (the left and right sides of my document are truncated). I tried tweaking the code to force a one inch margin.

    Document document = new Document(this.PageSize, 72, 72, 72, 72);

    But that did not fix anything.

    Another noteworthy tip is to use the Tidy.dll library to cleanup HTML (the HTML I am using comes from an XML transformation that I have not control of). I was getting a ‘no stack’ error on this call

    foreach (var item in (IEnumerable)HTMLWorker.ParseToList(reader, this._Styles)) {

    Once I cleaned up the HTML I was able to at least get the HTML to render. I used this .NET wrapper to call the tidy.dll (http://markbeaton.com/SoftwareInfo.aspx?ID=81a0ecd0-c41c-48da-8a39-f10c8aa3f931)

    Mark Pitts

    January 2, 2010 at 9:15 pm

    • Interesting. What does it tell me if I rotate to landscape, as in the following,

      HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.LETTER.Rotate());

      …my left and right margins are appearing.

      This leads me suspect it is not a margin issue after all.

      Any suggestions would be appreciated.

      Mark Pitts

      January 3, 2010 at 9:37 pm

      • I was able to get my HTML to render correctly in the portrait orientation.

        The fix was to reduce the width of the tables by 150pt’s (from 680 to 530).

        QN: Is there a less invasive way to reduce the HTML before feeding it into htmltopdfbuilder? Maybe I need to send to produce an HTML image then reduce the image.

        Cheers!

        Mark Pitts

        January 5, 2010 at 7:59 am

    • try this

      Document document = new Document(this.PageSize.A4, 72, 72, 72, 72);
      and u can set the margin also by
      document.setmargin(int top,int bottom,int left,int right);

      manoj

      January 5, 2010 at 6:08 am

      • I had already tried Document document = new Document(this.PageSize.A4, 72, 72, 72, 72); And setmargins has no affect.

        That is why I tried rotating, which at least gives me the left and right side of my document. It is for this reason that I suspect something else is going on. It’s almost as if the

        Below is the snippet that works in landscape:

        HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.LETTER.Rotate());
        HtmlPdfPage first = builder.AddPage();
        first.AppendHtml(source); // where source is my entire HTML document
        byte[] file = builder.RenderPdf();

        Any suggestions would be appreciated to get the HTML document to convert to portrait.

        Anonymous

        January 5, 2010 at 6:54 am

  54. I am new to C#. I have my aspx page rendering correctly. Now I have to have that same page saved to a pdf. One page. How do I get this page to output to pdf?
    HtmlToPdfBuilder builder = new HtmlToPdfBuilder(iTextSharp.text.PageSize.LETTER);
    HtmlPdfPage first = builder.AddPage();
    first.AppendHtml(“What do I add here?”);
    byte[] file = builder.RenderPdf();
    File.WriteAllBytes(“c:\\output\\final.pdf”, file);

    Appreciate all the assist on this.

    Chris York

    January 3, 2010 at 4:30 pm

    • Consider using the InternetExplorer object to capture the rendered HTML.

      SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
      object Empty = 0;

      object URL = “your URL”;

      // override BeforeNavigate2 event
      ie.BeforeNavigate2 += new
      SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(
      e.OnBeforeNavigate2);

      ie.Navigate2(ref URL, ref Empty, ref Empty, ref Empty, ref Empty);
      while (ie.Busy) { }

      // source contains the rendered HTML
      string source = ((mshtml.HTMLHtmlElementClass)((mshtml.HTMLDocumentClass)(((SHDocVw.InternetExplorerClass)(ie)).Document)).documentElement).outerHTML;

      Mark Pitts

      January 3, 2010 at 7:06 pm

  55. Great Job Man.
    But A lot of CSS like background-color, background-image etc are not working.
    What should be done?

    Hello

    January 6, 2010 at 6:19 am

  56. In your article you indicate that you can reference a CSS class (.classname) however, when I try it, the css class seems to be ignored upon rendering the PDF.

    Also, there does not seem to be any support for CSS such as: border: solid 1px black; etc….

    Is there a bug in the C# which causes class names and certain CSS elements to be ignored? Or is there simply no current support for some CSS elements?

    Bill Miles

    January 6, 2010 at 11:44 pm

  57. CSS is very limited with iTextSharp or more specifically the HTML parser for it — The HtmlPdfBuilder acts as a wrapper to the HTML support for iTextSharp, but that is about it.

    Since this post is getting so much attention I’ll start looking over any possible ways to fix some of the CSS issues

    webdev_hb

    January 7, 2010 at 12:32 am

  58. Can you please upload full project.
    It will make testing much easier.
    Thank you!

    Anonymous

    January 22, 2010 at 5:10 am

  59. Although I was able to get HtmlToPdfBuilder to work, I discovered that portions (entire tr tags) of my HTML were getting dropped.

    Without spending too much time, I suspect the issue may or may not be relate to nested table tags.

    HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.LETTER.Rotate());
    HtmlPdfPage first = builder.AddPage();
    first.AppendHtml(source);
    byte[] file = builder.RenderPdf();

    At this point in my project I had no choice but to fall back on a commercial product (AbcPdf)

    Mark Pitts

    January 22, 2010 at 6:26 am

  60. hi can anyone tell me how to convert the asp.net page that contains textbox,radiobutton,label,checkbox in to pdf

    Anonymous

    January 27, 2010 at 8:42 am

  61. Thanks very much.

    Very nice article

    but i m facing applying CSS stylesheets.

    Vishal Patil

    January 28, 2010 at 3:37 am

  62. Hi,

    First of all thank you so much for this great job.
    I’m trying to use the .net 2.0 version but I have an error with it : CS0501: ‘PDFBuilder.HtmlToPdfBuilder.PageSize.get’ must declare a body because it is not marked abstract or extern – on public Rectangle PageSize { get; set; }-

    Got an idea of what I’ve done wrong ?

    mat

    January 29, 2010 at 6:50 am

    • /*
      * HtmlToPdfBuilder.cs (for .NET 2.0)
      * ———————————
      * Hugo Bonacci (webdev_hb@yahoo.com)
      * http://www.hugoware.net
      */

      using System;
      using System.Collections.Generic;
      using System.Text;
      using iTextSharp.text;
      using System.IO;
      using iTextSharp.text.pdf;
      using iTextSharp.text.html.simpleparser;
      using System.Collections;
      using iTextSharp.text.html;
      using System.Text.RegularExpressions;
      using System.Data.Sql;

      namespace PDFBuilder {

      #region HtmlToPdfBuilder Class

      ///
      /// Simplifies generating HTML into a PDF file
      ///
      public class HtmlToPdfBuilder {

      #region Constants

      private const string STYLE_DEFAULT_TYPE = “style”;
      private const string DOCUMENT_HTML_START = “”;
      private const string DOCUMENT_HTML_END = “”;
      private const string REGEX_GROUP_SELECTOR = “selector”;
      private const string REGEX_GROUP_STYLE = “style”;

      //amazing regular expression magic
      private const string REGEX_GET_STYLES = @”(?[^\{\s]+\w+(\s\[^\{\s]+)?)\s?\{(?[^\}]*)\}”;

      #endregion

      #region Constructors

      ///
      /// Creates a new PDF document template. Use PageSizes.{DocumentSize}
      ///
      public HtmlToPdfBuilder(Rectangle size) {
      this.PageSize = size;
      this._Pages = new List();
      this._Styles = new StyleSheet();
      }

      #endregion

      #region Delegates

      ///
      /// Method to override to have additional control over the document
      ///
      public event RenderEvent BeforeRender;

      ///
      /// Method to override to have additional control over the document
      ///
      public event RenderEvent AfterRender;

      #endregion

      #region Properties

      ///
      /// The page size to make this document
      ///
      public Rectangle PageSize
      {
      get
      {
      return this._Pagesize;
      }
      set
      {
      }
      }

      ///
      /// Returns the page at the specified index
      ///
      public HtmlPdfPage this[int index] {
      get {
      return this._Pages[index];
      }
      }

      ///
      /// Returns a list of the pages available
      ///
      public HtmlPdfPage[] Pages {
      get {
      return this._Pages.ToArray();
      }
      }

      #endregion

      #region Members

      private List _Pages;
      private StyleSheet _Styles;
      private Rectangle _Pagesize;

      #endregion

      #region Working With The Document

      ///
      /// Appends and returns a new page for this document
      ///
      public HtmlPdfPage AddPage() {
      HtmlPdfPage page = new HtmlPdfPage();
      this._Pages.Add(page);
      return page;
      }

      ///
      /// Removes the page from the document
      ///
      public void RemovePage(HtmlPdfPage page) {
      this._Pages.Remove(page);
      }

      ///
      /// Appends a style for this sheet
      ///
      public void AddStyle(string selector, string styles) {
      this._Styles.LoadTagStyle(selector, HtmlToPdfBuilder.STYLE_DEFAULT_TYPE, styles);
      }

      ///
      /// Imports a stylesheet into the document
      ///
      public void ImportStylesheet(string path) {

      //load the file
      string content = File.ReadAllText(path);

      //use a little regular expression magic
      foreach (Match match in Regex.Matches(content, HtmlToPdfBuilder.REGEX_GET_STYLES)) {
      string selector = match.Groups[HtmlToPdfBuilder.REGEX_GROUP_SELECTOR].Value;
      string style = match.Groups[HtmlToPdfBuilder.REGEX_GROUP_STYLE].Value;
      this.AddStyle(selector, style);
      }

      }

      #endregion

      #region Document Navigation

      ///
      /// Moves a page before another
      ///
      public void InsertBefore(HtmlPdfPage page, HtmlPdfPage before) {
      this._Pages.Remove(page);
      this._Pages.Insert(
      Math.Max(this._Pages.IndexOf(before), 0),
      page);
      }

      ///
      /// Moves a page after another
      ///
      public void InsertAfter(HtmlPdfPage page, HtmlPdfPage after) {
      this._Pages.Remove(page);
      this._Pages.Insert(
      Math.Min(this._Pages.IndexOf(after) + 1, this._Pages.Count),
      page);
      }

      #endregion

      #region Rendering The Document

      ///
      /// Renders the PDF to an array of bytes
      ///
      public byte[] RenderPdf() {

      //Document is inbuilt class, available in iTextSharp
      MemoryStream file = new MemoryStream();
      Document document = new Document();
      PdfWriter writer = PdfWriter.GetInstance(document, file);

      //allow modifications of the document
      if (this.BeforeRender is RenderEvent) {
      this.BeforeRender(writer, document);
      }

      //header
      document.Add(new Header(Markup.HTML_ATTR_STYLESHEET, string.Empty));
      document.Open();

      //render each page that has been added
      foreach (HtmlPdfPage page in this._Pages) {
      document.NewPage();

      //generate this page of text
      MemoryStream output = new MemoryStream();
      StreamWriter html = new StreamWriter(output, Encoding.UTF8);

      //get the page output
      html.Write(string.Concat(HtmlToPdfBuilder.DOCUMENT_HTML_START, page._Html.ToString(), HtmlToPdfBuilder.DOCUMENT_HTML_END));
      html.Close();
      html.Dispose();

      //read the created stream
      MemoryStream generate = new MemoryStream(output.ToArray());
      StreamReader reader = new StreamReader(generate);
      foreach (object item in HTMLWorker.ParseToList(reader, this._Styles)) {
      document.Add((IElement)item);
      }

      //cleanup these streams
      html.Dispose();
      reader.Dispose();
      output.Dispose();
      generate.Dispose();

      }

      //after rendering
      if (this.AfterRender is RenderEvent) {
      this.AfterRender(writer, document);
      }

      //return the rendered PDF
      document.Close();
      return file.ToArray();

      }

      #endregion

      }

      #endregion

      #region HtmlPdfPage Class

      ///
      /// A page to insert into a HtmlToPdfBuilder Class
      ///
      public class HtmlPdfPage {

      #region Constructors

      ///
      /// The default information for this page
      ///
      public HtmlPdfPage() {
      this._Html = new StringBuilder();
      }

      #endregion

      #region Fields

      //parts for generating the page
      internal StringBuilder _Html;

      #endregion

      #region Working With The Html

      ///
      /// Appends the formatted HTML onto a page
      ///
      public virtual void AppendHtml(string content, params object[] values) {
      this._Html.AppendFormat(content, values);
      }

      #endregion

      }

      #endregion

      #region Rendering Delegate

      ///
      /// Delegate for rendering events
      ///
      public delegate void RenderEvent(PdfWriter writer, Document document);

      #endregion

      }

      ////////////***************///////////
      Try to simply copy the above .cs code for vs2.0
      I modified this line of code from the previously downloaded .cs code.
      private Rectangle _Pagesize;
      public Rectangle PageSize
      {
      get
      {
      return this._Pagesize;
      }
      set
      {
      }
      }
      Document document = new Document();
      //////////////*********///////////
      Enjoy a lot by getting the solution.

      Venkatesh

      February 25, 2010 at 1:36 am

  63. Love this class, very nice job

    Mikkel Holck Madsen

    February 2, 2010 at 2:46 am

  64. I am facing problems with the stylesheet. It is not supporting most of the tags like p,a etc

    Prashant Gupta

    February 8, 2010 at 5:29 am

  65. Hi,

    I have a requirement to validate if certain html fits on a pdf doc in a single page … Here is what i wrote :

    ——–html
    ======================================================

    Check with document
    0
    0
    562
    56

    <asp:textbox runat="server" id="lblText" textmode="MultiLine" text="Test Headingline 1 xyz xyz xyz xyz line 1 xyz xyz xyz xyz line 1 xyz xyz xyz xyz ” rows = “20” width=”400px”>

    ———- code behind
    private void GeneratePDF()
    {
    try
    {

    // convert the html to pdf //
    byte[] file = ConvertToPDF();

    // check to see if the htmlcontent fits on the pdf //
    GetNumberOfPDFPages(file);

    // not needed, only for verification //
    WriteFileToFileForVerification(file);
    }
    catch (Exception ex)
    {
    lblLabel.Text = ex.Message;
    }
    }

    private byte[] ConvertToPDF()
    {
    Rectangle rect = new Rectangle(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text), int.Parse(TextBox3.Text), int.Parse(TextBox4.Text));
    PDFBuilder.HtmlToPdfBuilder builder = new PDFBuilder.HtmlToPdfBuilder(rect);
    PDFBuilder.HtmlPdfPage NewPage;
    NewPage = builder.AddPage();
    NewPage.AppendHtml(lblText.Text.Trim());

    byte[] file = builder.RenderPdf();

    return file;
    }

    private void GetNumberOfPDFPages(byte[] file)
    {
    PdfReader reader = new PdfReader(file);
    lblPageCount.Text = ” No of Pages in the pdf doc : ” + reader.NumberOfPages;
    }

    private void WriteFileToFileForVerification(byte[] file)
    {
    PdfReader rd = new PdfReader(file);

    File.WriteAllBytes(Server.MapPath(“~”) + “/” + “final.pdf”, file);
    }

    — What the above code will do ———
    What the code above will do is create a pdf and check of the number of pages.. If number of pages in the generated document is more than 1 then we notify the user that the html supplied will not fit.

    — The output ——————-
    The pdf gets generated great, but if you look at the pdf, it leaves a huge space on the top of each page and due to this prints only one line and even though this text should fit in one page creates 4 pages with one line each…

    Can anyone please advice me as to why the big space on the top of each page in the document..

    Thanks for any help in advance…

    deryl

    March 8, 2010 at 1:33 pm

  66. Hi,

    Thank you so much for your codes! Totally save my life! Amazing!

    Full credits to you ! Thanks once again. Truly appreciate it.

    Best regards

    Sabrina

    March 13, 2010 at 1:19 pm

  67. if I have html like this:
    sfsdfsdfsdfddsdsfsdfsdfsdf

    can I write code with #total selector
    builder.AddStyle(“#total”, “font-weight:bold;”);

    and expect that style will apply ?? or what selectors are support HtmlToPdfBuilder??

    Winperec

    March 24, 2010 at 6:07 am

  68. Your post help me a lot of understanding how to use HTML to build a pdf file. I have a project need to build some pdf files. I want use some flexible way to modify the template using HTML or XML. But the output pdf file must be in CMYK color. Can I generate a CMYK pdf file using HTML convertion? or I should use XML instead? If XML is needed, how do you have any reference/example for me?

    Wilton

    May 10, 2010 at 7:38 am

  69. This post is very helpful!

    Hi i’m using iTextSharp in my web app but i’m getting the error Font size too small:0

    I’m using 10pt,Arial as standard font size in my pages.

    Thanks.

    Ian Apale

    June 9, 2010 at 1:00 am

  70. This post seems a good start to convert my Html to PDF format, however, there is no VB.NET equivalent of some sort. Even trying developerfusion’s conversion tool still returns errors.

    Can anyone help me out, Thanks.

    jonathan

    June 16, 2010 at 4:15 am

  71. Found a link for how to set column widths in tables. This stopped me pulling my hair out.

    http://old.nabble.com/Setwidths-td24066239.html

    Dim widths() As Integer = {10, 80}

    tbl.SetWidths(widths)

    Jessica

    July 5, 2010 at 3:47 pm

  72. i try –> HtmlToPdfBuilder.cs (for .NET 2.0)
    when i build project i run into the following error…

    ‘PDFBuilder.HtmlToPdfBuilder.PageSize.get’ must declare a body because it is not marked abstract or extern

    how to solve this?

    ps. i use it in asp.net

    kyo

    July 6, 2010 at 5:27 am

  73. To apply border bottom just put the below specified line of code in incell.cs file parameter constructer.

    value = props[“border-bottom-width”];
    float borderbottom = 0;
    if (value != null)
    borderbottom = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo);
    cell.BorderWidthBottom = borderbottom;

    also add below line of code in the insertStyle of the FactoryProperties class

    else if (key.Equals(Markup.CSS_KEY_BORDERWIDTHBOTTOM))
    {
    String ss = prop[key].Trim().ToLower(System.Globalization.CultureInfo.InvariantCulture);
    h[“border-bottom-width”] = ss;
    }

    In your html page add the Style as follows.

    1

    In order to make it work for other style propeties you have to parse the style element in the incell.cs class.

    rupeshn

    July 26, 2010 at 5:40 am

  74. […] Reference from : Hugoware […]

  75. […] If you want to use iTextSharp to convert HTML to PDF, you can start it by reading a very good article “iTextSharp – Simplify Your HTML to PDF Creation” at https://somewebguy.wordpress.com/2009/05/08/itextsharp-simplify-your-html-to-pdf-creation/ […]

  76. This is a great wrapper class for iTextSharp. Is it possible to directly stream the generated PDF to the client browser instead of saving it in the hard drive? If you have a code snippet how to do it, that will be great!!

    Thanks

    John Velu

    August 9, 2010 at 1:18 pm

  77. Hi,
    This article was wonderful and helpful to me but, I can’t get the output from my HTML code. I don’t know why? This component will not consider the ‘style’ attribute for the HTML tags. I have tried using this code snippets. But I was disappointed by seeing the ouput.

    StringBuilder htmlText =
    new StringBuilder(“<table background='c:\\IMG_0806.jpg' style='background-repeat:no-repeat;'");
    htmlText.Append("Karunagara Pandi Software Engineer”);

    HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.A4);
    HtmlPdfPage first = builder.AddPage();
    first.AppendHtml(htmlText.ToString());

    byte[] fileContent = builder.RenderPdf();
    File.WriteAllBytes(@”C:\PDFImage.pdf”, fileContent);

    Image is not shown…the page… Without using styles alone, how to get the output by adding subsidiary attribute of the tags…..

    Karunagara Pandi

    August 15, 2010 at 4:38 am

  78. Hi Hugo.

    Great job.
    But can you give me tips about how to format cells in a table.
    I mean:

    HIPEOPLE
    or
    HIPEOPLE

    does not work:
    the columns are the same size, 50% each.

    I see it works with the ‘colspan’ attribute, but I need ‘width’.

    Any idea?
    Thanks.

    About people looking for headers/footers with itextsharp 5 or later, have a look at this example:
    http://www.mazsoft.com/blog/post/2008/04/30/Code-sample-for-using-iTextSharp-PDF-library.aspx

    Gianluigi

    August 27, 2010 at 3:07 am

  79. Thanks very good job.
    I think it’s going to be very useful for me. But I am a beginner in C# and Visual Studio and I have some probem now.
    I have in my project a page with some data grips and Charts and I want to print this aspx to pdf but I don’t know how pass my html from my aspx,

    Denis

    September 13, 2010 at 1:57 pm

  80. I am using this code and save my html in “Cache[“pageContent”] = htmlStringWriter.ToString();” but when I passed it to first.AppendHtml(htmlStringWriter.ToString()); I had this exception and I dont know how handle.

    …..

    namespace Reports
    {
    public partial class CacheTest : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    string target = “GeneralGraphics.aspx”;
    DateTime expired = DateTime.Now.AddSeconds(-10); // You need to change here bese on your need

    if (Cache[“pageContent”] == null || (DateTime)Cache[“pageCreateTime”] < expired)
    {
    // Render the page
    System.IO.StringWriter htmlStringWriter = new System.IO.StringWriter();
    Server.Execute(target, htmlStringWriter);

    // Save the page content to the Cache
    Cache["pageContent"] = htmlStringWriter.ToString();
    Cache["pageCreateTime"] = DateTime.Now;

    //Page sizes are found in iTextSharp.text.PageSize
    HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.LETTER);
    HtmlPdfPage first = builder.AddPage();
    first.AppendHtml(htmlStringWriter.ToString());

    byte[] file = builder.RenderPdf();
    File.WriteAllBytes(Server.MapPath("PDF/Chart.pdf"), file);

    // Output the cached content
    Response.Clear();
    Response.Write((string)Cache["pageContent"]);
    Response.End();
    }
    }
    }
    }

    Denis

    September 13, 2010 at 3:16 pm

  81. Hi ,
    While running my webpade i got the following error..
    ‘PDFBuilder.HtmlToPdfBuilder.PageSize.get’ must declare a body because it is not marked abstract or extern .

    Can anybody tell me whats the problem with setting Properties in HtmlToPdfBuilder class..

    amit sharma

    September 15, 2010 at 1:43 am

  82. […] of the solution of html to pdf conversion for .Net is using iTextSharp, and thanks to this article where a html to pdf builder class is provided to make the use of iTextSharp bit easier. But I found […]

  83. hi..
    sound like many people found the solution here..
    congrats..
    can i using this code in asp.net c#?
    to convert my gridview to pdf file?

    bella

    September 21, 2010 at 11:00 pm

  84. hi,

    first.AppendHtml(” “);

    pls send me code how can i write formatted string i need textboxes within div html control

    thanks help me

    SATHEESH KUMAR

    October 29, 2010 at 5:47 am

  85. hi there 🙂 I have the same problem as other said: css file imported is not applied 😕
    which are the tags supported, which are not? what can it be done?
    I have this in my css file:

    h2
    {
    color: red;
    }
    div.big
    {
    background-color: green;
    }

    very basic…

    Adrian

    November 3, 2010 at 3:40 am

  86. Hi

    I have made some support for margins for the 4.1.6 version. You can download the files here and replace the files in 4.1.6 source.

    (Get file here)
    https://sourceforge.net/tracker/?func=detail&aid=3102295&group_id=72954&atid=536238

    Then you can do things like this:

    styles.LoadTagStyle(“li”, “style”, “margin-top:0;margin-bottom:0;”);
    styles.LoadTagStyle(“ul”, “style”, “margin-left:10px;margin-top:5;margin-bottom:5;”);
    styles.LoadTagStyle(“ol”, “style”, “margin-left:10px;margin-top:5;margin-bottom:5;”);
    styles.LoadStyle(“phead”, “style”, “font-family: Arial;font-size: 12pt;color:#333333; font-weight:bold;line-height:12pt;margin-top:10;”);

    Mårten

    November 3, 2010 at 11:06 am

  87. I am sorry Mårten, but what if I have a div and then another div…inherited, so then I will have my style like this div#element1, is there any posibility?

    transportinoradea.ro

    November 3, 2010 at 1:39 pm

  88. thank you , great work 🙂

    omar

    December 18, 2010 at 9:35 am

  89. Nice work but the result still does not close to comercial solutions to convert HTML to PDF.

    F C

    January 17, 2011 at 6:26 am

  90. I am trying to compile the class but it is giving me the error:

    expected ; )

    where public event RenderEvent BeforeRender = (writer, document) => { };

    Any ideas?

    Dave

    January 31, 2011 at 12:33 pm

  91. This article is just too good to be true. Found out there isn’t much support for CSS at all after hours of implementing code that doesn’t work. CSS is not used only for changing font sizes and colors, y’know?

    Very Upset and Disappointed User

    February 4, 2011 at 10:41 am

  92. I need to be able to determine the size in bytes of each page in a PDF file. I can get the size of a page in xy cordinates. But I need page size as if that page were stored as a file on disk (size in bytes). Is there a method in ITextSharp that will allow me to get the size of a given page in bytes?

    Thank You!

    Bryan

    February 8, 2011 at 5:05 pm

  93. Hi there, again. I have a dialog window so it does not have a link. It contains an image and some relative positioned divs on image. Do you believe that this tool can help me?

    Adrian

    February 22, 2011 at 7:20 am

  94. I am using your code and using the latest itextsharp version 5.0.6. I keep getting an error
    MarkUp is not declared. It may be inaccessible due to its protection level.

    ItextsharpUser

    March 9, 2011 at 6:21 am

    • I have the same error, which version of itext did you use?
      Also get this errors:
      ‘PDFBuilder.HtmlToPdfBuilder.PageSize.get’ must declare a body because it is not marked abstract or extern on this line:
      public Rectangle PageSize { get; set; }

      Also i try tu use vb.net class but is not completed and with other errors, anyone got a solution? thanks 😦

      Oliver Cortinas

      March 31, 2011 at 10:39 am

      • Finally i solve it using a previous version of iText and using a code inside the replays to remove the error i have, now i have another thing, i already have an html on disk so load the Html content of the file on a string, but i get this error:
        Input string was not in a correct format.
        on:
        public virtual void AppendHtml(string content, params object[] values) {
        this._Html.AppendFormat(content, values);
        }
        i guess this is becaus i send all the html of the file in the string maybe?

        Oliver Cortinas

        March 31, 2011 at 1:05 pm

      • Hi Oliver,

        I have the exact same error. Can you please give me the full details of how you solved it? Do I need to change the code of the HtmlToPdfBuilder class? I need to fix this urgently to fulfill requirements for a project I am working on.

        I tried to compile this in VS 2005 (C#). I first used the latest version of the iTextSharp libraries (5.0.6), and also tried with (4.1.2), but the error remains the same. If you have used a different version, can you please attach the dll or share the dll you used with me?

        Please give me the complete details of how to fix this as I am new to iTextSharp.

        If there’s anyone else who has encountered this and can help me, I will be very grateful to hear from you.

        Many thanks!

        Sridhar

        April 8, 2011 at 6:36 am

      • I, like imentioned in this post, the error was raised becuase in the html text you have this characters “{” or “}”, that is a problem for the code because use those characters for something else, my recomendation is replace thos strings before send it to convert to something lile “(” and “)” and your problem is solved 🙂
        anyway i just can’t make the code work for the new version of iText that is the version that solve the problems of apply styles to the pdf file converted 😦

        Oliver

        April 8, 2011 at 8:07 am

  95. Ok, solve it, i hace some “{” and “}” in html code that was crashing this thing….now i have a differente issue with REGEX_GET_STYLES:
    parsing “(?[^\{\s]+\w+(\s\[^\{\s]+)?)\s?\{(?[^\}]*)\}” – Unrecognized grouping construct.
    on:
    public void ImportStylesheet(string path) {
    string content = File.ReadAllText(path);
    foreach (Match match in Regex.Matches(content, HtmlToPdfBuilder.REGEX_GET_STYLES)) {

    any ideas?

    Oliver Cortinas

    March 31, 2011 at 2:51 pm

  96. Another thing is the only version of iText that works for that code in this page is version 4.1.2.0 you have to work with this one, search en google for this version…i don’t have the link right here.

    Oliver

    April 8, 2011 at 8:10 am

  97. Hey Peter
    Thanks for the nice support….. I am using the iTextSharp… to create pdf from Html but I am unable to implement CSS file. I have given the absolute path…. and also it doesn’t throw any error but create pdf without css….. so it looks jazzy..
    Pls help

    Dinesh Sharma

    April 19, 2011 at 9:43 am

  98. This looks cool but I’m getting a 404 not found on the code links. Is it down? Did I miss something?

    Jon

    April 29, 2011 at 3:20 pm

  99. Hi,

    I used the above code along with the itextSharp assembly, although i am able to generate the pdf if i pass a string. but the pdf is empty if i pass HTML tags..

    Below is the code i am trying with

    string strCont = “NameAgeCity”;
    HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.LETTER);
    HtmlPdfPage first = builder.AddPage();
    first.AppendHtml(“strCont”);
    byte[] file = builder.RenderPdf();
    File.WriteAllBytes(“c:\\final2.pdf”, file);

    Please let me know if am missing anything here.

    Thanks,
    Raj

    raj

    May 3, 2011 at 5:22 pm

  100. Seems like, the download link for source code is broken. Can you have a look at it?

    Aniket

    May 4, 2011 at 10:07 pm

  101. hello webdev_hb

    read this article the class your are provided. i am not not download. as well as in the code.
    I am getting following error
    “The type or namespace name ‘HtmlToPdfBuilder’ could not be found (are you missing a using directive or an assembly reference?)”

    After adding latest textsharp.dll in my project.

    so please help me to solve this issue.

    Best Regards
    Omkar

    omkar Mhaiskart

    May 6, 2011 at 6:47 am

  102. […] la conversion d'un HTML page pour PDF. Je suis en train de faire usage de la classe helper donné ici et j'ai aussi essayé de faire usage de StyleSheet.LoadTagStyle() pour appliquer le style CSS. Mais […]

  103. Hello very nice blog!! Man .. Excellent .. Wonderful .. I will bookmark your site and take the feeds additionally…I’m glad to find so many useful information here within the submit, we want develop extra techniques in this regard, thank you for sharing. . . . . .

    php script

    February 28, 2019 at 2:02 am


Leave a reply to ItextsharpUser Cancel reply