可以使用 Apache FOP 将任意 HTML 转换为 PDF 吗? [英] Can Apache FOP be used to convert an arbitary HTML to PDF?

查看:44
本文介绍了可以使用 Apache FOP 将任意 HTML 转换为 PDF 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Apache FOP 将 HTML 转换为 PDF.( HTML --> XHTML--> XSL-FO --> PDF).我使用来自 Antenna House 的 xhtml2fo.xsl 进行 xhtml --> XSL-FO 转换.

它适用于简单的 html 文件.

适用于带有样式(通过嵌入式 css 或样式属性)的 html 文件.PDF 已创建但完全未格式化.我正在尝试转换 HTML 文件,而我对样式/内容没有太多控制权.

在我的用例中为每个 html 创建一个 xslt 是不切实际的.

目前,我确实有一个使用 flysaucer 的工作实现.但是,该要求要求在没有 AGPL 许可的情况下实现.

我的问题是:这可以通过 FOP 实现吗?

感谢任何帮助

解决方案

tl;dr version:

在最一般的情况下,,您不能使用 FOP 转换任何 html 保留原始样式(更改格式化程序并不能解决问题).>

但是,您可以使用 FOP(或任何其他格式化程序)来尝试并合理地处理大量 html 文档;这可能需要一些 XSLT 调整.


为什么它不能正常工作

<块引用>

HTML -->XHTML -->XSL-FO -->PDF

您对必要转换链的描述很到位.

然而,FOP 只涉及最后一步:除了尚未实现的功能外,最终的 PDF 文件应尊重 FO 文件中表达的排版特征.><块引用>

我使用了来自 Antenna House 的 xhtml2fo.xsl 来制作 xhtml -->XSL-FO 转换[...]

PDF 已创建但完全未格式化

您使用的样式表是这个来自 AntennaHouse 网站的样式表?

快速浏览一下,似乎应该转换 style=..." 属性,在 FO 输出中生成单独的属性,但是 它不处理外部 CSS 文件.

因此,使用外部 CSS 样式的 HTML 文件将被转换为没有任何格式属性的 FO 文件(font-familyfont-size文本对齐, ...).

<块引用>

这可以通过 FOP 实现吗?

如果确实如此,格式化程序只能使用默认值,其中一些(font-family 想到)依赖于应用程序.

因此,根据您使用的格式化程序,您将获得稍微不同的结果,但仍然是未格式化"的结果.一个.

您需要的要么是一个合并"工具,要么是合并"工具.html 和 css 文件,内联样式以便 XSLT 可以处理它们,或者不同的样式表能够考虑外部 css 文件(但我怀疑在一般情况下编写一个工作并不容易).

什么可以不费吹灰之力解决

在处理 html 表格时,链接的 XSLT 使用 fo:table-and-caption 元素,FOP 不支持该元素,因此表格消失"了.来自输出.

这可以通过 XSLT 中的一个小改动来解决,或者(可能是一个更简洁的解决方案)使用导入另一个的自定义样式表:

<xsl:include href="xhtml2fo.xsl"/><xsl:output method="xml"版本=1.0"编码=UTF-8"缩进=否"/><xsl:template match="html:table";优先级=2"><fo:table xsl:use-attribute-sets="table"><!-- 警告:未处理表格标题!--><xsl:call-template name="process-table"/></fo:table></xsl:模板></xsl:stylesheet>

您实际使用的样式表可能需要进行一些类似的调整才能更好地与 FOP 结合使用.

披露:我是一名 FOP 开发人员,虽然现在不是很活跃.

I have tried to use Apache FOP to convert HTML to PDF. ( HTML -->XHTML--> XSL-FO --> PDF). I used the xhtml2fo.xsl from Antenna House for the xhtml --> XSL-FO conversion.

It works for simple html files.

It does not work for html files with styling ( via embedded css or by style attribute). A PDF is created but completely unformatted. I am trying to convert HTML file where I do not have much control over the styling/content.

Creating an xslt for each html is not practical in my use-case.

Currently, I do have a working implementation with flyingsaucer. However, the requirement calls for an implementation without AGPL license.

My Question is: Can this be achieved with FOP ?

Appreciate any help

解决方案

tl;dr version:

In the most general situation, no, you cannot use FOP to convert any html preserving the original styles (and changing formatter would not solve the problem).

However, you can use FOP (or any another formatter) to try and handle reasonably well a large subset of html documents; this could require some XSLT adjustment.


Why it cannot work in general

HTML --> XHTML --> XSL-FO --> PDF

Your description of the necessary transformation chain is spot on.

However, FOP is only involved in the last step: with the exception of the features that are not implemented yet, the final PDF file should respect the typographical characteristics expressed in the FO file.

I used the xhtml2fo.xsl from Antenna House for the xhtml --> XSL-FO conversion [...]

A PDF is created but completely unformatted

Is the stylesheet you are using this one from the AntennaHouse site?

From a quick look, it seems like it should convert the style="..." attribute producing separate attributes in the FO output, but it does not process external CSS files.

As a result, the HTML files styled with external CSS will be transformed into FO files without any formatting attribute (font-family, font-size, text-align, ...).

Can this be achieved with FOP ?

If that's indeed the case, the formatter cannot do anything but use the default values, a few of which (font-family comes to mind) are application-dependant.

So, according to the formatter you use you will have a slightly different result, but still an "unformatted" one.

What you need is either a tool to "merge" the html and css files, inlining the styles so that the XSLT can process them, or a different stylesheet capable of taking into account the external css files (but I suspect it would not be easy to write one working in a general case).

What can be fixed with little effort

While processing html tables the linked XSLT uses the fo:table-and-caption element, which is not supported by FOP so the tables "disappear" from the output.

This can be fixed with a small change in the XSLT, or (probably a cleaner solution) using a custom stylesheet importing the other one:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:html="http://www.w3.org/1999/xhtml">

  <xsl:include href="xhtml2fo.xsl"/>

  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>

  <xsl:template match="html:table" priority="2">
    <fo:table xsl:use-attribute-sets="table">
      <!-- warning: table caption is not processed! -->
      <xsl:call-template name="process-table"/>
    </fo:table>
  </xsl:template>

</xsl:stylesheet>

It is possible that the stylesheet you are actually using needs a few similar adjustments to better work in conjunction with FOP.

Disclosure: I'm a FOP developer, though not very active nowadays.

这篇关于可以使用 Apache FOP 将任意 HTML 转换为 PDF 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆