0

I'm trying to display a PDF through Primefaces. The PDF belongs to some item that the user picks from a list, and is supposed to be displayed on the detail page corresponding to that item. The item gets passed from the list bean to the detail bean via flash memory.

I'm providing the PDF as a StreamedContent through a @ViewScoped bean:

@Named
@ViewScoped
public class ItemDetailPageBean implements Serializable {

    private StreamedContent pdf;

    @PostConstruct
    public void init() {

        detailItem = (Item) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("detailItem");

        if (detailAntrag != null) {
            pdf = loadPdf(detailItem.getPdf());
        }
    }

    public StreamedContent loadPdf(byte[] byteArray) {
        if (byteArray != null) {
            ByteArrayInputStream stream = new ByteArrayInputStream(byteArray);
            stream.mark(0);
            return new DefaultStreamedContent(stream, "application/pdf");
        } else {
        return null;
        }
    }

    public StreamedContent getPdf() {
        if (FacesContext.getCurrentInstance().getRenderResponse()) {
            return new DefaultStreamedContent();
        } else {
            if (pdf != null)
            {
                try {
                    pdf.getStream().reset();
                } catch (IOException e) {
                    logger.debug("Error while resetting PDF stream: ", e);
                }
            }
            return pdf;        
        }
     }
    
    public void setPdf(StreamedContent pdf) {
        this.pdf = pdf;
    }
}

Marking and resetting the stream happens since I've read that it's a problem if the stream gets read from multiple times.

However, when I'm trying to display the PDF through itemDetailPageBean.pdf I'm getting an error:

Error in streaming dynamic resource.: 
org.jboss.weld.contexts.ContextNotActiveException: 
WELD-001303: No active contexts for scope type javax.faces.view.ViewScoped

I tried to provide the PDF through a @ApplicationScoped bean, but the problem stands that this bean needs to know which PDF to use. And when I'm trying to pass the PDF, or even the ID of the corresponding item, from my ViewScoped bean, I'm getting that same error.

I've read about how there are different requests for the page itself and for the PDF, and how there are two different ViewScoped beans - but my understanding ends there. Would be very grateful if you could clear things up a bit! I'm still a bit confused by faces request lifecycles tbh.

Thanks in advance!

Noel93
  • 91
  • 7
  • Which version of PrimeFaces are you using? There were changes in usage in PF8.0. – Brooksie May 26 '21 at 19:29
  • Does this answer help? [Primefaces p:media PDF not loading](https://stackoverflow.com/questions/66708421/primefaces-pmedia-pdf-not-loading/66805998#66805998) – Brooksie May 26 '21 at 19:36
  • Does this answer your question? [Display dynamic image from database or remote source with p:graphicImage and StreamedContent](https://stackoverflow.com/questions/8207325/display-dynamic-image-from-database-or-remote-source-with-pgraphicimage-and-str) – Jasper de Vries May 27 '21 at 07:11

0 Answers0