当前位置:  开发笔记 > 编程语言 > 正文

如何在Java中使用XML创建PDF?

如何解决《如何在Java中使用XML创建PDF?》经验,为你挑选了3个好方法。

目前,我正在用Java创建一个XML文件,并通过使用XSL/XSLT进行转换将其显示在JSP页面中.现在我需要获取该XML文件并在PDF中显示相同的信息.有没有办法通过使用某种XSL文件来做到这一点?

我见过iText Java-PDF库,但我找不到任何方法可以将它与XML和样式表一起使用.

任何援助将不胜感激.提前致谢!



1> Levent Divil..:

A - 解释

您应该使用Apache FOP框架生成pdf输出.只需以xml格式提供数据并使用xsl-fo文件呈现页面,并在此xsl-fo文件中指定边距,页面布局等参数.

我将提供一个简单的演示,我使用maven构建工具来收集所需的jar文件.请在页面末尾通知,pdf中嵌入了svg图形.我还想证明你可以在pdf中嵌入svg图形.

B - 示例XML输入数据




    
        User Bill Data
        Thursday December 9 2016 00:04:29
    
    
        John Doe
        34239
        123AD329248
        17.84
    
    
        Michael Doe
        54823
        942KFDSCW322
        34.50
    
    
        Jane Brown
        66742
        ABDD324KKD8
        69.36
    

C - XSL-FO模板



    
    
        
            
                
                    
                    
                
            
            
                
                    
                        
                        
                        
                        
                            
                                
                                    
                                        Bill Id:
                                        , Date: 
                                    
                                
                                
                                    
                                        XXX COMPANY
                                    
                                    
                                
                                
                                    
                                        
                                    
                                    Page  of 
                                    
                                
                            
                        
                    
                
                
                    MONTHLY BILL REPORT
                    
                        
                        
                        
                        
                        
                            
                                
                                    Full Name
                                
                                
                                    Postal Code
                                
                                
                                    National ID
                                
                                
                                    Payment
                                
                            
                            
                                
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                    
                                    
                                        
                                            
                                        
                                    
                                
                            
                        
                    
                    
                        
                            
                                
                            
                        
                    
                
            
        
    

D - 项目目录结构

在此输入图像描述

E - Pom文件


    4.0.0
    com.levent.fopdemo
    apache-fop-demo
    jar
    1.0-SNAPSHOT
    apache-fop-demo
    http://maven.apache.org

    
        2.1
    

          
        
        
            org.apache.xmlgraphics
            fop
            ${fop.version}
        
    

    
        Apache Fop Demo

        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.5.1
                
                    1.8
                    1.8
                
            
        
    

F - 演示代码:PdfGenerationDemo.java

package com.levent.fopdemo;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;

public class PdfGenerationDemo 
{
    public static final String RESOURCES_DIR;
    public static final String OUTPUT_DIR;

    static {
        RESOURCES_DIR = "src//main//resources//";
        OUTPUT_DIR = "src//main//resources//output//";
    }

    public static void main( String[] args )
    {
        try {
            convertToPDF();
        } catch (FOPException | IOException | TransformerException e) {
            e.printStackTrace();
        }
    }

    public static void convertToPDF() throws IOException, FOPException, TransformerException {
        // the XSL FO file
        File xsltFile = new File(RESOURCES_DIR + "//template.xsl");
        // the XML file which provides the input
        StreamSource xmlSource = new StreamSource(new File(RESOURCES_DIR + "//data.xml"));
        // create an instance of fop factory
        FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
        // a user agent is needed for transformation
        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
        // Setup output
        OutputStream out;
        out = new java.io.FileOutputStream(OUTPUT_DIR + "//output.pdf");

        try {
            // Construct fop with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

            // Setup XSLT
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new StreamSource(xsltFile));

            // Resulting SAX events (the generated FO) must be piped through to
            // FOP
            Result res = new SAXResult(fop.getDefaultHandler());

            // Start XSLT transformation and FOP processing
            // That's where the XML is first transformed to XSL-FO and then
            // PDF is created
            transformer.transform(xmlSource, res);
        } finally {
            out.close();
        }
    }
}

G - 样本输出:output.pdf

在此输入图像描述



2> Bogdan Maxim..:

您可以使用XSL格式化对象.这里有一些关于如何做的好文章:

http://www.xml.com/pub/a/2001/01/17/xsl-fo/index.html

http://www.xml.com/pub/a/2001/01/24/xsl-fo/index.html?page=1

http://www.javaworld.com/javaworld/jw-04-2006/jw-0410-html.html



3> hakan..:

您也可以在这里查看apache项目

推荐阅读
小妖694_807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有