Firstly, HTML and PDF are not related, although they were created at about the same time. HTML is designed to convey higher-level information, such as paragraphs and tables. Although there are ways to control it, the browser should ultimately use these higher-level concepts. PDF is designed to transfer documents and documents should “look” the same wherever they appear.
In an HTML document, you may have a paragraph that is 100% wide and depending on the width of your monitor, it may take 2 lines or 10 lines, and when it prints it may be 7 lines, and when you look at it on your phone 20 lines may be required. However, the PDF file must be independent of the rendering device, so regardless of screen size it should always display exactly the same.
Due to the musts above, PDF does not support abstract things such as “tables” or “paragraphs”. There are three main points that PDF supports: text, lines / shapes, and images. (There are other things, such as annotations and movies, but I try to make it simple here.) In PDF, you don't say "here is a paragraph, the browser does your thing!". Instead, you say: "Draw this text in this exact place X, Y, using this exact font, and don’t worry, I already calculated the width of the text, so I know that it will fit into this line." You also do not say “the table here”, but instead you say: “Draw this text in this exact place, and then draw a rectangle in this other exact place, which I have already calculated, so I know that it will be around the text.”
Secondly, iText and iTextSharp parse HTML and CSS. It. ASP.Net, MVC, Razor, Struts, Spring, etc. - all HTML frameworks, but iText / iTextSharp is 100% unaware of them. Same thing with DataGridViews, Repeaters, Templates, Views, etc., which are structure-specific abstractions. It is your responsibility to get the HTML from your choice of framework. IText will not help you. If you get an exception saying The document has no pages , or you think that “iText does not parse my HTML”, it’s almost certain that you don’t actually have HTML , you only think what you are doing.
Thirdly, the built-in class that has been around for many years is HTMLWorker , however it has been replaced by XMLWorker ( Java / .Net ). Zero work is done on HTMLWorker , which does not support CSS files and has limited support for the most basic CSS properties, but actually breaks down into specific tags . If you do not see the HTML attribute or CSS property and value in this file , then it is probably not supported by HTMLWorker . XMLWorker can sometimes be more complicated, but these complications also make extensible .
Below is the C # code that shows how to parse HTML tags in text abstractions that are automatically added to the document you are working on. C # and Java are very similar, so it’s relatively easy to convert them. Example # 1 uses embedded HTMLWorker to parse an HTML string. Since only inline styles are supported, class="headline" ignored, but everything else should work. Example # 2 is the same as the first, except that XMLWorker used XMLWorker . Example # 3 also parses a simple CSS example.
//Create a byte array that will eventually hold our final PDF Byte[] bytes; //Boilerplate iTextSharp setup here //Create a stream that we can write to, in this case a MemoryStream using (var ms = new MemoryStream()) { //Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF using (var doc = new Document()) { //Create a writer that bound to our PDF abstraction and our stream using (var writer = PdfWriter.GetInstance(doc, ms)) { //Open the document for writing doc.Open(); //Our sample HTML and CSS var example_html = @"<p>This <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>"; var example_css = @".headline{font-size:200%}"; /************************************************** * Example #1 * * * * Use the built-in HTMLWorker to parse the HTML. * * Only inline CSS is supported. * * ************************************************/ //Create a new HTMLWorker bound to our document using (var htmlWorker = new iTextSharp.text.html.simpleparser.HTMLWorker(doc)) { //HTMLWorker doesn't read a string directly but instead needs a TextReader (which StringReader subclasses) using (var sr = new StringReader(example_html)) { //Parse the HTML htmlWorker.Parse(sr); } } /************************************************** * Example #2 * * * * Use the XMLWorker to parse the HTML. * * Only inline CSS and absolutely linked * * CSS is supported * * ************************************************/ //XMLWorker also reads from a TextReader and not directly from a string using (var srHtml = new StringReader(example_html)) { //Parse the HTML iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml); } /************************************************** * Example #3 * * * * Use the XMLWorker to parse HTML and CSS * * ************************************************/ //In order to read CSS as a string we need to switch to a different constructor //that takes Streams instead of TextReaders. //Below we convert the strings into UTF8 byte array and wrap those in MemoryStreams using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css))) { using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html))) { //Parse the HTML iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss); } } doc.Close(); } } //After all of the PDF "stuff" above is done and closed but **before** we //close the MemoryStream, grab all of the active bytes from the stream bytes = ms.ToArray(); } //Now we just need to do something with those bytes. //Here I'm writing them to disk but if you were in ASP.Net you might Response.BinaryWrite() them. //You could also write the bytes to a database in a varbinary() column (but please don't) or you //could pass them to another function for further PDF processing. var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf"); System.IO.File.WriteAllBytes(testFile, bytes);
Update 2017
There is good news for HTML-to-PDF requirements. As this answer showed , the W3C css-break-3 standard will solve the problem ... This is a candidate’s recommendation with a plan to become the final recommendation this year after tests.
As a non-standard solution, there are solutions with plugins for C #, as shown by print-css.rocks .