21

Any idea how to render a PDF using iTextSharp so that it renders the page using CSS. The css can either be embedded in the HTML or passed in separately, I don't really care, just want it to work.

Specific code examples would be greatly appreciated.

Also, I would really like to stick with iTextSharp, though if you do have suggestions for something else, it's got to be free, open source, and have a license that permits using it in commercial software.

Adam Haile
  • 30,705
  • 58
  • 191
  • 286
  • I am going to use iTextSharp for my new RealEstate project, i will create a PDF file out of selected estates and send it to print. If the thing that you mention "using css" is possible, it would be great. I hope someone gives us a detailed answer about your question. Thanks – Barbaros Alp Jan 10 '09 at 02:03
  • Take a look at [this similar question](http://stackoverflow.com/questions/415535/maintain-css-styling-when-converting-html-to-pdf-in-asp-net). – Mauricio Scheffer Feb 01 '09 at 15:53

3 Answers3

9

It's not possible right now but nothing stops you from starting open-source project that will do it. I might actually start one, because I need it too!

Basically you will need parser that will convert html and css markup into iTextSharp classes. So <table> becames iTextSharp.SimpleTable and so on.

It would be easy to come up with prototype that would be able to work with limited html and css subset.

Update: Until the time this will be possible, this is how I temporarily resolved it for myself. Only two steps:

  • Tell your users to download open-source app called PDFCreator
  • Make all your html reports printer friendly by providing stylesheets for print.

    If some of your multi-page reports need to have headers on every page, set them up in THEAD html tag.

Now users will be able to print-friendly and if they choose PDFCreator printer driver, they will even be able to get report in PDF format (there are other pdf printer drivers but this one is free and open-source).

Also I know HTML is not as flexible as PDF but it might be good enough. I was doing some tests with real users and they actually like it because not only they can now print anything to PDF (even beyond my app), also their workflow is faster because they don't have to download and wait until their pdf reader opens up. they just print (or export to pdf) what they see on website directly from their webbrowser... kind of makes sense.

lubos hasko
  • 24,752
  • 10
  • 56
  • 61
  • Ha...if only I had the time. I would if I wasn't already working on this other project that I'm trying to use iTextSharp for. Besides...I suck at parsers... – Adam Haile Jan 10 '09 at 05:04
  • You should check out [Rotativa](https://github.com/webgio/Rotativa). Here is a [blog post](http://letsfollowtheyellowbrickroad.blogspot.it/search/label/Rotativa) on it too that explains how to get it using the Nuget package manager. It is based on wkhtmltopdf that Mic mentioned in another answer. – d456 Mar 27 '13 at 19:43
6

Try WKHTMLTOPDF.

It's an open source implementation of webkit. Both are free.

We've set a small tutorial here

Mic
  • 24,812
  • 9
  • 57
  • 70
2
 List<DateTime[]> getListWeeks(int annee)
        {
            List<DateTime[]> weeks = new List<DateTime[]>();
            DateTime beginDate = new DateTime(annee, 1, 1);
            DateTime endDate = new DateTime(annee, 12, 31);
            int nb =(int)beginDate.DayOfWeek;
            DateTime monday = beginDate.AddDays(-nb+1); ;
            DateTime saturday = monday.AddDays(6);
            while (monday < endDate)
            {
                    weeks.Add(new DateTime[] { monday, saturday });
                    monday = monday.AddDays(7);
                    saturday = monday.AddDays(6);
            }
            return weeks;
        }