ESP Logo

PDF-o-matic Source

Home · HTMLDOCMyESP: Login

Easy Software Products is closed September 6, 2010 for Labor Day. We wish you a safe and pleasant holiday! Online orders continue to be processed.

PDF-o-matic is a simple PHP script (view page) that uses HTMLDOC to convert the web page of your choice.

"pdf-o-matic.php":

<?
//
// "$Id: pdf-o-matic.php 554 2009-01-08 17:00:17Z mike $"
//
// Convert a web page to PDF, or show a form to get the URL...
//
// Copyright 2001-2005 by Easy Software Products.  This code may be
// freely used, and distributed under the terms of the HTMLDOC
// software license.
//

// HTML functions for HTMLDOC pages...
//
// This include file contains the following functions:
//
//   htmldoc_header($title = "")
//   html_footer()
//
// Both just put out a standard web page header (<html><body>)
// and trailer (</body></html>) - provide your own implementation
// if you use this script, e.g.:
//
//   function
//   htmldoc_header($title = "")        // I - Title string
//   {
//     print("<html><head><title>$title - Fooware</title></head><body>\n");
//   }
//
//   function
//   html_footer()
//   {
//     print("</body></html>\n");
//   }

include_once "../phplib/htmldoc.php";

// Globals...
global $_GET;                           // Form variables
global $_SERVER;                        // Server variables
global $PHP_SELF;                       // Local URL for this page


//
// 'show_form()' - Show a form for converting a URL to PDF.
//

function
show_form()
{
  global 
$PHP_SELF;

  print(
"<p class='box'>This page is provided as a demonstration of "
       
."HTMLDOC and may not be used for commercial purposes.  Violators "
       
."will be permanently banned from accessing this site!</p>\n");

  print(
"<p>PDF-o-matic is a simple PHP script "
       
."(<a href='pdf-o-matic.php?source'>view source</a>) "
       
."that uses HTMLDOC to convert the web page of your choice. "
       
."Enter a URL to be converted to PDF and click on the "
       
."<var>Create PDF</var> button:</p>\n"
       
."<form method='GET' action='$PHP_SELF'>\n"
       
."<center><input type='text' name='URL' size='40' value='http://'>"
       
."<input type='submit' value='Create PDF'>\n");

  
// The following hidden variable fixes a HUGE bug in Microsoft
  // Internet Explorer (all versions) where MSIE ignores the
  // Content-Type field and instead uses the "extension" on the
  // end of the URL...

  
print("<input type='hidden' name='FORMAT' value='.pdf'>"
       
."</center></form>\n");

  print(
"<p>Please make sure that your HTML code is error free and that all "
       
."the elements are supported with HTMLDOC. "
      
."<a href='htmldoc.html#HTMLREF'>Chapter 6</a> of the HTMLDOC "
      
."Users Manual lists the elements that are supported.</p>\n");

  print(
"<center><table border='1' bgcolor='#cccccc'>\n"
       
."<tr>"
       
."<th align='right'>Server:</th>"
       
."<td>3.0GHz Pentium D with 2GB RAM running Linux</td>"
       
."</tr>\n"
       
."<tr>"
       
."<th align='right'>Status:</th><td>");
  
system("ps -A | awk '{print $4}' | grep htmldoc | wc -l");
  print(
"copies of HTMLDOC running<br>System load average = ");
  
system("uptime | awk '{printf(\"%.2f\", ($10 + $11 + $12) / 3)}'");
  print(
"</td></tr>\n"
       
."</table></center>\n");
}


//
// 'bad_url()' - See if the URL contains bad characters...
//

function                                // O - 1 if the URL is bad, 0 otherwise
bad_url($url)                           // I - URL to check
{
  
// See if the URL starts with http: or https:...
  
if (strncmp($url"http://"7) != &&
      
strncmp($url"https://"8) != 0)
  {
    return (
1);
  }

  
// Check for bad characters in the URL...
  
$len strlen($url);
  for (
$i 0$i $len$i ++) {
    if (!
strchr("~_*()/:%?+-&@;=,$.0123456789"
               
."abcdefghijklmnopqrstuvwxyz"
               
."ABCDEFGHIJKLMNOPQRSTUVWXYZ"$url[$i]))
    {
      return (
1);
    }
  }

  return (
0);
}

            
//
// 'topdf()' - Convert the named file/URL to PDF.
//

function
topdf($filename,                        // I - File/URL to convert
      
$options "")                    // I - Additional command-line options
{
  
# Write the content type to the client...
  
header("Content-Type: application/pdf");
  
header("Content-Disposition: inline; filename=\"pdf-o-matic.pdf\"");
  
flush();

  
# Run HTMLDOC to provide the PDF file to the user...
  # Use the --no-localfiles option for enhanced security!
  # Use ulimit to limit the maximum amount of memory used by each instance!
  
passthru("ulimit -m 16384 -t 5; htmldoc --no-localfiles --no-compression -t pdf14 --quiet "
          
."--jpeg --webpage $options '$filename'");
}


//
// MAIN ENTRY - see if we have form data...
//

if ($argc && $argv[0] == "source")
{
  
// Show source...
  
htmldoc_header("PDF-o-matic Source");

?>

<p>PDF-o-matic is a simple PHP script (<a
href='pdf-o-matic.php'>view page</a>) that uses HTMLDOC to
convert the web page of your choice.</p>

<?php

  
print("<pre>\"pdf-o-matic.php\":\n");

  
highlight_file("pdf-o-matic.php");

  print(
"</pre>\n");

  
html_footer();
}
else if (
array_key_exists("URL"$_GET))
{
  
// Yes, see if the URL is a valid URL and not a filename (security...)
  
$url     $_GET["URL"];
  
$options "";

  if (
array_key_exists("HTTPS"$_SERVER) && $_SERVER["HTTPS"] == "on")
    
$self "https://$_SERVER[HTTP_HOST]$PHP_SELF";
  else
    
$self "http://$_SERVER[HTTP_HOST]$PHP_SELF";

  if (
bad_url($url))
  {
    
// Write the standard header...
    
htmldoc_header("PDF-o-matic");

    
// Show a form for the URL...
    
show_form();

    
// Show an error message...
    
print("<p><b>Error!</b></p>\n"
         
."<p>\"$url\" is not a valid URL. Unable to process!</p>\n");

    
// Show the standard footer...
    
html_footer();
  }
  else if (
array_key_exists("HTTP_REFERER"$_SERVER) &&
           
$self != $_SERVER["HTTP_REFERER"])
  {
    
// Write the standard header...
    
htmldoc_header("PDF-o-matic");

    
// Show an error message - no remote linking!
    
print("<h1>PDF-o-matic</h1>\n"
         
."<p><b>Error!</b></p>\n"
         
."<p>PDF-o-matic was linked from a remote site "
         
."($_SERVER[HTTP_REFERRER]), which is not allowed.</p>\n");

    
// Show the standard footer...
    
html_footer();
  }
  else
    
topdf($url"--header t.D --footer ./. --size letter --left 0.5in");
}
else
{
  
// Write the standard header...
  
htmldoc_header("PDF-o-matic");

  
// Show a form for the URL...
  
show_form();

  
// Show the standard footer...
  
html_footer();
}

?>
About ESP · Contact Us · Legal Notices · Privacy Policy · Terms and ConditionsCopyright 1993-2010 Easy Software Products. All Rights Reserved.