Report to pdf java

Generate Reports from XML Data in Java

XML is an eXtensive Markup Language that is self-descriptive, W3C Recommended, and designed to store and transport data. After receiving the data in XML format, as a developer, you can convert it into any other better human-readable format like PDF or MS Word document. This article will guide you to convert XML data into PDF and MS Word reports in Java using simple templates.

The following topics are discussed below:

Report Generation Java API — XML to PDF and WORD#

GroupDocs.Assembly provides Java API to automate the report generation from the XML data using DOCX or TXT template. It further supports the JSON, CSV, and other data sources to convert the data into presentable reports of different file formats.

Download or Configure#

You may download the JAR file from the downloads section, or just get the repository and dependency configurations for the pom.xml of your maven-based Java applications.

 GroupDocsJavaAPI GroupDocs Java API http://repository.groupdocs.com/repo/  com.groupdocs groupdocs-assembly 21.4  

Generate PDF Report from XML Data in Java#

Let’s quickly jumps to the steps that will lead you to convert XML data into the formatted PDF report.

Читайте также:  Python точность вывода print

XML to PDF Report in Java

  • Load the XML data source
  • Define the template according to your XML data
  • Provide XML data source and template to a method for report generation.

XML Data#

For the PDF report generation, the following XML sample data is used. It contains the data of managers and their respective clients with details.

  John Smith  A Company  1200000  . . Tony Anderson . . 

Template#

Define the following template in TXT or DOCX format. This allows iteration over Managers and their respective Clients. After that, use the code mentioned below for report generation.

Java Steps to Generate PDF Report from XML#

The following steps and the code allows automating the generation of PDF reports from the XML data as per the defined template.

  • Define XML data file, text template file, and PDF output report files.
  • Instantiate XMLDataSoure with XML data file.
  • Create DataSourceInfo with defined XML data source.
  • Call the assembleDocument method to generate the PDF report.

The following code implements the above steps and generates a PDF from the XML data source in Java.

Generate MS Word Report from XML data in Java#

Likewise, you can create the MS Word DOC/DOCX report from the same XML data in Java. There will be no difference, except to change the output file name.

  • Load the XML data file.
  • Defining the template in TXT or DOCX format.
  • Set the output report document format as DOCX.
  • Provide the XML data file, template, and output file path to DocumentAssembler to convert the XML to DOCX.

The following code converts the XML and generates the DOCX file using the defined template in Java.

Get a Free API License#

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion#

To conclude, you have learned to convert your XML data into PDF format as a report in Java. Additionally, you have seen the report generation in DOC/DOCX format from the same XML using the template. After reading the series, Generate PDF and MS Word Reports from JSON, CSV, XML, you should be comfortable in building your own report builder Java application.

Similarly, you can convert many other data sources to report. For more details, options, and examples, you can visit documentation and the GitHub repository. In case of further queries, contact us via the forum.

See Also#

Источник

Generate Reports from JSON Data in Java

JSON is a formatted and readable data interchange format to transmit data with attributes. However, the large data in JSON format is not very presentable and easily understandable. We mostly need to convert the large JSON data into a presentable format. This article will guide you to convert JSON data into PDF and MS Word reports in Java using a simple template.

Report Generation Java API#

I will be using GroupDocs.Assembly for Java API to generate reports from the provided JSON data and template in DOCX and TXT format. It also supports automatic report generation in multiple formats from CSV, XML data sources.

Download or Configure#

You may download the JAR file from the downloads section, or just get the repository and dependency configurations for the pom.xml of your maven-based Java applications.

 GroupDocsJavaAPI GroupDocs Java API http://repository.groupdocs.com/repo/  com.groupdocs groupdocs-assembly 21.1  

Generate PDF Report from JSON Data in Java#

Let’s quickly jumps to the steps that will lead you to convert JSON data into the formatted PDF report.

JSON to PDF Report in Java

  • Get JSON data source
  • Define template according to JSON data
  • Provide JSON data source and template to simple java code for report generation.

JSON Data#

For the PDF report generation, I will be using the following sample JSON data of managers and their respective clients and details.

\[ < "Name":"John Smith","Contract":\[ ,"Price":1200000>, ,"Price":750000>, ,"Price":350000>\] >, < "Name":"Tony Anderson","Contract":\[ ,"Price":650000>, ,"Price":550000>\] >, < "Name":"July James","Contract":\[ ,"Price":350000>, ,"Price":250000>, ,"Price":100000>, ,"Price":100000>\] > \] 

Template#

Define the following template in TXT or DOCX format. This will allow to iterate over Managers and their respective Clients and their details. After that, you can jump to code for report generation.

Java Steps to Generate PDF Report from JSON#

Following steps and Java code allows automatic conversion of JSON data into PDF report as per defined template.

  • Define JSON data file, .txt template file, and PDF output report file paths.
  • Instantiate JsonDataSoure with JSON data file.
  • Create DataSourceInfo with defined JsonDataSource.
  • Call the assembleDocument method of the DocumentAssembler class to generate the PDF report from the provided JSON data and defined template.

Generate MS Word Report from JSON data in Java#

Similarly, like the above PDF report generation, you can easily create the DOCX report by:

  • Defining the same template in DOCX format.
  • Set the output report document format as DOCX.
  • The rest of the code will remain the same to generate MS Word DOCX report from the JSON data.

For more details, options, and examples, you can go through the documentation and GitHub repository. In case of further queries and ambiguities, contact the free support on the forum.

Conclusion#

I hope you will feel comfortable building your own Java-based application to generate reports by converting JSON data to PDF format. Similarly, you can generate reports in other formats like DOCX using other data sources like CSV and XML.

See Also#

Источник

Report to pdf java

Free tutorials with examples : learn IT & improve your IT skills.

Enterprise / Solution Architect

This tutorial will explain you how to Generate a PDF File from Java using Jasper Reports Library.

Generate a PDF File with JasperReports and Java

Creating a Jasper Report (using grouping or not) is not a difficult task (for this we can use Jaspersoft Studio). More complicated could be to run that report from a Java EE Application. In my presentation, I will use PrimeFaces as JSF. More specific, imagine you have to generate a PDF file from Java EE Application using JasperReports Library.

Here are some notes in order to generate a PDF file using JasperReports and Java EE Application.

When your Report is done (the tests are included) for your report, set your «Default Data Adapter» somewhere on the disk (where the adapter definition will stay on the server). Save the .jrxml file and compile it from «Project» -> «Build All». A new .jasper file will be generated. You cannot read the .jasper report, but you can read the .jrxml file. The .jasper file will be put on the machine where the Java Application Server will be/ is installed. Take a look at the following picture:

Generate PDF files using JasperReports and Java EE Applications (PrimeFaces as JSF): jasper reports properties

The Adapter will be saved into a XML file and will be used when the Jasper Report will run on the Java Application Server (In my case I used WildFly 10.1 with JDK 8).

For doing this you have to right click on the Adapter name, and choose «Export To File». You don’t have to change its name. Take a look at the following picture:

Generate PDF files using JasperReports and Java EE Applications (PrimeFaces as JSF): data adapter definition

For building the Java EE Application which will run the Jasper Report, I used Eclipse Neon.2 Release (4.6.2) with JDK 8.

The libraries I have used for running (generating) a Jasper Report are the following:

Generate PDF files using JasperReports and Java EE Applications (PrimeFaces as JSF): jar

primefaces-5.0.jar is for PrimeFaces (JSF), ojdbc7.jar is for the Oracle Java Database Connectivity. The others are for generating the Jasper Reports. These libraries could be found on https://mvnrepository.com.

These files are very important when working with Jasper Reports Library !

  MyDataAdapter1 oracle.jdbc.driver.OracleDriver dw 1 true jdbc:oracle:thin:@192.168.57.141:1521:db12c  C:\Java\ojdbc7.jar  

Here is the index.xhtml page which contains the button from which you generate the Jasper Report:

    

Run Jasper Reports (JR) from Java EE Application (using JSF)

" />
import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.context.FacesContext; import javax.faces.event.ActionEvent; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.util.JRLoader; @ManagedBean(name= "buttonClass") public class ButtonClass < public void buttonAction(ActionEvent actionEvent) < addMessage("Jasper Reports will be generated . "); execRaport(); addMessage("Jasper Reports are generated !"); >public void addMessage(String summary) < FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null); FacesContext.getCurrentInstance().addMessage(null, message); >public void execRaport() < // Oracle Database connexion String url = "jdbc:oracle:thin:@192.168.57.141:1521:db12c"; String login = "dw"; String password = "1"; Connection connection = null; try < Driver monDriver = new oracle.jdbc.driver.OracleDriver(); DriverManager.registerDriver(monDriver); connection = DriverManager.getConnection(url, login, password); JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile("C:/JAVA/MyJasperReport.jasper"); Map parameters = new HashMap(); parameters.put("PARAMETER1", 3); // Fill the Jasper Report JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection); // Creation of the HTML Jasper Reports JasperExportManager.exportReportToPdfFile(jasperPrint, "C:/JAVA/MyJasperReport.pdf"); >catch (JRException e) < e.printStackTrace(); >catch (SQLException e) < e.printStackTrace(); >finally < try < connection.close(); >catch (SQLException e) < e.printStackTrace(); >> > >

LEARN-IT-WITH-EXAMPLES.com

Источник

Оцените статью