Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

arsenalgear

macrumors regular
Original poster
Dec 17, 2007
100
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! :)
 
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.
 
Hi,

Thanks for the reply. Is there a way to have the image print without the user seeing it in their browser? :)
 
Hi,

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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.