最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】docbook中手动将css转换为fo即pdf的xsl参数配置

Docbook crifan 2626浏览 0评论

【背景】

折腾:

【记录】把docbook中的html的css的配置效果应用到fo即PDF等输出文件类型中

期间,

已经尝试了:

【记录】docbook中尝试使用CSSToXSLFO将css转换为fo(pdf)的xsl配置参数

结果失败了。

所以只能现在去,手动的,将css的配置,转换为fo即pdf的xsl参数配置了。

【折腾过程】

1.去用:

<!--============================================================================
verbatim(programlisting/literallayout/...) setting
=============================================================================-->
<xsl:param name="shade.verbatim" select="1"></xsl:param>
<!-- http://lists.oasis-open.org/archives/docbook/200801/msg00009.html -->
<!-- http://www.dpawson.co.uk/docbook/styling/params.html -->
<xsl:attribute-set name="shade.verbatim.style">
    <!-- <xsl:attribute name="border">0</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">Lavender</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">#eee</xsl:attribute> -->
    <xsl:attribute name="background-color">#fefde6</xsl:attribute>
    <xsl:attribute name="padding">1pt</xsl:attribute>
</xsl:attribute-set>

的确是可以让,包括programlisting(和其他的literallayout等)的背景色,变成橘黄色:

programinglist and screent both light orange background

2.但是很明显,也把scree背景色,都变成一样的了。

希望是能区分开的。

参考:

Subject: Re: [docbook-apps] Background for programlisting

去试试:

<xsl:attribute name="background-color">
 <xsl:choose>
   <xsl:when test="self::d:programlisting">>#eee</xsl:when>
   <xsl:otherwise>inherit</xsl:otherwise>
 </xsl:choose>
 <xsl:choose>
   <xsl:when test="self::d:screen">>#edc</xsl:when>
   <xsl:otherwise>inherit</xsl:otherwise>
 </xsl:choose>
</xsl:attribute>

结果出错:

CLi@PC-CLI-1 ~/develop/docbook/books/docbook_dev_note/src
$ make pdf
=============================== generating docbook_dev_note fo ==============================
export XML_CATALOG_FILES="/home/develop/docbook/config_root/catalog/catalog.xml" && \
        export XML_DEBUG_CATALOG=1 && \
        xsltproc --xinclude --stringparam callout.graphics 1 --stringparam admon.graphics 1 --stringparam callout.graphics.number.limit 30 --stringparam callout.graphics.extension .svg --stringparam callout.graphics.path D:/tmp/tmp_dev_root/cgwin/home/CLi/develop/docbook/config/images/system/callouts/ --stringparam admon.graphics.extension .svg --stringparam admon.graphics.path D:/tmp/tmp_dev_root/cgwin/home/CLi/develop/docbook/config/images/system/colorsvg/ -o ../output/fo/docbook_dev_note.fo docbook_fo_crl.xsl docbook_dev_note.xml
Resolve: sysID docbook_fo_crl.xsl
-2147483592 Parsing catalog /home/develop/docbook/config_root/catalog/catalog.xml
/home/develop/docbook/config_root/catalog/catalog.xml added to file hash
Resolve URI docbook_fo_crl.xsl
Found URI match docbook_fo_crl.xsl
compilation error: file /home/develop/docbook/config_root/docbook-xsl-ns-1.78.1/fo/common_fo.xsl line 95 element attribute
element attribute only allowed within a template, variable or param
compilation error: file /home/develop/docbook/config_root/docbook-xsl-ns-1.78.1/fo/common_fo.xsl line 95 element attribute
xsltParseStylesheetTop: unknown attribute element
Catalogs cleanup
Free catalog entry crl.ent
Free catalog entry /home/develop/docbook/tools_root/docbook-xsl-ns-1.78.1/
Free catalog entry docbook_html.xsl
Free catalog entry docbook_fo.xsl
Free catalog entry /home/develop/docbook/config_root/docbook-xsl-ns-1.78.1/
Free catalog entry docbook_html_crl.xsl
Free catalog entry chunk_html_crl.xsl
Free catalog entry docbook_htmlhelp_crl.xsl
Free catalog entry docbook_fo_crl.xsl
Free catalog entry docbook_webhelp_crl.xsl
Free catalog entry /home/develop/docbook/config_root/catalog/catalog.xml
Free catalog entry
make: *** [../output/fo/docbook_dev_note.fo] Error 5

11.改为:

<!--============================================================================
verbatim(programlisting/literallayout/...) setting
=============================================================================-->
<xsl:param name="shade.verbatim" select="1"></xsl:param>
<!-- http://lists.oasis-open.org/archives/docbook/200801/msg00009.html -->
<!-- http://www.dpawson.co.uk/docbook/styling/params.html -->
<xsl:attribute-set name="shade.verbatim.style">
    <!-- <xsl:attribute name="border">0</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">Lavender</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">#eee</xsl:attribute> -->
    <xsl:attribute name="background-color">#fefde6</xsl:attribute>
    <xsl:attribute name="padding">1pt</xsl:attribute>

    <xsl:attribute name="background-color">
     <xsl:choose>
       <xsl:when test="self::d:programlisting">>#eee</xsl:when>
       <xsl:otherwise>inherit</xsl:otherwise>
     </xsl:choose>
     <xsl:choose>
       <xsl:when test="self::d:screen">>#edc</xsl:when>
       <xsl:otherwise>inherit</xsl:otherwise>
     </xsl:choose>
    </xsl:attribute>
</xsl:attribute-set>

效果为:

还是全部是橘黄色,没有区分开背景色。

3.改为:

<!--============================================================================
verbatim(programlisting/literallayout/...) setting
=============================================================================-->
<xsl:param name="shade.verbatim" select="1"></xsl:param>
<!-- http://lists.oasis-open.org/archives/docbook/200801/msg00009.html -->
<!-- http://www.dpawson.co.uk/docbook/styling/params.html -->
<xsl:attribute-set name="shade.verbatim.style">
    <!-- <xsl:attribute name="border">0</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">Lavender</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">#eee</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">#fefde6</xsl:attribute> -->
    <xsl:attribute name="padding">1pt</xsl:attribute>

    <xsl:attribute name="background-color">
     <xsl:choose>
       <xsl:when test="self::d:programlisting">>#eee</xsl:when>
       <xsl:otherwise>inherit</xsl:otherwise>
     </xsl:choose>
     <xsl:choose>
       <xsl:when test="self::d:screen">>#edc</xsl:when>
       <xsl:otherwise>inherit</xsl:otherwise>
     </xsl:choose>
    </xsl:attribute>
</xsl:attribute-set>

结果编译fo为pdf会出错:

CLi@PC-CLI-1 ~/develop/docbook/books/docbook_dev_note/src
$ make pdf
=============================== generating docbook_dev_note fo ==============================
export XML_CATALOG_FILES="/home/develop/docbook/config_root/catalog/catalog.xml" && \
        export XML_DEBUG_CATALOG=1 && \
        xsltproc --xinclude --stringparam callout.graphics 1 --stringparam admon.graphics 1 --stringparam callout.graphics.number.limit 30 --stringparam callout.graphics.extension .svg --stringparam callout.graphics.path D:/tmp/tmp_dev_root/cgwin/home/CLi/develop/docbook/config/images/system/callouts/ --stringparam admon.graphics.extension .svg --stringparam admon.graphics.path D:/tmp/tmp_dev_root/cgwin/home/CLi/develop/docbook/config/images/system/colorsvg/ -o ../output/fo/docbook_dev_note.fo docbook_fo_crl.xsl docbook_dev_note.xml
Resolve: sysID docbook_fo_crl.xsl
-2147483592 Parsing catalog /home/develop/docbook/config_root/catalog/catalog.xml
/home/develop/docbook/config_root/catalog/catalog.xml added to file hash
Resolve URI docbook_fo_crl.xsl
Found URI match docbook_fo_crl.xsl
Resolve: pubID crl.ent sysID https://www.crifan.com/files/res/docbook/entity/crl.ent
Found public match crl.ent
Resolve: pubID crl.ent sysID https://www.crifan.com/files/res/docbook/entity/crl.ent
Found public match crl.ent
Resolve: pubID crl.ent sysID https://www.crifan.com/files/res/docbook/entity/crl.ent
Found public match crl.ent
Resolve: pubID crl.ent sysID https://www.crifan.com/files/res/docbook/entity/crl.ent
Found public match crl.ent
Resolve: pubID crl.ent sysID https://www.crifan.com/files/res/docbook/entity/crl.ent
Found public match crl.ent
Resolve: pubID crl.ent sysID https://www.crifan.com/files/res/docbook/entity/crl.ent
Found public match crl.ent
Resolve: pubID crl.ent sysID https://www.crifan.com/files/res/docbook/entity/crl.ent
Found public match crl.ent
Making portrait pages on A4 paper (210mmx297mm)
XPath error : Undefined namespace prefix
Catalogs cleanup
Free catalog entry crl.ent
Free catalog entry /home/develop/docbook/tools_root/docbook-xsl-ns-1.78.1/
Free catalog entry docbook_html.xsl
Free catalog entry docbook_fo.xsl
Free catalog entry /home/develop/docbook/config_root/docbook-xsl-ns-1.78.1/
Free catalog entry docbook_html_crl.xsl
Free catalog entry chunk_html_crl.xsl
Free catalog entry docbook_htmlhelp_crl.xsl
Free catalog entry docbook_fo_crl.xsl
Free catalog entry docbook_webhelp_crl.xsl
Free catalog entry /home/develop/docbook/config_root/catalog/catalog.xml
Free catalog entry
make: *** [../output/fo/docbook_dev_note.fo] Error 10

4.再试试:

<!--============================================================================
verbatim(programlisting/literallayout/...) setting
=============================================================================-->
<xsl:param name="shade.verbatim" select="1"></xsl:param>
<!-- http://lists.oasis-open.org/archives/docbook/200801/msg00009.html -->
<!-- http://www.dpawson.co.uk/docbook/styling/params.html -->
<xsl:attribute-set name="shade.verbatim.style">
    <!-- <xsl:attribute name="border">0</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">Lavender</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">#eee</xsl:attribute> -->
    <xsl:attribute name="background-color">#fefde6</xsl:attribute>
    <xsl:attribute name="padding">1pt</xsl:attribute>

    <xsl:attribute name="background-color">
        <xsl:choose>
        <xsl:when test="self::d:programlisting">>#eee</xsl:when>
        <xsl:when test="self::d:screen">>#edc</xsl:when>
        <xsl:otherwise>inherit</xsl:otherwise>
        </xsl:choose>
    </xsl:attribute>
</xsl:attribute-set>

效果为:

还是没区分背景色。

5.参考:

D:\tmp\tmp_dev_root\cgwin\home\CLi\develop\docbook\tools\docbook-xsl-ns-1.78.1\fo\verbatim.xsl

去试试自己写代码试试。

后来终于用:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:d="http://docbook.org/ns/docbook"
                version='1.0'>
<!--============================================================================
verbatim(programlisting/literallayout/...) setting
=============================================================================-->
<xsl:param name="shade.verbatim" select="1"></xsl:param>
<!-- http://lists.oasis-open.org/archives/docbook/200801/msg00009.html -->
<!-- http://www.dpawson.co.uk/docbook/styling/params.html -->
<xsl:attribute-set name="shade.verbatim.style">
    <!-- <xsl:attribute name="border">0</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">Lavender</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">#eee</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">#fefde6</xsl:attribute> -->
    <xsl:attribute name="padding">1pt</xsl:attribute>
    
    <xsl:attribute name="background-color">
        <xsl:choose>
            <xsl:when test="self::d:programlisting">blue</xsl:when>
            <xsl:when test="self::d:screen">red</xsl:when>
            <xsl:otherwise>gray</xsl:otherwise>
            <!-- <xsl:otherwise>inherit</xsl:otherwise> -->
        </xsl:choose>
    </xsl:attribute>    
    
    <!--
    <xsl:attribute name="background-color">
        <xsl:choose>
            <xsl:when test="d:programlisting">>#eee</xsl:when>
            <xsl:when test="d:screen">>#edc</xsl:when>
            <xsl:otherwise>#fefde6</xsl:otherwise>
        </xsl:choose>
    </xsl:attribute>
    -->
</xsl:attribute-set>

算是可以实现条件判断式的,去设置不同的背景色了:

programinglist and screen different background color

5.但是如果设置颜色的值是:

#eee

#edc

#fefde6

则就出现下面这类错误:

SEVERE: Invalid property value encountered in background-color=">#edc": org.apache.fop.fo.expr.PropertyException: file:/D:/tmp/tmp_dev_root/cgwin/home/CLi/develop/docbook/books/docbook_dev_note/src/../output/fo/docbook_dev_note.fo:4787:44: illegal character; property:'background-color' (See position 4800:426)

6.所以需要去:

搞清楚之前在css中配置的颜色的值,尤其是#edc,到底是什么意思:

【已解决】搞清楚css中颜色值#xxx形式的#edc是什么含义

7.所以去试试:

<!--============================================================================
verbatim(programlisting/literallayout/...) setting
=============================================================================-->
<xsl:param name="shade.verbatim" select="1"></xsl:param>
<!-- http://lists.oasis-open.org/archives/docbook/200801/msg00009.html -->
<!-- http://www.dpawson.co.uk/docbook/styling/params.html -->
<xsl:attribute-set name="shade.verbatim.style">
    <!-- <xsl:attribute name="border">0</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">Lavender</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">#eee</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">#fefde6</xsl:attribute> -->
    <xsl:attribute name="padding">1pt</xsl:attribute>
    
    <xsl:attribute name="background-color">
        <xsl:choose>
            <xsl:when test="self::d:programlisting">#eeeeee</xsl:when>
            <xsl:when test="self::d:screen">#eeddcc</xsl:when>
            <xsl:otherwise>#fefde6</xsl:otherwise>
            <!-- <xsl:otherwise>inherit</xsl:otherwise> -->
        </xsl:choose>
    </xsl:attribute>    
    
    <!--
    <xsl:attribute name="background-color">
        <xsl:choose>
            <xsl:when test="d:programlisting">>#eee</xsl:when>
            <xsl:when test="d:screen">>#edc</xsl:when>
            <xsl:otherwise>#fefde6</xsl:otherwise>
        </xsl:choose>
    </xsl:attribute>
    -->
</xsl:attribute-set>

是否可以。

结果终于可以了:

in pdf programinglist and screen is expected background color

8.最后,由于在:

【已解决】搞清楚css中颜色值#xxx形式的#edc是什么含义

搞清楚了,xsl是支持#xxx的,所以去改为:

<!--============================================================================
verbatim(programlisting/literallayout/...) setting
=============================================================================-->
<xsl:param name="shade.verbatim" select="1"></xsl:param>
<!-- http://lists.oasis-open.org/archives/docbook/200801/msg00009.html -->
<!-- http://www.dpawson.co.uk/docbook/styling/params.html -->
<xsl:attribute-set name="shade.verbatim.style">
    <!-- <xsl:attribute name="border">0</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">Lavender</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">#eee</xsl:attribute> -->
    <!-- <xsl:attribute name="background-color">#fefde6</xsl:attribute> -->
    <xsl:attribute name="padding">1pt</xsl:attribute>
    
    <xsl:attribute name="background-color">
        <xsl:choose>
            <xsl:when test="self::d:programlisting">#eee</xsl:when>
            <xsl:when test="self::d:screen">#edc</xsl:when>
            <xsl:otherwise>#fefde6</xsl:otherwise>
            <!-- <xsl:otherwise>inherit</xsl:otherwise> -->
        </xsl:choose>
    </xsl:attribute>
</xsl:attribute-set>

效果也是一样的。

9.然后,继续去试试别的参数。

先去看看,equation的背景色。

结果发现css中是之前自己的设置:

D:\tmp\tmp_dev_root\cgwin\home\CLi\develop\docbook\config\docbook-xsl-ns-1.78.1\fo\common_fo.xsl

中的:

<!--============================================================================
equation setting
=============================================================================-->
<!-- TODO:tmp here only valid for FO(PDF), not take effect for HTML, need later fix it -->
<!-- <xsl:attribute-set name="equation.properties" use-attribute-sets="formal.object.properties"></xsl:attribute-set> -->
<xsl:attribute-set name="equation.properties" use-attribute-sets="formal.object.properties">
    <!-- http://www.dpawson.co.uk/docbook/styling/params.html#d2221e282 -->
    <!-- <xsl:attribute name="border-color">thin black ridge</xsl:attribute> -->
    <!-- http://raibledesigns.com/wiki/Wiki.jsp?page=DocBook -->
    <!-- <xsl:attribute name="border">0</xsl:attribute> -->
    <!-- <xsl:attribute name="border-style">solid</xsl:attribute> -->
    <!-- <xsl:attribute name="border-width">.1mm</xsl:attribute> -->
    <xsl:attribute name="background-color">Lavender</xsl:attribute>
    <!-- <xsl:attribute name="border-width">0.5pt</xsl:attribute> -->
    <xsl:attribute name="padding">1pt</xsl:attribute>
</xsl:attribute-set>

效果还是之前的:

pdf equation background is old ok

无需变化。

10.还是去继续看看,那个example的背景色。

后来查到了,是之前自己在:

D:\tmp\tmp_dev_root\cgwin\home\CLi\develop\docbook\config\docbook-xsl-ns-1.78.1\fo\common_fo.xsl

中的:

<!--============================================================================
formal(figure/table/equation/example/...) setting
=============================================================================-->
<!-- http://www.sagehill.net/docbookxsl/TitleFontSizes.html#FormalTitleProperties -->
<xsl:attribute-set name="formal.title.properties" 
                   use-attribute-sets="normal.para.spacing">
  <xsl:attribute name="font-weight">bold</xsl:attribute>
  <xsl:attribute name="font-size">12pt</xsl:attribute>
  <xsl:attribute name="hyphenate">false</xsl:attribute>
  <xsl:attribute name="space-after.minimum">0.4em</xsl:attribute>
  <xsl:attribute name="space-after.optimum">0.6em</xsl:attribute>
  <xsl:attribute name="space-after.maximum">0.8em</xsl:attribute>
  <xsl:attribute name="text-align">center</xsl:attribute>
</xsl:attribute-set>

然后另外看到:

D:\tmp\tmp_dev_root\cgwin\home\CLi\develop\docbook\tools\docbook-xsl-ns-1.78.1\fo\formal.xsl

中有:

    <xsl:when test="self::d:example">
      <fo:block id="{$id}"
                xsl:use-attribute-sets="example.properties">
        <xsl:if test="$keep.together != ''">
          <xsl:attribute name="keep-together.within-column"><xsl:value-of
                          select="$keep.together"/></xsl:attribute>
        </xsl:if>
        <xsl:copy-of select="$content"/>
      </fo:block>

所以去单独加:

<xsl:attribute-set name="example.properties">
  <xsl:attribute name="background-color">#fefde6</xsl:attribute>
</xsl:attribute-set>

效果是:是希望的颜色:

pdf example background is expected light

 

11.另外,再去设置note,tip,important,warning,caution的背景色:

看到:

D:\tmp\tmp_dev_root\cgwin\home\CLi\develop\docbook\tools\docbook-xsl-ns-1.78.1\fo\admon.xsl

中有:

<xsl:template name="graphical.admonition">
  <xsl:variable name="id">
    <xsl:call-template name="object.id"/>
  </xsl:variable>
  <xsl:variable name="graphic.width">
     <xsl:apply-templates select="." mode="admon.graphic.width"/>
  </xsl:variable>

  <fo:block id="{$id}"
            xsl:use-attribute-sets="graphical.admonition.properties">
    <fo:list-block provisional-distance-between-starts="{$graphic.width} + 18pt"
                    provisional-label-separation="18pt">
      <fo:list-item>
          <fo:list-item-label end-indent="label-end()">
            <fo:block>
              <fo:external-graphic width="auto" height="auto"
                                         content-width="{$graphic.width}" >
                <xsl:attribute name="src">
                  <xsl:call-template name="admon.graphic"/>
                </xsl:attribute>
              </fo:external-graphic>
            </fo:block>
          </fo:list-item-label>
          <fo:list-item-body start-indent="body-start()">
            <xsl:if test="$admon.textlabel != 0 or d:title or d:info/d:title">
              <fo:block xsl:use-attribute-sets="admonition.title.properties">
                <xsl:apply-templates select="." mode="object.title.markup">
		  <xsl:with-param name="allow-anchors" select="1"/>
		</xsl:apply-templates>
              </fo:block>
            </xsl:if>
            <fo:block xsl:use-attribute-sets="admonition.properties">
              <xsl:apply-templates/>
            </fo:block>
          </fo:list-item-body>
      </fo:list-item>
    </fo:list-block>
  </fo:block>
</xsl:template>

<xsl:template name="nongraphical.admonition">
  <xsl:variable name="id">
    <xsl:call-template name="object.id"/>
  </xsl:variable>

  <fo:block id="{$id}"
            xsl:use-attribute-sets="nongraphical.admonition.properties">
    <xsl:if test="$admon.textlabel != 0 or d:title or d:info/d:title">
      <fo:block keep-with-next.within-column='always'
                xsl:use-attribute-sets="admonition.title.properties">
         <xsl:apply-templates select="." mode="object.title.markup">
	   <xsl:with-param name="allow-anchors" select="1"/>
	 </xsl:apply-templates>
      </fo:block>
    </xsl:if>

    <fo:block xsl:use-attribute-sets="admonition.properties">
      <xsl:apply-templates/>
    </fo:block>
  </fo:block>
</xsl:template>

所以去设置:

<xsl:attribute-set name="admonition.properties">
    <xsl:attribute name="background-color">
        <xsl:choose>
            <xsl:when test="self::d:note">#eee</xsl:when>
            <xsl:when test="self::d:tip">#d8ecd6</xsl:when>
            <xsl:when test="self::d:warning">#f8e8e8</xsl:when>
            <xsl:when test="self::d:caution">#FFC</xsl:when>
            <xsl:otherwise>#fefde6</xsl:otherwise>
        </xsl:choose>
    </xsl:attribute>
</xsl:attribute-set>

结果是:

背景色可以生效:

pdf warning background

pdf note background

pdf tip background

还不错。

12.剩下的,其他的需要转化为pdf中的配置,还有一些,比如标题字体都是褐色之类的。

有空再折腾。

 

【总结】

转载请注明:在路上 » 【记录】docbook中手动将css转换为fo即pdf的xsl参数配置

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.391 seconds, using 22.09MB memory