Active5 months ago
I want to convert the below HTML to PDF using iTextSharp but don't know where to start:
Itext Pdf Example
Converting PDF layout to HTML layout is very hard because the PDF design philosophy is very different from how HTML layouting works. Google has some sort of solution for it but it will usually break layout. Regarding your CV concept: As CV layout is highly important for customers using a site I would not want to auto-convert PDF CVs to HTML CVs.
Our article about How to convert HTML to PDF using iTextSharp Library In ASP.NET. We will show you how to Export HTML DIV contents to PDF using iTextSharp or how to export ASP.Net Panel control (which is rendered as HTML DIV) to PDF (Portable Document Format) Document or entire webpage to PDF using iTextSharp Free PDF Library. What version of DLL are you using. Practically you should be able to cast it by converting it to arraylist. If you send me the codesnippet you have and the dll version i will try to help you out. Questions: I want to convert the below HTML to PDF using iTextSharp but don’t know where to start: This is some sample text!!! Answers: First, HTML and PDF are not related although they were created around the same time. Another way to convert PDF to HTML is by clicking on “Convert PDF” in the Welcome page. Select a PDF file you want to convert from local folder, select the output format type and choose an output folder to save the converted word file.
Chris HaasChris Haas40.8k7 gold badges103 silver badges219 bronze badges
5 Answers
First, HTML and PDF are not related although they were created around the same time. HTML is intended to convey higher level information such as paragraphs and tables. Although there are methods to control it, it is ultimately up to the browser to draw these higher level concepts. PDF is intended to convey documents and the documents must 'look' the same wherever they are rendered.
In an HTML document you might have a paragraph that's 100% wide and depending on the width of your monitor it might take 2 lines or 10 lines and when you print it it might be 7 lines and when you look at it on your phone it might take 20 lines. A PDF file, however, must be independent of the rendering device, so regardless of your screen size it must always render exactly the same.
Because of the musts above, PDF doesn't support abstract things like 'tables' or 'paragraphs'. There are three basic things that PDF supports: text, lines/shapes and images. (There are other things like annotations and movies but I'm trying to keep it simple here.) In a PDF you don't say 'here's a paragraph, browser do your thing!'. Instead you say, 'draw this text at this exact X,Y location using this exact font and don't worry, I've previously calculated the width of the text so I know it will all fit on this line'. You also don't say 'here's a table' but instead you say 'draw this text at this exact location and then draw a rectangle at this other exact location that I've previously calculated so I know it will appear to be around the text'.
Second, iText and iTextSharp parse HTML and CSS. That's it. ASP.Net, MVC, Razor, Struts, Spring, etc, are all HTML frameworks but iText/iTextSharp is 100% unaware of them. Same with DataGridViews, Repeaters, Templates, Views, etc. which are all framework-specific abstractions. It is your responsibility to get the HTML from your choice of framework, iText won't help you. If you get an exception saying
The document has no pages
or you think that 'iText isn't parsing my HTML' it is almost definite that you don't actuallyhave HTML, you only think you do.Third, the built-in class that's been around for years is the
HTMLWorker
however this has been replaced with XMLWorker
(Java / .Net). Zero work is being done on HTMLWorker
which doesn't support CSS files and has only limited support for the most basic CSS properties and actually breaks on certain tags. Create iso of windows 7. If you do not see the HTML attribute or CSS property and value in this file then it probably isn't supported by HTMLWorker
. XMLWorker
can be more complicated sometimes but those complications also make itmoreextensible.Below is C# code that shows how to parse HTML tags into iText abstractions that get automatically added to the document that you are working on. C# and Java are very similar so it should be relatively easy to convert this. Example #1 uses the built-in
HTMLWorker
to parse the HTML string. Since only inline styles are supported the class='headline'
gets ignored but everything else should actually work. Example #2 is the same as the first except it uses XMLWorker
instead. Example #3 also parses the simple CSS example.2017's update
There are good news for HTML-to-PDF demands. As this answer showed, the W3C standard css-break-3 will solve the problem.. It is a Candidate Recommendation with plan to turn into definitive Recommendation this year, after tests.
As not-so-standard there are solutions, with plugins for C#, as showed by print-css.rocks.
@Chris Haas has explained very well how to use
my add is:
By using
Edit: adding sample code:
ASPX page:
itextSharp
to convert HTML
to PDF
, very helpfulmy add is:
By using
HtmlTextWriter
I put html tags inside HTML
table + inline CSS i got my PDF as I wanted without using XMLWorker
.Edit: adding sample code:
ASPX page:
C# code:
of course include iTextSharp Refrences to cs file
Hope this helps!
Thank you
meJustAndrewThank you
Itext Convert Html To Pdf C#
3,3644 gold badges27 silver badges53 bronze badges
SamSam
As of 2018, there is also iText7 (A next iteration of old iTextSharp library) and its HTML to PDF package available: itext7.pdfhtml
Usage is straightforward:
Method has many more overloads.
Update: iText* family of products has dual licensing model: free for open source, paid for commercial use.
George ChakhidzeGeorge Chakhidze
Jonas TJonas T2,0013 gold badges23 silver badges39 bronze badges
Here's the link I used as a guide. Hope this helps!
You can download the sample file. Just place the
Harambe Attack HelicopterHarambe Attack Helicopterhtml
you want to convert in the files
folder and run. It will automatically generate the pdf file and place it in the same folder. But in your case, you can specify your html path in the htmlFileName
variable.3651 gold badge12 silver badges32 bronze badges
Itext Api Convert Html To Pdf
protected by Bruno LowagieMar 19 '15 at 17:35
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?