Spring REST 文档:如何输出 PDF 而不是 HTML?未解决的指令

Spring REST Docs: How to output PDF instead of HTML? Unresolved directive

我通读了 Spring REST Docs 的文档,并能够从片段中生成一个 HTML 文件,通过向插件添加另一个执行块,我也可以生成一个 pdf:

<execution>
    <id>output-pdf</id>
    <phase>prepare-package</phase>
    <goals>
        <goal>process-asciidoc</goal>
    </goals>
    <configuration>
        <backend>pdf</backend>
        <doctype>book</doctype>
        <attributes>
            <snippets>${project.build.directory}/generated-snippets</snippets> 
            <icons>font</icons>
            <pagenums/>
            <toc/>
            <idprefix/>
            <idseparator>-</idseparator>
        </attributes>
    </configuration>
</execution>

问题是生成的 pdf 不包含 Spring REST 文档生成的任何片段,取而代之的是:

Unresolved directive in api-doc.adoc - include::{snippets}/request-parts.adoc[]
Unresolved directive in api-doc.adoc - include::{snippets}/response-body.adoc[]
Unresolved directive in api-doc.adoc - include::{snippets}/response-fields.adoc[]

它似乎没有找到片段的位置,所以它们没有被渲染。

听起来你已经很接近了。请确保将 asciidoctorj-pdf 和 asciidoctorj 添加为 asciidoctor-maven-plugin 的依赖项。以下 Maven 片段显示了此设置。此配置生成 HTML 和 PDF 输出。它可以通过删除另一个执行块来减少到任何一个。

<plugin>
    <groupId>org.asciidoctor</groupId>
    <artifactId>asciidoctor-maven-plugin</artifactId>
    <version>1.5.6</version>
    <dependencies>
        <dependency>
            <groupId>org.asciidoctor</groupId>
            <artifactId>asciidoctorj-pdf</artifactId>
            <version>1.5.0-alpha.16</version>
        </dependency>
        <dependency>
            <groupId>org.asciidoctor</groupId>
            <artifactId>asciidoctorj</artifactId>
            <version>1.5.6</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>generate-docs</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>process-asciidoc</goal>
            </goals>
            <configuration>
                <backend>html</backend>
                <doctype>book</doctype>
                <sourceHighlighter>highlightjs</sourceHighlighter>
            </configuration>
        </execution>
        <execution>
            <id>generate-pdf-docs</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>process-asciidoc</goal>
            </goals>
            <configuration>
                <backend>pdf</backend>
                <sourceHighlighter>coderay</sourceHighlighter>
            </configuration>
        </execution>
    </executions>
</plugin>

这个设置对我有用。目前我手头没有 Gradle 示例。