To make this work the CGI script or program must send the appropriate HTTP attributes, the required empty line to signify the beginning of the document, and then execute the HTMLDOC program to generate the HTML, PostScript, or PDF file as needed.
#!/bin/sh docroot=/insert/directory/here cd $docroot echo "Content-Type: application/pdf" echo "" htmldoc -t pdf --webpage $1Users of this CGI would reference the URL "http://www.domain.com/topdf.cgi?index.html" to generate a PDF file of the site's home page.
You can update the script to use the PATH_TRANSLATED
environment variable so that URLs like
"http://www.domain.com/cgi-bin/topdf/index.html" work as well:
#!/bin/sh docroot=`dirname $PATH_TRANSLATED` cd $docroot echo "Content-Type: application/pdf" echo "" htmldoc -t pdf --webpage $PATH_TRANSLATED
Here is a simple Perl subroutine that can be used to write a PDF report to the HTTP client:
sub topdf(filename); sub topdf { print "Content-Type: application/pdf\n\n" system htmldoc -t pdf --webpage $filename }
Here are some simple C functions that can be used to generate a PDF report to the HTTP client from a temporary file or pipe:
#include <stdio.h> #include <stdlib.h> /* topdf() - convert a HTML file to PDF */ FILE *topdf(const char *filename) /* HTML file to convert */ { char command[1024]; /* Command to execute */ puts("Content-Type: application/pdf\n"); sprintf(command, "htmldoc -t pdf --webpage %s", filename); return (popen(command, "w")); } /* topdf2() - pipe HTML output to HTMLDOC for conversion to PDF */ FILE *topdf2(void) { puts("Content-Type: application/pdf\n"); return (popen("htmldoc -t pdf --webpage -", "w")); }