从XML或HTML生成PDF文件。

时间:2021-04-13 22:18:28

Are there any API/solution to generate PDF report from XML file data and definition. For example the XML definition/data could be:

是否有API/解决方案可以从XML文件数据和定义生成PDF报告。例如,XML定义/数据可以是:

<pdf>
    <paragraph font="Arial">Title of report</paragraph>
</pdf>

Converting HTML to PDF will be also a good solution I feel.

将HTML转换成PDF也是一个很好的解决方案。

Currently we write Java code using iText API. I want to externalize the code so that non-technical person can edit and make changes.

目前,我们使用iText API编写Java代码。我希望外部化代码,以便非技术人员可以编辑和更改。

5 个解决方案

#1


10  

Have a look at Apache FOP. Use an XSLT stylesheet to convert the XML (or XHTML) into XSL-FO. Then use FOP to read the XSL-FO document and format it to a PDF document (see Hello World with FOP).

看看Apache FOP。使用XSLT样式表将XML(或XHTML)转换为XSL-FO。然后使用FOP读取XSL-FO文档并将其格式化为PDF文档(参见使用FOP的Hello World)。

#2


4  

iText has a facility for generating PDFs from XML (and HTML, I think). Here is the DTD, but I found it difficult to sort out. Aside from that, I never found any good documentation on what is supported. My approach was to look at the source for SAXiTextHandler and ElementTags to figure out what was acceptable. Although not ideal, it is pretty straight-forward.

iText具有从XML(和HTML)生成pdf的功能。这里是DTD,但是我发现很难分类。除此之外,我从未找到任何关于所支持内容的好的文档。我的方法是查看SAXiTextHandler和ElementTags的源代码,以确定什么是可接受的。虽然不是很理想,但很直接。

<itext orientation="portrait" pagesize="LETTER" top="36" bottom="36" left="36" right="36" title="My Example" subject="My Subject" author="Me">
<paragraph size="8" >This is an example</paragraph>
</itext>

...

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.xml.SAXiTextHandler;

...

String inXml = ""; //use xml above as an example
ByteArrayOutputStream temp = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter writer = null;
try
{
    writer = PdfWriter.getInstance(document, temp);
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    parser.parse(new ByteArrayInputStream(inXml), new SAXiTextHandler(document));
}
catch (Exception e)
{
    // instead, catch the proper exception and do something meaningful
    e.printStackTrace();
}
finally
{
    if (writer != null)
    {
        try
        {
            writer.close();
        }
        catch (Exception ignore)
        {
            // ignore
        }
    } // if
}

//temp holds the PDF

#3


2  

Take a look at JasperReports, it uses iText to export files i think, and its IDE is simple and can be used by non-programmers.

看看JasperReports,我认为它使用iText来导出文件,它的IDE很简单,可以被非程序员使用。

Edit: i forgot to mention, you can use JasperReports engine directly in your application, or you can use iReport "Designer for JasperReports"

编辑:我忘了说,你可以直接在你的应用程序中使用JasperReports引擎,或者你可以使用iReport“JasperReports的设计师”。

#4


1  

You will want to use a well supported XML format for this, as it will allow you to leverage the work of others.

您将希望为此使用受良好支持的XML格式,因为它允许您利用其他人的工作。

A well supported XML format is DocBook XML - http://www.docbook.org/ - and this - http://sagehill.net/docbookxsl/index.html - appears to be a good resource on doing the XML -> PDF using XSLT with the Docbook style sheets and other formats.

一个受到良好支持的XML格式是DocBook XML - http://www.docbook.org/ -而这个- http://sagehill.net/docbookxsl/index.html -似乎是一个很好的资源,用于使用XSLT处理DocBook样式表和其他格式的XML -> PDF。

This approach allows you to use any XSLT processor and any XSL/FO processor to get to your result. This gives you easy scriptability as well as the freedom to switch implementations if needed - notably older Apache FOP implementations degraded badly when the resulting PDF got "too large".

这种方法允许您使用任何XSLT处理器和任何XSL/FO处理器来获取结果。这使您可以轻松地编写脚本,以及在需要时切换实现的*——特别是在生成的PDF变得“太大”时,老的Apache FOP实现严重退化。

#5


0  

Prince is one of the best tools out there. It uses CSS for styles, so if you appreciate that method of separating data from display (read: your users are able to do that too), it may be a very good fit for you. (The control over display that browsers offer through CSS is, by comparision, primitive.)

王子是最好的工具之一。它使用CSS样式,所以如果您喜欢将数据与显示分离的方法(阅读:您的用户也可以这样做),它可能非常适合您。(通过比较,浏览器通过CSS提供的显示控制是原始的。)

#1


10  

Have a look at Apache FOP. Use an XSLT stylesheet to convert the XML (or XHTML) into XSL-FO. Then use FOP to read the XSL-FO document and format it to a PDF document (see Hello World with FOP).

看看Apache FOP。使用XSLT样式表将XML(或XHTML)转换为XSL-FO。然后使用FOP读取XSL-FO文档并将其格式化为PDF文档(参见使用FOP的Hello World)。

#2


4  

iText has a facility for generating PDFs from XML (and HTML, I think). Here is the DTD, but I found it difficult to sort out. Aside from that, I never found any good documentation on what is supported. My approach was to look at the source for SAXiTextHandler and ElementTags to figure out what was acceptable. Although not ideal, it is pretty straight-forward.

iText具有从XML(和HTML)生成pdf的功能。这里是DTD,但是我发现很难分类。除此之外,我从未找到任何关于所支持内容的好的文档。我的方法是查看SAXiTextHandler和ElementTags的源代码,以确定什么是可接受的。虽然不是很理想,但很直接。

<itext orientation="portrait" pagesize="LETTER" top="36" bottom="36" left="36" right="36" title="My Example" subject="My Subject" author="Me">
<paragraph size="8" >This is an example</paragraph>
</itext>

...

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.xml.SAXiTextHandler;

...

String inXml = ""; //use xml above as an example
ByteArrayOutputStream temp = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter writer = null;
try
{
    writer = PdfWriter.getInstance(document, temp);
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    parser.parse(new ByteArrayInputStream(inXml), new SAXiTextHandler(document));
}
catch (Exception e)
{
    // instead, catch the proper exception and do something meaningful
    e.printStackTrace();
}
finally
{
    if (writer != null)
    {
        try
        {
            writer.close();
        }
        catch (Exception ignore)
        {
            // ignore
        }
    } // if
}

//temp holds the PDF

#3


2  

Take a look at JasperReports, it uses iText to export files i think, and its IDE is simple and can be used by non-programmers.

看看JasperReports,我认为它使用iText来导出文件,它的IDE很简单,可以被非程序员使用。

Edit: i forgot to mention, you can use JasperReports engine directly in your application, or you can use iReport "Designer for JasperReports"

编辑:我忘了说,你可以直接在你的应用程序中使用JasperReports引擎,或者你可以使用iReport“JasperReports的设计师”。

#4


1  

You will want to use a well supported XML format for this, as it will allow you to leverage the work of others.

您将希望为此使用受良好支持的XML格式,因为它允许您利用其他人的工作。

A well supported XML format is DocBook XML - http://www.docbook.org/ - and this - http://sagehill.net/docbookxsl/index.html - appears to be a good resource on doing the XML -> PDF using XSLT with the Docbook style sheets and other formats.

一个受到良好支持的XML格式是DocBook XML - http://www.docbook.org/ -而这个- http://sagehill.net/docbookxsl/index.html -似乎是一个很好的资源,用于使用XSLT处理DocBook样式表和其他格式的XML -> PDF。

This approach allows you to use any XSLT processor and any XSL/FO processor to get to your result. This gives you easy scriptability as well as the freedom to switch implementations if needed - notably older Apache FOP implementations degraded badly when the resulting PDF got "too large".

这种方法允许您使用任何XSLT处理器和任何XSL/FO处理器来获取结果。这使您可以轻松地编写脚本,以及在需要时切换实现的*——特别是在生成的PDF变得“太大”时,老的Apache FOP实现严重退化。

#5


0  

Prince is one of the best tools out there. It uses CSS for styles, so if you appreciate that method of separating data from display (read: your users are able to do that too), it may be a very good fit for you. (The control over display that browsers offer through CSS is, by comparision, primitive.)

王子是最好的工具之一。它使用CSS样式,所以如果您喜欢将数据与显示分离的方法(阅读:您的用户也可以这样做),它可能非常适合您。(通过比较,浏览器通过CSS提供的显示控制是原始的。)