]> git.sesse.net Git - webpdf/blobdiff - createpdf.pl
Upgraded ImageMagick on cirkus, so no problems with PDF output from ImageMagick anymore.
[webpdf] / createpdf.pl
index 78ed00ecddc923625db0ac49e0e0d3d59b67e6b1..8bece1ce8182857e30952eb6a617c8186c5a3cc6 100755 (executable)
@@ -1,10 +1,30 @@
 #! /usr/bin/perl
+#
+# webpdf -- small set of scripts to make PDF files automatically from various
+#           formats, via a web interface.
+#
+# Copyright 2005 Steinar H. Gunderson <sgunderson@bigfoot.com>. 
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, version 2.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+# 
 use CGI;
 use POSIX;
 use strict;
 use File::Temp;
+require './config.pm';
 
-$ENV{"HOME"} = "/home/cassarossa/itk/sesse/sesse-pdf/";
+$ENV{"HOME"} = $pdfweb::config::homedir;
 
 my $cgi = CGI->new;
 my $filename = $cgi->param('input');
@@ -50,7 +70,7 @@ if ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) {
        $outname = "$1.pdf";
 
        # Run through ImageMagick first of all, then gs
-       open PIPE, "| convert $2:- ps:- | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
+       open PIPE, "| convert $2:- pdf:- | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
                 or die "convert: $!";
 
        my ($buf, $ret);
@@ -84,7 +104,7 @@ if ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) {
        close DOC;
 
        # Create PostScript from OOo :-)
-       system("/usr/lib/openoffice/program/soffice -display :25 -headless -pt pdf /home/cassarossa/itk/sesse/public_html/pdf/output/$pdf_filename.$ext");
+       system("/usr/lib/openoffice/program/soffice -display $pdfweb::config::xserver -headless -pt pdf $pdfweb::config::outputdir/$pdf_filename.$ext");
 
        system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.pdf >&2");
 } elsif ($filename =~ /(.*)\.(c|cc|cpp|cs|h|py|rb|pl|diff|patch|js|php[1-5]?|hs|f|f90|java|css|sql|l|y|s?ml|sh|awk|m|v)$/i) {
@@ -102,7 +122,7 @@ if ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) {
        (my $sanitized_filename = $filename) =~ tr/a-zA-Z0-9./_/c;
 
        # This is just perverse :-P 
-       system("vim -Z -R +\"set printheader=\%<$sanitized_filename\%h\%m\%=Page\\ \%N\" +\"ha >/home/cassarossa/itk/sesse/public_html/pdf/output/$pdf_filename.ps\" +:q output/$pdf_filename.$ext");
+       system("vim -Z -R +\"set printheader=\%<$sanitized_filename\%h\%m\%=Page\\ \%N\" +\"ha >$pdfweb::config::outputdir/$pdf_filename.ps\" +:q output/$pdf_filename.$ext");
 
        system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.ps >&2");
 } else {
@@ -119,8 +139,10 @@ EOF
 
 my $size = -s "output/$pdf_filename";
 
+(my $sanitized_outname = $outname) =~ tr/a-zA-Z0-9. -/_/c;
+
 print "Content-type: application/pdf\n";
-print "Content-disposition: attachment; filename=\"$outname\"\n";  # FIXME: XSS problems?
+print "Content-disposition: attachment; filename=\"$sanitized_outname\"\n";
 print "Content-length: $size\n\n";
 
 system("cat output/$pdf_filename");  # yuck?