asciidoctor / asciidoctorj

:coffee: Java bindings for Asciidoctor. Asciidoctor on the JVM!
http://asciidoctor.org
Apache License 2.0
627 stars 172 forks source link

Title page in PDF has not been generated via byte array stream #1058

Closed jasminesoap2222 closed 3 years ago

jasminesoap2222 commented 3 years ago

The following simple contents has been converted to the pdf file with title page using asciidoctorj 2.5.2 and asciidoctorj-pdf 1.6.0. test1_file.pdf However, in case of output the converted data to the byte array stream, there is no title page.test1_stream.pdf

[contents]

:doctype: book

= Test Title: my subtitle
:author: alice
:revdate: 10.2021

This is a test adoc.

[code]

Options options = Options.builder().build();
options.setBackend("pdf");
options.setSafe(SafeMode.SERVER);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
options.setToStream(bos);

String[] res = asciidoctor.convert(contents, options)

Any advice would be appreciated.

abelsromero commented 3 years ago

If memory serves, stream method assumes the content is to be embedded and disables the header and the footer. Just enable it with headerFooter(true).

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Options options = Options.builder()
            .backend("pdf")
            .safe(SafeMode.SERVER)
            .docType("book")
            .toStream(bos)
            .headerFooter(true)
            .build();

        asciidoctor.convert(contents, options);

        Files.write(Path.of("toStream.pdf"), bos.toByteArray());
jasminesoap2222 commented 3 years ago

Thank you. Your help has solved it.

Moreover, I have met the other problems. The generated title page has no background and logo images. How does the asciidoctorj runtime load the image files in "resources/asciidoctor/images" folder on the web server ?

[contents]

:doctype: book

= Test Title
:author: Alice
:revdate: 10.2021
:title-logo-image: image:asciidoctor/images/logo.png[]
:title-page-background-image: image:asciidoctor/images/bg.png[]

Could I set the embed base64 image data to "title-logo-image" and "title-page-background-image" ?

jasminesoap2222 commented 3 years ago

Moreover, I want to remove the header and footer in the title page only. Please tell me how.

abelsromero commented 3 years ago

For fine-grained customizations in PDF you should create a theme https://github.com/asciidoctor/asciidoctor-pdf/blob/main/docs/theming-guide.adoc. Here is an example using maven as build tool https://github.com/asciidoctor/asciidoctor-maven-examples/tree/main/asciidoctor-pdf-with-theme-example. The build solution is important since that's the one to handle paths, if you are doing the build by hand with custom code you can use anything. For example, you should be fine just getting the absolute path as any Java classpath resource, or setting some root path config.

PS (edit): I noted in your examples you place the attributes on top of the title. Not sure if this is just short writting, but if you are doing that, that's an invalid configuration. My personal suggestion is to pass document level attributes using the API as in this example

        Options options = Options.builder()
            .backend("pdf")
            .safe(SafeMode.SERVER)
            .docType("book")
            .toFile(file)
            .headerFooter(true)
            .attributes(Attributes.builder()
                .attribute("title-logo-image", "src/main/resources/asciidoctor_logo.png")
                .build())
            .build();

Note the path will only work when running from IDE.

mojavelinux commented 3 years ago

Moreover, I want to remove the header and footer in the title page only.

FYI, that's already the default behavior. The header/footer would only be displayed on the title if configured in your theme file. See https://github.com/asciidoctor/asciidoctor-pdf/blob/v1.6.x/docs/theming-guide.adoc#key-prefix-running-content

jasminesoap2222 commented 3 years ago

They have been solved. Thank you.