Printing an image through a link

Discussion in 'Web Design and Development' started by arsenalgear, Sep 17, 2009.

  1. arsenalgear macrumors member

    Joined:
    Dec 17, 2007
    #1
    Hi there,

    I am coding a website for a business, and on their homepage they want to include a link to a printable coupon. Could someone please tell me how I would go about creating a link that, when clicked, automatically retrieves an image and initiates a browser's "Print" command to print it?

    Thank you very much! :)
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Can't do this through a simple link. An idea is to use PHP.
    PHP:
    <?php // print image: filename: printpage.php
    $img '';
    if (!
    array_key_exists('img'$_GET)) {
     echo 
    '<h1>Problem showing image.</h1>';
    }
    else {
      
    $img $_GET['img'];
    }
    if (
    file_exists($img)) {
      echo <<<EOF
      <img src="$img">
      <script type="javascript">
      window.print();
      </script>
    EOF;
    }
    else {
      echo 
    '<h1>Given file not found.</h1>';
    }
    ?>
    It'll accept and image path as a argument and if the file exists it will put it in an image tag then use JavaScript to print the page, which opens the user's print dialog if they have JavaScript enabled. The link to this page would look like,
    HTML:
    <a href="printpage.php?img=image.jpg">Coupon</a>
    You can add some extra stuff onto the PHP to ensure the file is of an image type and maybe check to only allow images from a certain directory or whatever you want to add.
     
  3. arsenalgear thread starter macrumors member

    Joined:
    Dec 17, 2007
    #3
    Hi,

    Thanks for the reply. Is there a way to have the image print without the user seeing it in their browser? :)
     
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    Not that I've ever seen, and I would hate if a web page could prompt my print dialog to print something i haven't even seen yet. That's a potential security issue so I doubt there's a way to do it. Perhaps with Flash, but again not the greatest experience for the user.
     

Share This Page