Ireport aracılığı ile oluşturmuş olduğumuz pdf dökümanlarını PrimeFaces Document Viewer'da nasıl gösterebiliriz.
Bunun için kullandıklarım
Netbeans 8.0.2
PrimeFaces 6.2
PrimeFaces Extensions 6.2.8
itext 2.1.7
JasperReports 5.6.0
Maven:
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.faces</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.2</version>
</dependency>
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>6.2.8</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>5.6.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="TEST_PU" transaction-type="JTA">
<jta-data-source>Sakila</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:pe="http://primefaces.org/ui/extensions"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
</h:head>
<h:body>
<h:form>
<p:tabView>
<p:tab title="StreamedContent">
<pe:documentViewer height="500" value="#{basicDocumentViewerController.content}" download="extensions-rocks.pdf"/>
</p:tab>
</p:tabView>
</h:form>
</h:body>
</html>
BasicDocumentViewerController.java
package com.kurtomerfaruk.documentwiewer;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
/**
*
* @author Omer Faruk Kurt
* @Created on date 22/08/2018 22:26:23
*/
@ManagedBean(name = "basicDocumentViewerController")
@SessionScoped
public class BasicDocumentViewerController implements Serializable {
private static final long serialVersionUID = 1L;
private StreamedContent content;
public StreamedContent getContent() {
if(content == null){
content=pdfDocumentGenerate();
}
return content;
}
public void setContent(StreamedContent content) {
this.content = content;
}
public DefaultStreamedContent pdfDocumentGenerate() {
JasperPrint jasperPrint;
try {
FacesContext facesContext = FacesContext.getCurrentInstance();
String reportPath = facesContext.getExternalContext().getRealPath("/reports") + File.separator;
String fileName = "actor_list.jasper";
fileName = reportPath + fileName;
Context initialContext = new InitialContext();
DataSource datasource = (DataSource) initialContext.lookup("Sakila");
Connection conn = datasource.getConnection();
HashMap parameterMap = new HashMap();
jasperPrint = JasperFillManager.fillReport(fileName, parameterMap, conn);
byte[] document = JasperExportManager.exportReportToPdf(jasperPrint);
return new DefaultStreamedContent(new ByteArrayInputStream(document), "application/pdf", "Actor_List");
} catch (JRException | NamingException ex) {
Logger.getLogger(BasicDocumentViewerController.class.getName()).log(Level.SEVERE, null, ex);
return null;
} catch (SQLException ex) {
System.out.println("No se puede obtener la conexion: " + ex);
return null;
}
}
}
Sonuç
Kaynak Kodları
0 Yorumlar