3 # webpdf -- small set of scripts to make PDF files automatically from various
4 # formats, via a web interface.
6 # Copyright 2005 Steinar H. Gunderson <sgunderson@bigfoot.com>.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, version 2.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 $ENV{"HOME"} = "/home/cassarossa/itk/sesse/sesse-pdf/";
29 my $filename = $cgi->param('input');
30 my $file = $cgi->upload('input');
32 # It kind of sucks that we just can't get the temporary file name from
33 # CGI.pm, but OK, here goes :-)
35 my $pdf_filename = join('', (0..9, 'A'..'Z', 'a'..'z')[
36 rand 62, rand 62, rand 62, rand 62, rand 62,
37 rand 62, rand 62, rand 62, rand 62, rand 62
41 my $psopts = "/setdistillerparams { } /def";
44 if ($cgi->param('preset') eq 'screen') {
45 $pdfopts = "-dPDFSETTINGS=/screen";
46 } elsif ($cgi->param('preset') eq 'ebook') {
47 $pdfopts = "-dPDFSETTINGS=/ebook";
48 } elsif ($cgi->param('preset') eq 'printer') {
49 $pdfopts = "-dPDFSETTINGS=/printer";
50 } elsif ($cgi->param('preset') eq 'prepress') {
51 $pdfopts = "-dPDFSETTINGS=/prepress";
52 } elsif ($cgi->param('preset') eq 'default') {
56 if ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) {
59 # Yay, just a round through GhostScript
60 open PIPE, "| gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
64 while ($ret = read($file, $buf, 1024)) {
68 } elsif ($filename =~ /(.*)\.(bmp|png|jpe?g|xpm)$/i) {
71 # Run through ImageMagick first of all, then gs
72 open PIPE, "| convert $2:- ps:- | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
76 while ($ret = read($file, $buf, 1024)) {
80 } elsif ($filename =~ /(.*)\.txt$/i) {
84 open PIPE, "| mpage -da -1 - | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
88 while ($ret = read($file, $buf, 1024)) {
92 } elsif ($filename =~ /(.*)\.(doc|xls|ppt)$/i) {
97 open DOC, ">output/$pdf_filename.$ext"
98 or die "output/$pdf_filename.$ext: $!";
100 while ($ret = read($file, $buf, 1024)) {
105 # Create PostScript from OOo :-)
106 system("/usr/lib/openoffice/program/soffice -display :25 -headless -pt pdf /home/cassarossa/itk/sesse/public_html/pdf/output/$pdf_filename.$ext");
108 system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.pdf >&2");
109 } 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) {
113 open DOC, ">output/$pdf_filename.$ext"
114 or die "output/$pdf_filename.$ext: $!";
116 while ($ret = read($file, $buf, 1024)) {
121 (my $sanitized_filename = $filename) =~ tr/a-zA-Z0-9./_/c;
123 # This is just perverse :-P
124 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");
126 system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.ps >&2");
129 Content-type: text/html
131 <html><head><title>Error</title></head>
133 <p>You sent an unknown file type.</p>
139 my $size = -s "output/$pdf_filename";
141 print "Content-type: application/pdf\n";
142 print "Content-disposition: attachment; filename=\"$outname\"\n"; # FIXME: XSS problems?
143 print "Content-length: $size\n\n";
145 system("cat output/$pdf_filename"); # yuck?