CSS 使用 XSL-FO、CSS3 代替 CSS2 创建分页文档,如 PDF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10641667/
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
Use of XSL-FO, CSS3 instead of CSS2 to create Paginated documents like PDF?
提问by Peter Krauss
There are a lot of old texts, like this 2002 book, stating that we must use "CSS for Web" and "XSL-FO for print". I think in nowadays (2012) we can, finally, to use CSS with render engines that understand paged media of CSS2and something of CSS3... But where the "new texts", the consensus of programmers, and the investment of softhouses?
有很多旧文本,比如这本 2002 年的书,说明我们必须使用“CSS for Web”和“XSL-FO for print”。我认为在如今(2012 年),我们终于可以将 CSS 与能够理解CSS2和CSS3 的分页媒体的渲染引擎一起使用……但是“新文本”、程序员的共识和软件公司的投资在哪里?
XSL-FO or "XSL Formatting Objects" (a W3C standard) was the most often used technology to generate PDF documents, from XML or XHTML content. Version 1.1 of XSL-FOwas published in 2006, 1.0 in 2001.
XSL-FO 或“XSL Formatting Objects”(W3C 标准)是从 XML 或 XHTML 内容生成 PDF 文档最常用的技术。XSL-FO 1.1版 于 2006 年发布,1.0 版于 2001 年发布。
CSS2.1 is from 2011, but CSS2.0 is a 1998 standard, revised in 2008... I think standard ages are not a problem. CSS with HTML, XHTML or XML have "the power of print": see tools like PrinceXML, WebKit print module(or wkhtmltopdf), ABCpdfand others.
CSS2.1 是 2011 年的,但 CSS2.0 是 1998 年的标准,2008 年修订......我认为标准年龄不是问题。带有 HTML、XHTML 或 XML 的 CSS 具有“打印的力量”:参见PrinceXML、WebKit 打印模块(或wkhtmltopdf)、ABCpdf等工具。
Choosing between CSS and XSL-FO: with CSS2 you can fit the text exactly to the paper page, etc. It's not a matter of pagination, multiple column layouts, place footnotes, running headers, or margins of a page... Both, CSS (paged media) and XSL-FO, are good standards to do this.
在 CSS 和 XSL-FO 之间进行选择:使用 CSS2,您可以使文本完全适合纸页等。这不是分页、多列布局、放置脚注、运行标题或页面边距的问题......两者, CSS(分页媒体)和 XSL-FO 是执行此操作的良好标准。
PS: there are some related questions/answers for this context, about webkit transform, converting with with PHPand about Generation PDF from HTML. No one with good answer for this presented question.
PS:这里有一些相关的问题/答案,关于 webkit 转换,使用 PHP 转换以及关于从 HTML 生成 PDF。没有人对这个提出的问题有很好的答案。
采纳答案by Peter Krauss
Thanks all comments and answers!
感谢所有评论和答案!
Now, 2014, passed over 1.5 years of my post (May 17 '12), is time to consolidate: no answer was, for me, a "full answer", but all answers (see Nenotlep's and Alex's) contributed to form a big picture. My main motivation now, to consolidate, is the @mzjn's news (here)of 2013-11.
现在,2014 年,我的职位(2012 年 5 月 17 日)过去了 1.5 年,是时候巩固了:对我来说,没有答案是“完整的答案”,但所有答案(参见 Nenotlep 和 Alex 的)都有助于形成一个大的图片。我现在巩固的主要动机是@mzjn 2013-11 年的新闻(此处)。
XSL-FO is officially dying
XSL-FO 正式消亡
On Sat, 2013-11-02, Liam R. E. Quinwrote: "We have closed the Working Group because not enough people were taking part", W3C XML Activity Lead, about the failure of XSL-FO 2.0continuity. (see a better copy here).
在 2013-11-02 星期六,Liam RE Quin写道: “我们关闭了工作组,因为没有足够的人参与”,W3C XML 活动负责人,关于XSL-FO 2.0连续性的失败。(在此处查看更好的副本)。
The last update for the Working Draft was in January 2012, and now confirmed: W3C stop developing XSL-2.
工作草案的最后一次更新是在 2012 年 1 月,现在确认:W3C 停止开发 XSL-2。
Why? It will be replaced by CSS3-page, see below.
为什么?它将被CSS3-page替换,见下文。
PS: to discuss the "official statment", use https://stackoverflow.com/a/21345449/287948
PS:讨论“官方声明”,使用https://stackoverflow.com/a/21345449/287948
CSS3 is officially growing
CSS3 正式成长
The standard CSS3-pageis a draft, but many applications, like PrinceXML v9and AntennaHouse Formatter v6demonstrated that it is ready (!); and, the expected launch of HTML5 for 2014is carrying along the forecast release CSS3.
标准的CSS3 页面是一个草案,但许多应用程序,如PrinceXML v9和AntennaHouse Formatter v6表明它已经准备好了(!);并且,2014 年 HTML5的预期发布与 CSS3 的预测版本一起进行。
So, I understand that for W3C, CSS3-pagedo all that we need to express good prints and good PDF.
所以,我明白对于 W3C,CSS3-page 可以做我们需要表达好的印刷品和好的 PDF 的一切。
Other motivations
其他动机
One day, in a far future... PDF will dead — it is complex and is not part of the XML family or W3C investments —, and many claim that EPUB will replace it. This is another good motivation: tablet readers and PC browsers will print (HTML, XHTML and EPUB) as well as PDF. So PDF will be not necessary... And, for this day, the only standard need for, ex. Webkit printing project, will be the CSS3-pagestandard.
总有一天,在遥远的未来...... PDF 将会消亡——它很复杂,不属于 XML 家族或 W3C 投资——并且许多人声称 EPUB 将取代它。这是另一个很好的动机:平板电脑阅读器和 PC 浏览器将打印(HTML、XHTML 和 EPUB)以及 PDF。所以 PDF 将是不必要的......而且,今天,唯一的标准需要,例如。Webkit 打印项目,将成为CSS3 页面标准。
CSS3 is the key point in two strategic affairs: 1) to generate good PDF from XML or HTML contents; 2) to replace PDF.
CSS3 是两个战略事务的关键点:1) 从 XML 或 HTML 内容生成好的 PDF;2)替换PDF。
NOTE: another 2014's updates for the links of the question: wkHtmlToPDFis now here. About "new texts", now we have many, see ex. Building Books with CSS3.
注意:问题链接的另一个 2014 年更新:wkHtmlToPDF nowis here。关于“新文本”,现在我们有很多,见前。使用 CSS3 构建书籍。
An updated answer for programmers, for this page's question, Why use XSL-FO instead of CSS2, for transform HTML into good PDF?
程序员的更新答案,针对此页面的问题,为什么使用 XSL-FO 而不是 CSS2,将 HTML 转换为好的 PDF?
If you go further and implement a new system for XML-Publishing, there are no good reason to use XSL-FO. SUMMARIZING:
如果您更进一步并为XML-Publishing实现一个新系统,则没有充分的理由使用XSL-FO。总结:
XSL-FO is a dead technology today, only used by niche companies, to give maintenance to legacy systems in big publishing companies, like Elsevier... Most writers/readers of Stackoverflow are from small and medium companies. Companies like O'Reilly Media, Inc. already use CSS3 for print.
CSS3will replace CSS2, covering all gaps (and fears as @AlexS's) of CSS2.
today (2014), as you can check by Google or my links (see PrinceXML v9 and AntennaHouse Formatter v6), we have some good software to render content with CSS2or CSS3.
as @bytebuster say, "CSS is much easier to develop" (and easier to learn!).
as I say above, CSS3 is not isolated, it is a piece of the "XML/HTML/SVG" family.
is much cheaper to develop "HTML+CSS templates" (hourly cost of a standard web designer doing a simple task), than "XSL-FO templates" (hourly cost of a rare professional in a complex task).
....
XSL-FO 今天是一种死技术,只被利基公司使用,用于维护大型出版公司的遗留系统,如爱思唯尔...... Stackoverflow 的大多数作者/读者来自中小型公司。这样的公司O'Reilly Media公司已经使用CSS3打印。
CSS3将取代CSS2,涵盖了所有的差距(和恐惧为@来自Alexs的)的CSS2。
今天(2014 年),您可以通过 Google 或我的链接(参见 PrinceXML v9 和 AntennaHouse Formatter v6)查看,我们有一些很好的软件可以使用CSS2或CSS3呈现内容。
正如@bytebuster 所说,“CSS 更容易开发”(而且更容易学习!)。
正如我上面所说,CSS3 不是孤立的,它是“XML/HTML/SVG”系列的一部分。
开发“HTML+CSS 模板”(标准网页设计师完成简单任务的每小时成本)比“XSL-FO 模板”(复杂任务中罕见的专业人士的小时成本)便宜得多。
....
News...
消息...
Jan'2016, the definitive CSS3 standard is coming!
2016 年 1 月,最终的 CSS3 标准即将到来!
About W3C standards: the old "css-page"was replaced by "css-break", and "paged media"to "fragmentation"... Now it is a Candidate Recommendation, see https://www.w3.org/TR/css-break-3
关于W3C标准:旧的“css-page”被“css-break”代替,“paged media”被“fragmentation”……现在是Candidate Recommendation,见https://www.w3.org/ TR/css-break-3
Apr'2020, Blimey, +4 years and nothing!... Ok, need more tests
2020 年 4 月,Blimey,+4 年,一无所有!...好的,需要更多测试
Total 8 years from question's post, and 4 years from "css-break-3 fineshed!" announcement ...
从问题的帖子开始总共 8 年,从“css-break-3 Fineshed!”开始 4 年。公告 ...
Chrome was the first to finesh in 2019 but some was wrong in test validation team of W3C, and in 2020 back... Now the status (in 23 tests) is:
Chrome 是 2019 年第一个完善的,但在 W3C 的测试验证团队中有些错误,并且在 2020 年回来......现在状态(在 23 个测试中)是:
- Chrome's Blinkengine fail 1 test;
- Firefox's Geckoengine fail 3 tests.
- Chrome 的Blink引擎未通过 1 次测试;
- Firefox 的Gecko引擎未能通过 3 次测试。
The draft now is hereand tests here.
回答by Nenotlep
Updated 01.10.2015
2015 年 10 月 1 日更新
I used to do CSS to PDF (wkhtmltopdf) and XSL-FO to PDF and I prefer CSS, but there are lots of issues with it. IMO the best CSS/HTML to PDF renderer is wkhtmltopdf, but it has tons of problems like print-quality material issues, page breaking issues, CMYK coloring, exact positioning and fullscreen rendering.
我曾经将 CSS 转为 PDF (wkhtmltopdf),将 XSL-FO 转为 PDF,我更喜欢 CSS,但它有很多问题。IMO 最好的 CSS/HTML 到 PDF 渲染器是 wkhtmltopdf,但它有很多问题,比如打印质量材料问题、分页问题、CMYK 着色、精确定位和全屏渲染。
Requirements like "move that box 1.8mm to the right and up so that it touches the top of the paper"and "we need the last page to be a 100% wide marginless table"are both quite doable in XSL-FO but in CSS it is too frightening to even consider. In some cases CSS just doesn't cut it as good enough software to render it doesn't exist even if the tags do. Even wkhtmltopdf (0.11, not sure about later) uses XSLT when rendering the TOC and doesn't really support @page
.
诸如“将该框向右和向上移动 1.8 毫米以使其接触纸张顶部”和“我们需要最后一页是一个 100% 宽的无边距表格”之类的要求在 XSL-FO 中都非常可行,但在 CSS 中甚至考虑都太可怕了。在某些情况下,即使标签存在,CSS 也不会将它切割为足够好的软件来呈现它不存在。甚至 wkhtmltopdf(0.11,以后不确定)在呈现 TOC 时也使用 XSLT 并且并不真正支持@page
.
I can't speak for PrinceXML as although it looks great I know in advance that the price tag would be impossible so it's not an option - I suspect this is true for a lot of developers and companies.
我不能说 PrinceXML,因为它看起来很棒,我事先知道价格标签是不可能的,所以它不是一个选项 - 我怀疑这对很多开发人员和公司来说都是如此。
If there was better software to do the rendering and more user I really do think CSS would be a better option usually as it's so much nicer to write (both css and the source (x)html) and there are tons of editors out there. It's a bit like the old Linux vs Windows debate - IMO Linux is nicer to use but lacks the software, existing expertise and support that is often required.
如果有更好的软件来进行渲染和更多的用户,我真的认为 CSS 通常会是一个更好的选择,因为它写起来更好(包括 css 和源 (x)html),并且有大量的编辑器。这有点像旧的 Linux 与 Windows 辩论——IMO Linux 更好用,但缺乏通常需要的软件、现有专业知识和支持。
And to echo the comments, source material is always an issue with CSS. CSS for XML is a bit uncharted territory and just about everything everywhere is XML. Unfortunately. I have a severe dislike for XML even though it's practically much more usable than (X)HTML.
为了回应评论,源材料始终是 CSS 的问题。XML 的 CSS 是一个有点未知的领域,几乎所有地方都是 XML。很遗憾。我非常不喜欢 XML,尽管它实际上比 (X)HTML 更有用。
回答by mzjn
One possible reason for banking on CSS rather than XSL-FO in the future is that the XML Print and Page Layout Working Groupat W3C is no longer active. There was not enough interest to sustain this working group. The group published an XSL 2.0 working draftin early 2012, but now it seems quite unlikely that an updated W3C recommendation will ever emerge.
未来依赖 CSS 而不是 XSL-FO 的一个可能原因是W3C的XML 打印和页面布局工作组不再活跃。没有足够的兴趣来维持这个工作组。该小组于 2012 年初发布了XSL 2.0 工作草案,但现在似乎不太可能出现更新的 W3C 建议。
There is a very recent thread on the XSL-List mailing list about the reasons for closing the working group and about the future of XSL-FO vs. CSS. See http://markmail.org/thread/65j2ah2kulcp35fm.
XSL-List 邮件列表上有一个关于关闭工作组的原因以及 XSL-FO 与 CSS 的未来的最新主题。请参阅http://markmail.org/thread/65j2ah2kulcp35fm。
And by the way, even though this is an interesting topic, I'm not sure if the question is a good fit for Stack Overflow. IMHO, it is more of an open-ended invitation to discuss something rather than a question about a specific, practical, answerable problem.
顺便说一句,尽管这是一个有趣的话题,但我不确定这个问题是否适合 Stack Overflow。恕我直言,这更像是一个开放式的邀请来讨论一些事情,而不是一个关于一个具体的、实际的、可回答的问题的问题。
回答by Alex S
I agree with some of what has been posted by @Nenotlep. But I am not sure if CSS markup is yet as extensive for Paginated documents as XSL-FO. But I would not know that.
我同意@Nenotlep 发布的一些内容。但我不确定 CSS 标记对于分页文档是否像 XSL-FO 一样广泛。但我不会知道。
I also added this part to his answer because I was unable to "comment" on the answer.
我还将这部分添加到他的答案中,因为我无法对答案进行“评论”。
There is some history to the whole issue.
整个问题都有一段历史。
Additionally, the richness of XSL-FO and its learnings & burn-in curve over the last 10+ years on the FO rendering has had quite a tenure to get "more" things ironed out.
此外,XSL-FO 的丰富性及其在过去 10 多年里对 FO 渲染的学习和老化曲线已经有相当长的时间来解决“更多”问题。
I was responsible for proof of concept and prototyping an Enterprise wide XML Content related system for a Fortune 20 back in 2003.
早在 2003 年,我就负责为财富 20 强的企业范围的 XML 内容相关系统进行概念验证和原型设计。
One of the pieces of that system had to render PDF, Word, X/HTML versions of documents on the fly as people changed, added & modified content XML.
该系统的其中一个部分必须随着人们更改、添加和修改 XML 内容而动态呈现文档的 PDF、Word、X/HTML 版本。
Even XSL-FO > PDF and to Word-ML had a bunch of teething issues at the time.
甚至 XSL-FO > PDF 和 Word-ML 在当时也有很多初期问题。
These were inherent due to the following reason:
由于以下原因,这些是固有的:
- Original and new goals and capabilities of the Markup & Styling languages
- Ability & Limitations of the Final Rendering Component to accurately represent the given markup (i.e. XSL-FO to PDF Component or X/HTML to Screen via Web Browser)
- 标记和样式语言的原始和新目标和功能
- 最终渲染组件准确表示给定标记的能力和限制(即 XSL-FO 到 PDF 组件或 X/HTML 到屏幕通过 Web 浏览器)
It has been 10 years since I have been frequently hands on with XSL-FO / HTML/ CSS but the above issues were interesting to discuss with the Gods of XML/ XSL world at the time (Dave Pawson, Michael Kay, Wendell Piez etc.)
自从我经常接触 XSL-FO/HTML/CSS 已经 10 年了,但与当时的 XML/XSL 世界之神(Dave Pawson、Michael Kay、Wendell Piez 等)讨论上述问题很有趣。 )
It is quite possible that all representative markup that XSL-FO had over CSS for Paginated output, is now (2013) possibly replicated in CSS3 and is rendered appropriately.
XSL-FO 在分页输出的 CSS 上的所有代表性标记现在(2013 年)很可能在 CSS3 中复制并被适当地呈现。
I hope this helps.
我希望这有帮助。
2017 Edit:
2017年编辑:
Apparently CSS is still playing catch up in some ways and I remember having most of this in 2003 - That is 14 years and in web tech that's an eon too slow :) .
显然 CSS 在某些方面仍在追赶,我记得大部分时间是在 2003 年 - 那是 14 年,在网络技术中,这太慢了 :) 。
https://twitter.com/t_machine_org/status/917025348646199297
回答by Anon
As far as I know you cannot generate SVG charts or SVG barcodes with CSS.
据我所知,您无法使用 CSS 生成 SVG 图表或 SVG 条码。