Primefaces - p:fileDownload PDF

Asked

Viewed 620 times

0

Hello,

I’m trying to download a PDF in my application but I’m not getting it.

When I click on the Download instead of downloading is checking in the form if the required fields have been filled in.

Filling in all fields works normally.

Follow the code below:

<h:form id="formTeste" >            
    <p:panel toggleable="true" id="pgFormTeste" header="Teste">
       <h:panelGrid columns="2">                                

           <p:outputLabel for="descricao" value="Descrição:" />
           <p:inputText value="#{testeController.teste.desc}" required="true" />

          <p:outputLabel value= "PDF:" />
          <p:commandButton value="Download" ajax="false">
             <p:fileDownload value="#{testeController.getFile()}" />
          </p:commandButton> 
       </h:panelGrid>
    </p:panel>
 </form>

Controller:

private String pathPDF = System.getProperty("jboss.apps.data.dir") + File.separator + "sigeap" + File.separator + "001.pdf";

public void setFile(StreamedContent file) {    
    this.file = file;    
}    

public StreamedContent getFile() throws FileNotFoundException {    
    FileInputStream stream = new FileInputStream(pathPDF);    
    file = new DefaultStreamedContent(stream, "application/pdf", "001.pdf");   
    return file;    
}   

If I can do that, it’ll solve most of my problem...

Taking advantage, if someone also knows how to open the PDF that is in a folder on the server directly in the browser as does the Pdfobject would help a lot too.

Thank you

  • 2

    Try <p:commandButton value="Download" ajax="false" immediate="true">

  • Thank you very much... It worked perfectly.

1 answer

1


As already said in the comment, use the option immediate=true in the commmandButton.

Answer also already validated in another question.

  • Thank you very much. That’s right. Can you tell me if you can open this same file directly in the browser using the Pdfobject?

  • 1

    The question should be closed as duplicate. This forum follows the same specifications as the original stack overflow ?

  • I think not, because the question was in English. Not everyone speaks both languages.

  • I do not know Pdfobject, only the media. I believe it is possible yes, but via code in Managedbeans.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.