C# 使用 CSS 从 HTML 渲染 iTextSharp 中的 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/430280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:04:23  来源:igfitidea点击:

Render PDF in iTextSharp from HTML with CSS

c#htmlpdfitextsharp

提问by Adam Haile

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.

任何想法如何使用 iTextSharp 呈现 PDF 以便它使用 CSS 呈现页面。css 可以嵌入在 HTML 中,也可以单独传入,我不在乎,只希望它起作用。

Specific code examples would be greatlyappreciated.

具体的代码示例将大大赞赏。

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,但如果您对其他东西有任何建议,它必须是免费的、开源的,并且拥有允许在商业软件中使用它的许可证。

采纳答案by lubos hasko

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.SimpleTableand so on.

基本上,您将需要将 html 和 css 标记转换为 iTextSharp 类的解析器。于是就<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
  • 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.

  • 告诉您的用户下载名为PDFCreator的开源应用程序
  • 通过提供用于打印的样式表,使您的所有 html 报告打印机友好。

    如果您的某些多页报告需要在每个页面上都有标题,请将它们设置在 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. 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.

我也知道 HTML 不如 PDF 灵活,但它可能已经足够好了。我正在与真实用户进行一些测试,他们实际上很喜欢它,因为他们现在不仅可以将任何内容打印为 PDF(甚至超出我的应用程序),而且他们的工作流程更快,因为他们不必下载并等待他们的 pdf 阅读器打开向上。他们只是直接从他们的网络浏览器打印(或导出为 pdf)他们在网站上看到的内容......有点道理。

回答by Mic

Try WKHTMLTOPDF.

试试 WKHTMLTOPDF。

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

它是 webkit 的开源实现。两者都是免费的。

We've set a small tutorial here

我们在这里设置了一个小教程

回答by Noureddine HALA

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