简体   繁体   English

使用CSS从HTML在iTextSharp中渲染PDF

[英]Render PDF in iTextSharp from HTML with CSS

Any idea how to render a PDF using iTextSharp so that it renders the page using CSS. 任何想法如何使用iTextSharp渲染PDF,以便它使用CSS渲染页面。 The css can either be embedded in the HTML or passed in separately, I don't really care, just want it to work. CSS既可以嵌入HTML中,也可以单独传递,我并不在乎,只是希望它能工作。

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. 另外,我真的想坚持使用iTextSharp,尽管如果您有其他建议,它必须是免费的,开源的,并具有允许在商业软件中使用它的许可证。

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. 基本上,您将需要将HTML和CSS标记转换为iTextSharp类的解析器。 So <table> becames iTextSharp.SimpleTable and so on. 因此<table>变成了iTextSharp.SimpleTable等等。

It would be easy to come up with prototype that would be able to work with limited html and css subset. 能够与有限的html和css子集一起工作的原型很容易提出。

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 告诉您的用户下载名为PDFCreator的开源应用程序
  • Make all your html reports printer friendly by providing stylesheets for print. 通过提供要打印的样式表,使所有html报表打印机友好。

    If some of your multi-page reports need to have headers on every page, set them up in THEAD html tag. 如果您的某些多页报表需要在每个页面上都包含标题,请在THEAD html标签中进行设置。

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). 现在,用户将能够进行友好的打印,并且如果他们选择PDFCreator打印机驱动程序,他们甚至可以获取PDF格式的报告(还有其他pdf打印机驱动程序,但是该驱动程序是免费的且开源的)。

Also I know HTML is not as flexible as PDF but it might be good enough. 我也知道HTML不如PDF灵活,但它可能已经足够了。 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. 我正在与真实用户进行一些测试,他们实际上很喜欢,因为他们不仅可以将任何内容打印为PDF(甚至超出我的应用程序),而且他们的工作流程更快,因为他们无需下载并等待pdf阅读器打开起来 they just print (or export to pdf) what they see on website directly from their webbrowser... kind of makes sense. 他们只是直接从Web浏览器中打印(或导出为pdf)他们在网站上看到的内容,这很有意义。

Try WKHTMLTOPDF. 尝试WKHTMLTOPDF。

It's an open source implementation of webkit. 这是webkit的开源实现。 Both are free. 两者都是免费的。

We've set a small tutorial here 我们在这里设置了一个小教程

 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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM