How to generate PDF from HTML using C#
October 26, 2018
My client need is actually to create a c# home window solution which would certainly develop PDF files coming from html, html file consists of images, font style as well as designating e.g Postpaid phone costs(which possesses images, message, various different font style and styling). This inquiry might be actually offered on stackoverflow however i have actually looked a whole lot and did certainly not discover any sort of great instance of producing pdf coming from complicated html.
Second, iText as well as iTextSharp analyze HTML and CSS. That’s it. ASP.Net, MVC, Razor, Struts, Spring, etc, are actually all HTML structures yet iText/iTextSharp is one hundred% uninformed of them. Very same along with DataGridViews, Repeaters, Templates, Perspectives, etc. which are all framework-specific absorptions. It is your obligation to acquire the HTML from your selection of platform, iText won’t assist you. If you acquire an exception stating The document possesses no webpages or even you assume that “iText isn’t analyzing my HTML” it is just about precise that you don’t really possess HTML, you just believe you do.
Zero job is actually being performed on HTMLWorker which doesn’t support CSS reports as well as possesses just limited assistance for the many essential CSS residential properties and actually rests on certain tags. If you do certainly not observe the HTML quality or even CSS building and market value in this data at that point it most likely isn’t supported through HTMLWorker.
Below is actually C# code that demonstrates how to parse HTML tags in to iText absorptions that acquire instantly added to the document that you are actually dealing with. C# and also Caffeine are very comparable so it needs to be actually fairly quick and easy to convert this. Instance # 1 uses the built-in HTMLWorker to analyze the HTML string. Because simply inline styles are supported the lesson=”headline” obtains disregarded yet everything else should in fact work. Instance # 2 is actually the exact same as the very first apart from it utilizes XMLWorker as an alternative. Instance # 3 also parses the easy CSS example.
I have attached the html and also css which needs to have to become exchanged PDF however presently making use of ItextSharp with c#, creating the records as apparent in connected screenshot.
Considering that of the requirements over, PDF does not support intellectual points like “tables” or even “paragraphs”. There are three general points that PDF supports: content, images and also lines/shapes. (There are actually various other traits like notes and also movies but I’m trying to keep it simple listed below.) In a PDF you don’t say “below’s a paragraph, browser do your factor!”. Rather you claim, “pull this message at this exact X, Y location using this exact font and also do not panic, I have actually previously determined the distance of the content so I recognize it will all of match on this collection”. You additionally do not claim “here’s a dining table” but rather you claim “attract this text at this precise site and at that point draw a square at this various other exact area that I have actually previously calculated so I understand it is going to look around the content”.
HTML as well as PDF are actually certainly not related although they were made around the very same opportunity. HTML is actually aimed to communicate greater level relevant information such as dining tables and also paragraphs.
In an HTML document you may have a paragraph that’s 100% wide and also depending upon the distance of your display it may take 2 lines or even 10 lines and when you print it could be 7 collections and also when you examine it on your phone it might take 20 lines. A PDF report, nonetheless, must be actually independent of the providing unit, thus irrespective of your display measurements it need to constantly deliver specifically the exact same.