asciidoctor / asciidoctorj-pdf

AsciidoctorJ PDF bundles the Asciidoctor PDF RubyGem (asciidoctor-pdf) so it can be loaded into the JVM using JRuby.
Apache License 2.0
36 stars 17 forks source link

Could not create PDF #37

Closed jenskelkel closed 1 year ago

jenskelkel commented 4 years ago

Try to creat e PDF from Java. If I use the convert Method I get null as result. The convertFile Methode trow an Exception

Exception in thread "main" org.asciidoctor.jruby.internal.AsciidoctorCoreException: org.jruby.exceptions.TypeError: (TypeError) cannot convert instance of class org.jruby.gen.RubyObject50 to class java.lang.String at org.asciidoctor.jruby.internal.JRubyAsciidoctor.convert(JRubyAsciidoctor.java:318) at org.asciidoctor.jruby.internal.JRubyAsciidoctor.convert(JRubyAsciidoctor.java:281) at org.asciidoctor.jruby.internal.JRubyAsciidoctor.convert(JRubyAsciidoctor.java:350) at App.main(App.java:24) Caused by: org.jruby.exceptions.TypeError: (TypeError) cannot convert instance of class org.jruby.gen.RubyObject50 to class java.lang.String

Here my simple Sourcecode:

public static void main(String[] args) throws IOException { Asciidoctor asciidoctor= Asciidoctor.Factory.create(); File file=new File("C:/temp/test.adoc");

    FileReader reader = new FileReader(file);

    StringWriter writer= new StringWriter();
    Map<String, Object> options = options()
            .inPlace(true)
            .backend("pdf")
            .asMap();
    asciidoctor.convert(reader,writer,options);

    System.out.println(writer.getBuffer());

}

Perhaps someone know the solution

Thanks.

Jens

robertpanzer commented 4 years ago

Can you please give some details on the versions of asciidoctorj and asciidoctorj-pdf that you were using?

jenskelkel commented 4 years ago

Here my pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0
<groupId>com.jenskelkel.test</groupId>
<artifactId>asciidoc</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
    <java.version>1.11</java.version>
    <maven.compiler.source>1.11</maven.compiler.source>
    <maven.compiler.target>1.11</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>org.asciidoctor</groupId>
        <artifactId>asciidoctorj</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.asciidoctor</groupId>
        <artifactId>asciidoctorj-pdf</artifactId>
        <version>1.5.0-beta.8</version>
    </dependency>
</dependencies>

robertpanzer commented 4 years ago

Yes, that won’t work. PDF is not necessarily a string, therefore it doesn’t make much sense to convert into a StringWriter. If you want to have the pdf in memory please use the option to_stream with a ByteArrayOutputStream instead: https://github.com/asciidoctor/asciidoctorj/blob/master/asciidoctorj-api/src/main/java/org/asciidoctor/OptionsBuilder.java

I see that the API is a bit misleading here, but currently passing in a StringWriter can only be used for backends producing „plain text“ like HTML, docbook etc

You can also find an example for this here: https://github.com/asciidoctor/asciidoctorj/blob/master/asciidoctorj-distribution/src/test/groovy/org/asciidoctor/diagram/WhenAPdfDocumentIsRenderedToStream.groovy

jenskelkel commented 4 years ago

Thanks, the OptionBuilder.toStream was the puzzle that I am missing.

abelsromero commented 1 year ago

I understand the question is resolved. If not, feel free to address further questions to chat.asciidoctor.org