Java Generate a PDF with flying Saucer

2019-09-14 11:32发布

问题:

The jar i am using is from the maven repo

The code i am testing is from a previous stack solution

I fear it might be outdated because the libraries are depreciated. When testing the following code:

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;

import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;

public class test
{

    public static void main(String[] args)
    {
        ITextRenderer renderer = new ITextRenderer();

        // if you have html source in hand, use it to generate document object
        renderer.setDocumentFromString( "C:/Users/Goran/Documents/Documents/Development/workspace/FlyingSaucer/data/input/report.xhtml" );
        renderer.layout();

        String fileNameWithPath = "C:/Users/Goran/Documents/Documents/Development/workspace/FlyingSaucer/data/output/" + "PDF-FromHtmlString.pdf";
        FileOutputStream fos = new FileOutputStream( fileNameWithPath );
        renderer.createPDF( fos );
        fos.close();

        System.out.println( "File 2: '" + fileNameWithPath + "' created." );
    }

}

I have the following error this this line: renderer.createPDF( fos );

The type com.lowagie.text.DocumentException cannot be resolved. It is indirectly referenced from required .class files

Anyone able to shed some light on this, or suggest the best way to create a pdf document in java? I have both XML and XHTML documents available.

EDIT When i add the com.lowagie.text library from the repo

Exception in thread "main" java.lang.NoClassDefFoundError: org/xhtmlrenderer/extend/UserAgentCallback
    at test.main(test.java:16)
Caused by: java.lang.ClassNotFoundException: org.xhtmlrenderer.extend.UserAgentCallback
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

Am i even using the right libraries / the most up to date ones?

Can someone who has the most recent libraries or know a repo where they are stored link them?

EDIT 2 Abandoned idea of using flying saucer and using ApacheFop as suggested. Current error is:

Mar 10, 2016 9:58:23 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,400".
Mar 10, 2016 9:58:23 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Font "ZapfDingbats,normal,700" not found. Substituting with "ZapfDingbats,normal,400".
Mar 10, 2016 9:58:23 PM org.apache.fop.events.LoggingEventListener processEvent
INFO: Rendered page #1.

回答1:

I created a simple Maven project with the following depencency:

    <dependency>
        <groupId>org.xhtmlrenderer</groupId>
        <artifactId>core-renderer</artifactId>
        <version>R8</version>
    </dependency>

I created a test class with excatly the code that you posted above and was able to compile it without issue. This is what Eclipse shows as the Maven dependencies:

Note that I had to change this line for the code to work:

renderer.setDocumentFromString("<html><body><strong>Hello</strong> <em>world</em>!</body></html>");

... since the string should be the HTML content itself any not the path to the file.

The PDF looked like this:

I hope that helps...




回答2:

I am using the exact same dependency as you mentioned, so that should not be the problem.

However, you are definitely using the method setDocumentFromString in a wrong way. Instead of:

renderer.setDocumentFromString( "C:/some/path/report.xhtml" );

the input parameter must be the HTML itself, NOT the path to it. Like this:

renderer.setDocumentFromString( "<html><body>some content</body></html>" );

Alternatively, (and preferably) use the setDocument method which takes as input a path in form of String or a File. see JavaDoc