1 # Copyright (C) 2005 Fletcher T. Penney <fletcher@freeshell.org>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the
15 # Free Software Foundation, Inc.
16 # 59 Temple Place, Suite 330
17 # Boston, MA 02111-1307 USA
20 $ModulesDescription .= '<p><a href="http://git.savannah.gnu.org/cgit/oddmuse.git/tree/modules/pdf.pl">pdf.pl</a>, see <a href="http://www.oddmuse.org/cgi-bin/oddmuse/PDF_Module">PDF Module</a></p>';
22 *PdfOldDoBrowseRequest
= *DoBrowseRequest
;
23 *DoBrowseRequest
= *PdfDoBrowseRequest
;
25 use vars
qw($pdfDirectory $pdfProcessCommand $tempBaseDirectory);
27 # These variables must be configured properly!
28 $pdfProcessCommand = "/path/to/your/pdflatexscript"
29 unless defined $pdfProcessCommand;
30 # Note - this script will vary from machine to machine
31 # The key is to set up pdflatex to use the appropriate texmf folder
32 # Also, I recommend running three times, if you use indexing, etc
33 # You can also try to get latexmk to work (I could not)
39 # # Use MultiMarkdown to automagically convert a text file to html and pdf
43 # export TEXINPUTS=/arpa/af/f/fletcher/texmf//:
45 # for filename in "$@"
48 # # Use XSLT to process XHTML to LaTeX
49 # /usr/pkg/bin/xsltproc /www/af/f/fletcher/wiki/wikidb/modules/Markdown/xhtml2article.xslt "$filename" > "${filename%.*}.tex"
51 # /usr/pkg/bin/pdflatex "$filename"
52 # /usr/pkg/bin/pdflatex "$filename"
53 # /usr/pkg/bin/pdflatex "$filename"
57 $tempBaseDirectory = "$ModuleDir/Markdown/temp"
58 unless defined $tempBaseDirectory;
59 $pdfDirectory = "/path/to/your/pdf/directory"
60 unless defined $pdfDirectory;
63 # Do not need to change these
67 sub PdfDoBrowseRequest{
69 $id = FreeToNormal($id);
71 if (GetParam('pdf','')) {
73 # Strip `.pdf` if present
74 # This does cause problems if you have a page name
75 # that ends in `.pdf`...
82 local $OpenPageName = '';
86 # Create a working directory
87 CreateTempDirectory($id);
95 if (-f "$pdfDirectory/$id.pdf") {
96 # Remove working directory/lockfile
97 system ("/bin/rm -rf \"$tempDirectory\"");
99 # pdf in place, redirect browser to download
100 my %headers = (-uri=>"$ScriptName/pdf/$id.pdf");
101 print $q->redirect(%headers);
103 # Something happened - pdf not in place
104 # Leave lockfile to prevent the hard-headed from
105 # killing your server, and for debugging
107 ReportError(Ts('There was an error generating the pdf for %s. Please report this to webmaster, but do not try to download again as it will not work.', $id));
111 &PdfOldDoBrowseRequest();
116 # Create an HTML file with just the content of the page body
122 open(STDOUT, '>', \$result);
124 open(STDERR, '>/dev/null');
129 open(FILE,"> $tempDirectory/temp.html") or ReportError(Ts('Cannot write %s', "temp.html"));
131 print FILE qq{<?xml version="1.0" encoding="UTF-8" ?>
135 # Create meta-data (you can customize this)
136 print FILE qq{<title>$OpenPageName</title>};
137 print FILE qq{<meta name="author" content="$SiteName"/>};
138 print FILE qq{<meta name="copyright" content="2005. This work is licensed under a Creative Commons License: http://creativecommons.org/licenses/by-sa/2.5/"/>};
139 print FILE qq{<meta name="XMP" content="CCAttributionShareAlike"/>};
140 print FILE "</head><body>";
142 # Output the body and close the file
144 print FILE "\n</body>\n</html>\n";
150 # Run a series of steps to convert XHTML to pdf
151 # You may have to change this based on your system
154 open(STDOUT, '>/dev/null');
156 open(STDERR, '>/dev/null');
158 # Run latex script and copy pdf to final location
159 system("cd \"$tempDirectory\"; \"$pdfProcessCommand\" temp.html > /dev/null; /bin/cp temp.pdf \"$pdfDirectory/$id.pdf\" ");
164 # If we save a new version of a file, we want to delete the old pdf
165 # To save wasted time, don't recreate it until called for
167 *PdfOldDoPost = *DoPost;
168 *DoPost = *PdfNewDoPost;
171 my $id = FreeToNormal(shift);
173 unlink("$pdfDirectory/$id.pdf");
179 sub CreateTempDirectory {
182 $tempDirectory = "$tempBaseDirectory/$id";
184 # Create the general directory if it doesn't exist
185 CreateDir($tempBaseDirectory);
187 # Now, create a temp directory for this page
188 # If it exists, then someone else is generating pdf - give error message
190 if (-d $tempDirectory) {
191 # Someone else is creating this pdf
193 ReportError(Ts('Someone else is generating a pdf for %s. Please wait a minute and then try again.', $id));
197 CreateDir($tempDirectory);
202 # Fix Wiki Links - they have to be fully qualified
204 *PdfOldCreateWikiLink = *CreateWikiLink;
205 *CreateWikiLink = PdfNewCreateWikiLink;
207 sub PdfNewCreateWikiLink {
210 my $rawlink = PdfOldCreateWikiLink($title);
212 if ($rawlink =~ /http\:\/\//) {
215 $rawlink =~ s/\((.*)\)/($ScriptName\/$1)/;
221 *PdfOldGetFooterLinks = *GetFooterLinks;
222 *GetFooterLinks = *PdfNewGetFooterLinks;
224 sub PdfNewGetFooterLinks {
226 my $result = PdfOldGetFooterLinks($id,$rev);
229 push(@NoLinkToPdf,"");
230 foreach my $page (@NoLinkToPdf) {
231 if ($id =~ /^$page$/) {
236 return $result . "<br/>" . ScriptLink("pdf/$id.pdf",T('Download this page as PDF'));