1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP direct printing using printer_write function

Discussion in 'PHP' started by karthik kithrak, Feb 24, 2018.

  1. #1
    Hello,
    I am working on a project which is generates invoice using php & html. i want to print that invoice directly to default printer using php. Seems printer_write function prints only source code & not the output view. So i created invoice into PDF using FPDF. Still i cant able to print PDF file using printer_write function. Any suggestions?

    My sample code:
    $pHandle = fopen("files/sample.pdf", "r");
    $handle = printer_open("HP LaserJet 1020");
    printer_set_option($handle, PRINTER_MODE, "RAW");
    printer_write($handle,$pHandle);
    printer_close($handle);
    SEMrush
     
    karthik kithrak, Feb 24, 2018 IP
    SEMrush
  2. JEET

    JEET Well-Known Member

    Messages:
    2,272
    Likes Received:
    118
    Best Answers:
    2
    Trophy Points:
    185
    #2
    Why using PHP for this. Use this in HTML: <body onload="window.print();">
     
    JEET, Mar 30, 2018 IP
  3. JEET

    JEET Well-Known Member

    Messages:
    2,272
    Likes Received:
    118
    Best Answers:
    2
    Trophy Points:
    185
    #3
    Second option is this.
    at the very end of your HTML page, put this code:

    <script type="text/javascript">
    window.print();
    </script>

    This is recommended by some,
    argument being that the printing starts only after the page is completely loaded...

    However, the onload attribute inside <body> gets executed only when the page is completely loaded...
    So pretty much same thing.
    https://www.w3schools.com/tags/ev_onload.asp
     
    JEET, Mar 30, 2018 IP