The blog for hints & tips about Oracle Application Development Framework and Mobile Application Framework

  • ADFv
    September 16, 2011

How-to launch browser print dialog when showing printable page

Frank Nimphius
Master Principal Product Manager

The following code, when referenced from the beforePhase property of the f:view component, automatically opens the browser print dialog if a page is rendered as printable. I did write about this topic in the OTN Harvest summary of January 2011. To this time however I used an internal flag, which doesn't feel right. The code used in this post only uses public APIs and thus is a solution that lasts.

public void beforePhaseMethod(PhaseEvent phaseEvent) {
  if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE){
   FacesContext fctx = FacesContext.getCurrentInstance();
    AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
   if(adfFacesContext.getOutputMode()== OutputMode.PRINTABLE){
    ExtendedRenderKitService erks = null;
         erks = Service.getRenderKitService(fctx, ExtendedRenderKitService.class);
erks.addScript(fctx, "window.print();");

The managed bean method is referenced from the f:view component as follows

<f:view beforePhase="#{SampleBean.beforePhaseMethod}">

With this listener and code, when the af:showPrintableBehavior tag is used on a command item to show a printable page, the browser print dialog is automatically opened. While the same code also works for page fragments, the f:view tag is only available for JSPX documents. In this case you either set the beforePhase method property on the JSPX document hosting the page fragment, or define a global phase listener (faces-config.xml) that then works for all pages in an application.

Join the discussion

Comments ( 2 )
  • Preston Friday, December 8, 2017
    I am working on an Oracle ADF application which has a print function using af:showPrintablePageBehavior.
    This creates a new tab in Chrome and IE with printable values from the database. In Firefox this button does nothing. Is there a workaround for this?
  • Frank Nimphius Monday, December 11, 2017
    Actually its best to post this issue to the ADF forum on OTN. I am no longer in the ADF organisation and can't test what you are seeing.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.