Use the Apache XML project FOP to generate a PDF file (on)

xiaoxiao2021-03-06  51

Use the Apache XML project FOP to generate a PDF file (on)

I. Foreword: In the project, I have to save the document into a PDF file. I have found a long time online, I finally discovered that Apache's FOP project has this function.

Second, suitable for the reader object: I want to learn to learn!

Third, the introduction: FOP is called Formatting Objects Processor, translated into formatted object processors, simple points, it is a print format drive, which can easily generate corresponding formatted files. FOP now supported formatted files include PDF, PCL, PS, SVG, XML, Print, AWT, MIF AND TXT. Most mainly useful PDF files we are familiar with. The following uses a few examples of how FOP do it.

Fourth, content:

1. Install: From http://xml.apache.org/dist/fop/, download a file package FOP-0.20.5rc-bin.tar.gz file package decompression, generate a folder assumption to F: / FOP

Directory structure

F: / FOP directory

04: 00P

.

12/19/2002 04: 00p

..

12/11/2002 12: 26A 46, 124 CHANGES

12/11/2002 12: 26A 679 FOP.BAT

12/11/2002 12: 26A 2,446 FOP.SH

12/11/2002 12: 26A 2,675 license

12/11/2002 12: 26A 1,248 Readme

12/11/2002 12: 26A 597 ReleaseNotes.html

12/11/2002 12: 26A 884 Status

12/19/2002 04: 00p

lib

12/19/2002 04: 00p

DOCS

12/19/2002 04: 00p

Contrib

12/19/2002 04: 00p

confed

12/19/2002 04: 00p

build

12/11/2002 12: 26A 14, 899 BUGS.HTML

Where the lib directory consists of several needs:

(A) XML's API: XML-Apis.jar

(B) Apache's XML parser: Xercesimpl-2.2.1.jar

(c) Apache's XSLT processor: Xalan-2.4.1.jar

(d) Apache's SVG library file that makes PDF colorful pictures: Batik.jar

(e) Avalon Framework library file: avalon-framework-cvs-20020806.jar

Add them to classpath;

Add: f: / FOP in the system's PATH variable

A simple test:

Mr. became a FO file:

Hello World!

Open the DOS window: Type

FOP HelloWorld.fo HelloWorld.pdf

If there is no problem with the installation configuration, you will see a helloWorld.pdf file in the current directory.

2. Let's take a look at how to use the FOP library resources in the Java program to generate a PDF file, which will encounter several situations: (1) XLS-foàpdf; (2) xmlàxsl-foàpdf; (3) javaàxmlàxsl-foàpdf

(1) XLS-foàpdf:

XSL is a standard XML document for W3C, which includes two types: XSLT and XSLFO, and XSLFO EXTENSIBLE STYLESHEET LANGUAGE FORMATTING OBJECTS, which is also an extension style language file with formatted objects. A function using the FOP can generate a PDF file directly. The processing flow is as follows:

XLS-FO

PDF

FOP

// Example: Examplefo2pdf.java

Import java.io.file;

Import java.io.ioException;

Import Java.io.InputStream;

Import java.io.outputstream;

// SAX

Import org.xml.sax.inputsource;

// Avalon

Import org.apache.avalon.framework.exceptionUtil;

Import org.apache.avalon.framework.logger.logger;

Import org.apache.avaalon.framework.logger.consolelogger;

// FOP

Import org.apache.fop.apps.driver;

Import org.apache.fop.apps.fopexception;

Import org.apache.fop.Messaging.MessageHandler;

/ **

* This class demonstrates the conversion of an fo file to pdf using fop. * /

Public class examplefo2pdf {

Public Void Convertfo2PDF (File Fo, File PDF) Throws oException, FOPEXCEPTION {

// Construct Driver

Driver driver = new driver ();

// setup logger

Logger logger = new consolelogger (consoleLogger.Level_INFO);

Driver.setLogger (Logger);

MessageHandler.SetScreenLogger (Logger);

// setup renderer (Output Format)

Driver.setrenderer (driver.render_pdf);

// setup OUTPUT

OutputStream out = new java.io.fileoutputstream (PDF);

Try {

Driver.setOutputStream (OUT);

// setup input

InputStream in = new java.io.fileinputStream (fo);

Try {

Driver.setInputSource (New InputSource (in));

// Process Fo

Driver.run ();

} finally {

In.Close ();

}

} finally {

Out.close ();

}

}

Public static void main (String [] args) {

Try {

System.out.println ("FOP EXAMPLEFO2PDF / N");

System.out.println ("preplay ...");

// setup Directories

FILE Basedir = New File (".");

// setup Input and Output Files

File Fofile = New File (Basedir, "HelloWorld.fo");

File pdffile = new file (basedir, "resultfo2pdf.pdf");

System.out.println ("INPUT: XSL-FO (" FOFILE ")")

System.out.println ("OUTPUT: PDF (" PDFFILE ")")

System.out.println ();

System.out.println ("Transforming ...");

Examplefo2pdf app = new examplefo2pdf ();

App.convertfo2PDF (FOFILE, PDFFILE);

System.out.println ("Success!");

} catch (exception e) {

System.rr.println (ExceptionUtil.PrintStackTrace (e));

System.exit (-1);

}

}

}

Compile the above program to convert the XMLFO file helloworld.fo to Resultfo2PDF.PDF

Reference: http://xml.apache.org/

Author E-mail: jasea@sina.com

转载请注明原文地址:https://www.9cbs.com/read-116831.html

New Post(0)