| 1 |
<? |
| 2 |
// |
| 3 |
// Sample "portal" script to convert the named HTML file to PDF on-the-fly. |
| 4 |
// |
| 5 |
// Usage: http://www.domain.com/path/topdf.php/path/filename.html |
| 6 |
// |
| 7 |
|
| 8 |
// |
| 9 |
// 'topdf()' - Convert the named file/URL to PDF. |
| 10 |
// |
| 11 |
|
| 12 |
function topdf($filename, $options = "") { |
| 13 |
# Write the content type to the client... |
| 14 |
header("Content-Type: application/pdf"); |
| 15 |
flush(); |
| 16 |
|
| 17 |
# Run HTMLDOC to provide the PDF file to the user... |
| 18 |
passthru("htmldoc -t pdf --quiet --jpeg --webpage $options \'$filename\'"); |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
// |
| 23 |
// 'bad_url()' - See if the URL contains bad characters... |
| 24 |
// |
| 25 |
|
| 26 |
function bad_url($url) { |
| 27 |
// See if the URL starts with http: or https:... |
| 28 |
if (strncmp($url, "http://", 7) != 0 && |
| 29 |
strncmp($url, "https://", 8) != 0) { |
| 30 |
return 1; |
| 31 |
} |
| 32 |
|
| 33 |
// Check for bad characters in the URL... |
| 34 |
$len = strlen($url); |
| 35 |
for ($i = 0; $i < $len; $i ++) { |
| 36 |
if (!strchr("~_*()/:%?+-&@;=,$.", $url[$i]) && |
| 37 |
!ctype_alnum($url[$i])) { |
| 38 |
return 1; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
// |
| 46 |
// MAIN ENTRY - Pass the trailing path info in to HTMLDOC... |
| 47 |
// |
| 48 |
|
| 49 |
global $SERVER_NAME; |
| 50 |
global $SERVER_PORT; |
| 51 |
global $PATH_INFO; |
| 52 |
global $QUERY_STRING; |
| 53 |
|
| 54 |
if ($QUERY_STRING != "") { |
| 55 |
$url = "http://${SERVER_NAME}:${SERVER_PORT}${PATH_INFO}?${QUERY_STRING}"; |
| 56 |
} else { |
| 57 |
$url = "http://${SERVER_NAME}:${SERVER_PORT}$PATH_INFO"; |
| 58 |
} |
| 59 |
|
| 60 |
if (bad_url($url)) { |
| 61 |
print("<HTML><HEAD><TITLE>Bad URL</TITLE></HEAD>\n" |
| 62 |
."<BODY><H1>Bad URL</H1>\n", |
| 63 |
."<P>The URL <B><TT>$url</TT></B> is bad.</P>\n" |
| 64 |
."</BODY></HTML>\n"); |
| 65 |
} else { |
| 66 |
topdf($url); |
| 67 |
} |
| 68 |
?> |