使用 PDF 插件

Maven PDF 插件允许您生成文档的 PDF 文档。

从命令行

无需任何额外配置即可从命令行调用 PDF 插件执行。与其他插件一样,要运行 PDF 插件,您可以使用:

  mvn pdf:pdf

其中第一个pdf指的是插件的别名,第二个pdf指的是插件目标。

默认情况下,pdf 将在target/pdf/目录中生成。

备注

  1. 默认情况下,PDF 插件会生成一个 PDF 文档,其中汇总了您的所有站点文档。如果要单独生成每个站点文档,则需要在命令行中添加-Daggregate=false 。
  2. 默认情况下,PDF 插件使用FOP实现。该插件还支持iText实现,您只需要在命令行中添加-Dimplementation=itext 即可。

作为构建过程的一部分

PDF 插件可以放入项目的 pom.xml 中,以便在每次构建项目时执行。以下是每次构建项目时在站点阶段运行 PDF 插件的示例配置(放入pom.xml<build>部分中的<plugins>列表):

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-pdf-plugin</artifactId>
  <executions>
    <execution>
      <id>pdf</id>
      <phase>site</phase>
      <goals>
        <goal>pdf</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.reporting.outputDirectory}</outputDirectory>
        <includeReports>false</includeReports>
      </configuration>
    </execution>
  </executions>
</plugin>

注意:在这种情况下,pdf 插件与 Maven Site 插件相结合,以将站点文档和 pdf 生成到默认输出站点目录中,即target/site。你只需要调用mvn site

文档描述符

默认情况下,pdf 插件处理站点插件的site.xml中指定的所有源文件。您可以使用文档描述符(默认为src/site/pdf.xml)自定义要包含的文件的顺序。下面给出一个例子:

<document xmlns="http://maven.apache.org/DOCUMENT/1.0.1"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/DOCUMENT/1.0.1 http://maven.apache.org/xsd/document-1.0.1.xsd"
  outputName="maven-pdf-plugin">

  <meta>
    <title>Maven PDF Plugin</title>
    <author>The Apache Maven Project</author>
  </meta>

  <toc name="Table of Contents">
    <item name="Introduction" ref="index.apt"/>
    <item name="Usage" ref="usage.apt.vm"/>
    <item name="Filtering Document Descriptor" ref="examples/filtering.apt"/>
    <item name="Configuring Reports" ref="/examples/configuring-reports.xml.vm"/>
    <item name="Limitations" ref="limitations.apt"/>
    <item name="FAQ" ref="faq.fml"/>
  </toc>

  <cover>
    <coverTitle>${project.name}</coverTitle>
    <coverSubTitle>v. ${project.version}</coverSubTitle>
    <coverType>User Guide</coverType>
    <projectName>${project.name}</projectName>
    <projectLogo>http://maven.apache.org/images/maventxt_logo_200.gif</projectLogo>
    <companyName>The Apache Software Foundation</companyName>
    <companyLogo>http://www.apache.org/images/asf_logo_wide.png</companyLogo>
  </cover>
</document>

如果没有给出封面元素,信息仅用于 pdf 封面。toc生成目录并指定要包含在 pdf 中的文件的顺序。有关文件格式的完整描述,请参阅文档模型参考

备注

  1. Fo/iText 接收器仅使用少数文档元数据,如作者、机密、日期和标题。
  2. 文档描述符支持过滤示例中描述的过滤

国际化

PDF 插件能够生成国际化的 pdf,类似于网站创建

要启用多个语言环境,请将类似于以下内容的配置添加到您的 POM:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pdf-plugin</artifactId>
        <version>1.5.1</version>
        <configuration>
          <locales>en,fr</locales>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

这将生成两份 pdf,一份英文和一份法文。与站点插件一样,如果en是您当前的语言环境,那么它将在站点的根目录中生成,并在fr/子目录中提供该站点的法语翻译副本。

以下是站点和 pdf 插件的完整国际化目录布局:

+- src/
   +- site/
      +- apt/
      |  +- index.apt     (Default version)
      |
      +- fr/
      |  +- apt/
      |     +- index.apt  (French version)
      |
      +- site.xml         (Default site descriptor)
      +- site_fr.xml      (French site descriptor)
      +- pdf.xml          (Default pdf descriptor)
      +- pdf_fr.xml       (French pdf descriptor)

特定 FOP 配置属性

用于使用 FOP 实现的 pdf 转换的所有布局属性都是从默认配置文件中读取的。可以使用位于src/site/resources/的自定义配置文件pdf-config.xml覆盖此文件中的属性。

此文件的格式必须与原始默认配置文件完全相同。但是,您只需要指定要覆盖的属性,例如更改预格式化文本的字体大小,您可以使用:

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

  <xsl:attribute-set name="body.pre" use-attribute-sets="base.pre.style">
    <xsl:attribute name="font-size">8pt</xsl:attribute>
  </xsl:attribute-set>

</xsl:stylesheet>