Auto-print a page in APEX

My web site accepts applications for a sports team, and the last step is the applicant needs to print out a form to be signed. To make things as simple as possible, I want this form to send itself to their printer as soon as they open it. Now, I’m not a javascript expert; but instead of googling for the code, I stole it by doing a View Source on Google Mail’s print feature.

To get any APEX page to print when it is opened, all you need to do is add two bits to the page definition:

HTML Header

function Print(){document.body.offsetHeight;window.print()}

Page HTML Body Attribute

onload="Print()"

Isn’t javascript easy? I’m not sure what the “document.body.offsetHeight” is all about but I suspect it’s something to do with waiting for the whole page to load and render before the print starts.

“Not possible” is rarely correct
Customising a Navigation List

Comments

  1. Christian Pitet
    8 August 2019 - 10:42 pm

    Hi,

    I tried you solution but I get a javascript error :
    Uncaught ReferenceError: Print is not defined
    at onload (f?p=112:13:31722992776085::::::34)
    onload @ f?p=112:13:31722992776085::::::34

    Regards.

    • Hi Christian,

      I’d say that means you haven’t put the definition of the Print() function in the correct location, or your call to it is occurring before the function has been loaded into the page; another possible cause is that there is a syntax error in your Print() function. Check your console log for clues.

  2. Hi.
    I used this code for print…

    function printdiv(div_print)//setTimeout(function(){
    {
    var headstr = “”;
    var footstr = “”;
    var newstr = document.all.item(div_print).innerHTML;
    var oldstr = document.body.innerHTML;
    document.body.innerHTML = headstr+newstr+footstr;
    window.print();
    location.reload();
    return false;
    }

    And
    onload=”printdiv(div_print)”
    but i got error..like…
    Uncaught TypeError: Cannot read properties of null (reading ‘innerHTML’)
    at printdiv (item-setup-report1?session=3886592598070:155:42)
    at onload (item-setup-report1?session=3886592598070:183:206)
    —-
    would you please solve it..

    • Jeffrey Kemp
      20 May 2023 - 8:09 pm

      Hi Hridoy, sorry – I don’t know where you got that code, but it wasn’t from me.

Leave a Reply

Your email address will not be published / Required fields are marked *