Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

A 'Print This Article' Link

For some websites, it would be nice to let people print the content of a web page on your website. Just the article; no header, navigation, or ads.

Setting it up is feasible with CSS @media print and @media screen rules. But the details can be onerous.

Instead, that work can be avoided with an alternative: Provide a "print this article" link that utilizes a PHP script to copy the article into an otherwise blank page so it is available to print.

This article describes how.

But there is a caveat. The content to be printed needs to be in the web page source code between HTML article tags. If the page has no article tags, then the entire web page will print, which includes the header, navigation, etc.

Additional information about the article tag can be found at The HTML article Element of the willmaster.com website and at The Article Contents Element of the developer.mozilla.org website.

After the link is clicked and the PHP script kicks in, the content is published on a separately generated printable page and the printer dialog is launched (the person has to allow the printing to proceed). When printed, the person closes the printable page.

Installation is relatively simple.

The source code for the "print this article" PHP script is copy and paste — no customization required. Once that is installed on your server, web pages can be "print this article" enabled with, as mentioned, one line of JavaScript and a "print this article" link.

Let's start with the PHP script. As noted above, it is copy and paste.

Below is the source code. Save it as PagePrinter.php or other *.php file name. Upload it to your server and make a note of its URL.

<?php
if( ! isset($_GET['p']) ) { exit; }
$page = file_get_contents($_GET['p']);
$page = preg_replace('!^.*?<article[^>]*>!si','',$page);
$page = preg_replace('!</article>.*?<article[^>]*>!si',"\r\n\r\n",$page);
$page = preg_replace('!</article>.*$!si','',$page);
$page = preg_replace('!<aside[^>]*>.*?</aside>!si','',$page);
echo <<<PAGE
<!doctype html>
<html>
<head>
<title>For Printing</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
html, body { font-size:100%; font-family:sans-serif; }
img { max-width:100%; }
</style>
</head>
<body>
$page
</body>
</html>
PAGE;
?>
<script type="text/javascript">
print();
</script>

The PagePrinter.php script:

  1. Receives the URL of a web page to print.
  2. Grabs the page at the URL.
  3. Removes everything except what is between HTML article tags (unless there are no article tags).
  4. Removes everything between HTML aside tags, if present.
  5. Tells the browser to print.

When PagePrinter.php is ready on your server, then you are ready to set up your web pages for printing.

There are two steps.

Step 1.
Place this JavaScript on your web page. Anywhere on the web page is okay; immediately above the cancel </body> is a common place for JavaScript.

<script type="text/javascript">
function PrintThisPage() { window.open("https://example.com/PagePrinter.php?p="+encodeURIComponent(document.URL),"printme","popup"); }
</script>

Replace https://example.com/PagePrinter.php with the URL to your installation of the PagePrinter.php script.

Step 2.
Put this link into your web page.

<a href="javascript:PrintThisPage()">Print this article</a>

Optionally, the link text may be modified. The link value needs to remain as is.

If you wish to remove the "Print this article" link when the page prints, put the above link between HTML aside tags. Example:

<aside>
<a href="javascript:PrintThisPage()">Print this article</a>
</aside>

You now have a "print this article" link on your web page. Tap it and see how it works. If the browser allows popups, the printable page should be in a popup. Otherwise, the printable page will be in a separate browser tab.

To put a print link on other web pages, follow the 2 steps above for each web page.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC