Gossamer Forum
Home : Products : DBMan : Customization :

Updated MOD: Export to PDF

Quote Reply
Updated MOD: Export to PDF
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Export search results to PDF 2.0
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

The previous version of Expport to PDF utilized a tool called PDFever, and only permitted ASCII text input. For archival or quick printed reference, this was sufficient.

Now, utilizing a tool called HTMLDoc, we can now convert HTML files to PDF directly, on a score of platforms (Win95, HP-UX, RedHat Linux, etc).

--Note, this tool can be executed command-line -- which is how we'll be using it -- and in graphical mode. It's an awesome freeware program that does a great job of converting webpage or books into PDF files! You can get it from http://www.easysw.com/htmldoc/ .

If you've installed Export to PDF 1.0, completely remove the sub export_to_pdf (html.pl) routine. We will be replacing it. All the calls in the code will remain the same.

First, add the following code to the bottom of your html.pl file, before the 1;:

Code:
sub export_to_pdf {
my (@hits) = @_;
my ($numhits) = ($#hits+1) / ($#db_cols+1);
# Let's create a few names based on the UNIX time
$outfile = time();
$pdffile = $outfile;
$time = $outfile;
$outfile .= ".html";
$pdffile .= ".pdf";
open (FILE, ">$outfile");

&html_print_headers;
print FILE qq| # All but one line PRINTed in this SUB goes to a HTML file, not to the display
<html>
<head>
<title>$html_title: Export to PDF.</title>
</head>
<body>
HTML Header info goes here
|;

for (0 .. $numhits - 1) {
%rec = &array_to_hash($_, @hits);
&top10($rec{$db_key}); # If you do not have the hits counter installed, remove this line
foreach $field (@fields) {
$rec{$field} =~ s/<LI>/<BR>*/gi; # HTMLDoc doesn't work with <LI> very well
}

print FILE qq|
...Results here...
|;
}

print FILE qq|
Footer HTML goes here
</body>
</html>
|;
close FILE;
unlink(</path/to/public_html/pdf/*.pdf> ); # Let's clear out old PDF files
open (FILE, ">pdfconvert");
print FILE "htmldoc --webpage --landscape --size letter --left 1in --right 1.5in --top .25in --bottom .25in -f /path/to/public_html/pdf/$pdffile $outfile";
# Your PDF's probably have to be written somewhere in your HTML directory so they can be viewed
close FILE;
chmod (0755, "pdfconvert"); # Make the file executable
system './pdfconvert'; # Execute the script
unlink (pdfconvert, $outfile); # Delete the script and the HTML file (cleanup)
print qq|<A HREF="/pdf/$pdffile"><LI> Results in PDF</A>|; # This is the only line printed to the display when this SUB is called
}

Next, in your sub html_footer (html.pl) (or wherever you want the Export link to show up), after:

Code:
print qq!<LI> <A HREF="$db_script_link_url&delete_search=1">Delete</A><BR> ! if ($per_del);
print qq!<LI> <A HREF="$db_script_link_url&modify_search=1">Modify</A><BR> ! if ($per_mod);

add the following line:

Code:
&export_to_pdf;

If you want to restrict who can use this function, indicate the permissions required:

Code:
if($per_add) { &export_to_pdf };

NOTE: The previous version of this MOD executed a second PERL script which returned the PDF code directly to the output, allowing it to be saved or viewed right away. The PDF file was never actually written to file. In this version, a third-party Binary file is executed to convert a file to a file; the PDF file is actually written to the server's hard disk each time a search is performed. The user must click the PDF link to view/save the file. This can be troublesome in a few ways: first, if someone else performs a search before you click the link, you'll get nothing because your file was deleted. (This can be fixed with some javascript codes. I'll add it another time.) Another is that if you have not restricted who can access the PDF functions, you're going to be compiling a LOT of PDF files.

This MOD is being presented in the fashion for which I am using it in my own script. There are several different ways to modify this code: keep files for xx minutes then delete, keep all files and clean them up with a crontab, create a FORM button with an onClick call to a Javascript (after you click, the file is deleted). This code should be in no way considered a "finished" MOD, as it will have to be taylored a lot to meet your needs. But it should get you going on the right track. Smile

--Lee