Hi my code is as follows:
public StreamedContent getTempPdfFile() throws IOException {
File testPdfFile = new File("D:\\AFC150_20180819_0103.pdf");
streamedContent = new DefaultStreamedContent(new FileInputStream(testPdfFile), "application/pdf",
"AFC150_20180819_0103.pdf");
return streamedContent;
}
<h:panelGrid columns="1" cellpadding="5">
<p:media value="#{realReport.tempPdfFile}" player="pdf" width="1000px" height="300px">
<f:param name="id" />
</p:media>
</h:panelGrid>
But the PDF file is not getting displayed on the page.
Here is my sample, hope it helps, the code has the inline and attachment:
void sendBackPDFToClient()
{
//File temp = File.createTempFile(fileName, ".pdf");
File testPdfFile = new File("D:\AFC150_20180819_0103.pdf");
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType("application/pdf");
ec.setResponseContentLength((int)testPdfFile.length());
//Inline
//ec.setResponseHeader("Content-Disposition", "inline; filename=\"" + testPdfFile.getName() + "\"");
//Attach for Browser
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + testPdfFile.getName() + "\"");
OutputStream output = ec.getResponseOutputStream();
Files.copy(testPdfFile.toPath(), output);
fc.responseComplete();
}
Have you tried the Document Viewer component of PrimeFaces Extensions?
I think its much easier to use than p:media and offers more features including ability to load it from a URL, a Stream, or a Resource. That basic example shows you how to do it with all 3 types.
JAVA:
public StreamedContent getTempPdfFile() throws IOException {
File testPdfFile = Paths.get("D:\\AFC150_20180819_0103.pdf").toFile();
return new DefaultStreamedContent(new FileInputStream(testPdfFile), "application/pdf",
"AFC150_20180819_0103");
}
XHTML:
<pe:documentViewer height="500" width="1000" value="#{realReport.tempPdfFile}"/>
Related
and I cant' store file more than 10Mb
the primary problrm is why doesn't except pdf file???
this is my ManagedBean
file upload function :-
public void fileUploadListener(FileUploadEvent e) throws IOException, Exception {
this.file = e.getFile();
File oStream = new File("c:\\test.pdf");
courseResourceBean.update(item);
oStream.createNewFile();
Long sss = file.getSize();
InputStream inputStream = file.getInputstream();
FileOutputStream outputStream = new FileOutputStream(oStream);
byte[] buffer = new byte[sss.byteValue()];
int lenght;
while ((lenght = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, lenght);
}
outputStream.close();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("File Uploaded Successfully" + file.getSize()));
String extension = file.getContentType();
}
file download function :-
public void prepDownload(Long id) throws Exception {
File files = new File("c:\\test.pdf");
InputStream input = new FileInputStream(files);
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(files.getName()), files.getName()));
System.out.println("PREP = " + download.getName());
}
and this is the xhtml page
upload :-
<p:outputLabel value="#{msg.courseName}, #{msg.inArabic}" style="text-align: right"/>
<p:fileUpload value="#{courseResourceMB.file}" mode="advanced" sizeLimit="200000000000"
fileUploadListener="#{courseResourceMB.fileUploadListener}" update="msg11" >
</p:fileUpload>
downlad:-
<p:commandButton id="downloadLink" value="Download" actionListener="#{courseResourceMB.prepDownload(item.id)}" ajax="false">
<p:fileDownload value="#{courseResourceMB.download}" />
</p:commandButton>
Sorry for my poor english but I really want to show two pdf reports from jasper report at the same time in differents tabs on browser. I´m working with java jsf, primefaces. The principal idea is when the button is clicked show this reports in diferents tabs. I try to do this:
I have this in the Managed Bean:
public void showReports() {
RequestContext.getCurrentInstance().execute("document.getElementById('fromGeneral:rep2').click();");
RequestContext.getCurrentInstance().execute("document.getElementById('fromGeneral:rep3').click();");
}
public void printReport(String name) {
try {
Map<String, Object> mapParametros = new HashMap<>();
mapParametros.put("CORR", corr);
printJasper(mapParametros, new File("/Jasper/Reports/" + name));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void printJasper(Map<String, Object> reportValues, File fileJ) {
ByteArrayInputStream input = null;
BufferedOutputStream output = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
try {
facesContext = FacesContext.getCurrentInstance();
externalContext = facesContext.getExternalContext();
response = (HttpServletResponse) externalContext.getResponse();
FileInputStream file = new FileInputStream(fileJ);
JasperReport compiledTemplate = (JasperReport) JRLoader.loadObject(file);
ByteArrayOutputStream out = new ByteArrayOutputStream();
JasperPrint jasperPrint = JasperFillManager.fillReport(compiledTemplate, reportValues, dataSourceP.getConnection());
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, "this.print();");
exporter.exportReport();
input = new ByteArrayInputStream(out.toByteArray());
response.reset();
response.setHeader("Content-Type", "application/pdf");
response.setHeader("Content-Length", String.valueOf(out.toByteArray().length));
response.setHeader("Content-Disposition", "inline; filename=\"ticket.pdf\"");
output = new BufferedOutputStream(response.getOutputStream(), Constants.DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[Constants.DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
} catch (Exception exception) {
System.out.println(exception.getMessage());
} finally {
try {
if (output != null) {
output.close();
}
if (input != null) {
input.close();
}
} catch (Exception exception) {
/* ... */
}
}
facesContext.responseComplete();
}
An this in my view:
<h:form>
<p:commandButton value="Show them" action="#{reportBean.showReports()}"/>
<p:commandButton value="REPORT 1" id="rep1" style="font-size: 25px; float:right;visibility: hidden;" action="#{reportBean.printReport("Report1")}" ajax="false" onclick="this.form.target = '_blank';"/>
<p:commandButton value="REPORT 2" id="rep2" style="font-size: 25px; float:right;visibility: hidden;" action="#{reportBean.printReport("Report2")}" ajax="false" onclick="this.form.target = '_blank';"/>
</h:form>
But doesn´t work, it just show the second report.
Help!.
Thanks!
Try a hit using p:commandLink like i am using.
<p:commandLink id="PreviewR1" value="Print Preview" ajax="false" action="#{reportBean.printReport("Report1")}" target="_blank" />
<p:commandLink id="PreviewR2" value="Print Preview" ajax="false" action="#{reportBean.printReport("Report2")}" target="_blank" />
It will open the report 1 & 2 in separate browser tab while your original web page will remains the same.
You just don't click the correct buttons:
public void showReports() {
RequestContext.getCurrentInstance().execute("document.getElementById('fromGeneral:rep2').click();");
RequestContext.getCurrentInstance().execute("document.getElementById('fromGeneral:rep3').click();");
}
You're clicking on rep2 and rep3. rep3 doesn't exists, you need to click on rep1 instead. That should be the reason why only the 2nd report is shown.
Finally I found other way to solve it.
The method showReports() instead to click two buttons, this open two xhtml that each one has inside of <h.form> a remotecommand with autorun true, that show the reports. I don´t know if it´s the best way to do it, but It works.
Thanks for all your comments
In my application, I use jsf & richfaces, I want to display a generated pdf in web browser , I have generated this PDF in serverside and it is available in ServletResponse, but I am unable to display it in my web page.
I have tried this question but, it did not solve my problem.
Is there any libraries or special ways to do this?
What I have tried to do is given below.
<h:commandLink rendered="#{ fileImportOptionsViewBean.isFileExist(status.myPdf) }" action="#
{fileImportOptionsViewBean.downloadFile(satus.myPdf)}" target="_blank">
<h:graphicImage url="../../resources/xyz/icons/pdf.png" /></h:commandLink>
downloadFile method
public void downloadFile(String filePath){
try {
File file=new File(filePath);
if(file.exists()){
FacesContext fc=FacesContext.getCurrentInstance();
ExternalContext ec=fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType("application/pdf");
ec.setResponseHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
ec.setResponseBufferSize(8192);
OutputStream output = ec.getResponseOutputStream();
URL url = file.toURL();
try(
BufferedInputStream bis = new BufferedInputStream(url.openStream());
BufferedOutputStream bos = new BufferedOutputStream(output);
){
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
}
fc.responseComplete();
}
} catch (Exception e) {
}
}
Any help is appreciated,Thanks.
I don't see anything wrong with your current setup.Most probably the problem lies in your XHTML page and something is causing your h:commandLink not to fire the event.Please refer this post for further details,surely this will be of some help to you.
I have a task of displaying a PDF fetched from the database as a pop up in my JSF application upon clicking a link. However my modal panel isn't rendering the PDF. Instead I get a small dark grey box at the top left corner of the panel.
Here's my XHTML code:
<rich:column styleClass="viewUserTable">
<f:facet name="header">
<h:outputText value="Pdf" />
</f:facet>
<h:outputLink value="#" id="link" style="color:blue;margin: 0 auto;">
Proof
<rich:componentControl for="panel" attachTo="link"
operation="show" event="onclick" />
</h:outputLink>
<rich:modalPanel id="panel" width="350" height="100">
<f:facet name="header">
<h:outputText value="PDF"></h:outputText>
</f:facet>
<a4j:mediaOutput element="object" mimeType="application/pdf"
id="media" session="false" createContent="#{getMyBean.showPdf}"
value="1" style="width:800px; height:600px;" cacheable="false"
standby="loading...">
</a4j:mediaOutput>
</rich:modalPanel>
Here's the method I'm calling in my bean to create content for <a4j:mediaOutput>.
public void showPdf(OutputStream stream, Object object) throws SQLException, IOException {
Blob proof= myObject.getPdf(someValue);//DB call to get the pdf
InputStream inStream = proof.getBinaryStream();
int length = -1;
byte[] buffer = new byte[4096];
while ((length = inStream.read(buffer)) != -1) {
stream.write(buffer, 0, length);
}
}
I am not sure of the significance of the value attribute in the <a4j:mediaOutput> but I found an example on internet where similar content was being fetched from DB and and the value was set as 1. I m using RichFaces 3.3.
A friend of mine came to me with this problem today and like you I've struggled to come up with a solution. After some time studying servlet I could solve the problem.
Your method should look like:
public void showPdf(OutputStream stream, Object object) throws SQLException, IOException {
Blob proof = myObject.getPdf(someValue);//DB call to get the pdf
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
ServletOutputStream sout = null;
response.setContentType("application/pdf");
//set a constant for a name to avoid cache and duplicate files on server
response.setHeader("Content-Disposition","filename=fileName_" + (new Date()).getTime() + ".pdf" );
response.setHeader("Content-Transfer-Encoding", "binary;");
response.setHeader("Pragma", " ");
response.setHeader("Cache-Control", " ");
response.setHeader("Pragma", "no-cache;");
response.setDateHeader("Expires", new java.util.Date().getTime());
//Here you will have to pass to total lengh of bytes
//as I used a file I called length method
response.setContentLength(Long.valueOf( proof.someByteLengthProperty() ).intValue());
sout = response.getOutputStream();
byte[] buf = new byte[4096];
InputStream is = new FileInputStream(proof.getBinaryStream());
int c = 0;
while ((c = is.read(buf, 0, buf.length)) > 0) {
sout.write(buf, 0, c);
}
sout.flush();
is.close();
}
It seems that only writing out the OutputStream is not enough so you have to create a ServletOutputStream and send it throghout the 'FacesContext' so it can render the output as if it was sending a download stream.
I'm trying to generate a simple PDF report using JasperReport on a button click. Here's the relevant code:
report.xhtml:
<p:commandButton value="#{msg['report.generate']}" action="#{generateReportBean.generateReport}" />
GenerateReportBean.java:
public void generateReport() throws JRException, IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext
.getResponse();
InputStream reportStream = facesContext.getExternalContext().getResourceAsStream("/WEB-INF/reports/report.jrxml");
JasperDesign jasperDesign = JRXmlLoader.load(reportStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
byte[] report = JasperRunManager.runReportToPdf(jasperReport, new HashMap(), new JREmptyDataSource());
response.setContentType("application/pdf");
response.setContentLength(report.length);
response.setHeader("Content-disposition", "attachment; filename=report.pdf" );
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.write(report);
servletOutputStream.flush();
servletOutputStream.close();
facesContext.responseComplete();
}
The problem is that when I use FireBug I can see a response (for me it's ok):
X-Powered-By JSF/2.0
Content-Type application/pdf
Content-Length 1310
Content-Disposition attachment; filename=report.pdf
Server Jetty(7.4.0.v20110414)
But I don't get the popup that would allow me to save the pdf. As you can see I'm developing on Jetty 7.4 maven plugin. Am I doing something wrong here?
Add ajax="false" to your commandButton
<p:commandButton ajax="false" value="#{msg['report.generate']}" action="#{generateReportBean.generateReport}" />