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
25 require './config.pm';
27 $ENV{"HOME"} = $pdfweb::config::homedir;
30 my $filename = $cgi->param('input');
31 my $file = $cgi->upload('input');
33 # It kind of sucks that we just can't get the temporary file name from
34 # CGI.pm, but OK, here goes :-)
36 my $pdf_filename = join('', (0..9, 'A'..'Z', 'a'..'z')[
37 rand 62, rand 62, rand 62, rand 62, rand 62,
38 rand 62, rand 62, rand 62, rand 62, rand 62
42 my $psopts = "/setdistillerparams { } /def";
45 if ($cgi->param('preset') eq 'screen') {
46 $pdfopts = "-dPDFSETTINGS=/screen";
47 } elsif ($cgi->param('preset') eq 'ebook') {
48 $pdfopts = "-dPDFSETTINGS=/ebook";
49 } elsif ($cgi->param('preset') eq 'printer') {
50 $pdfopts = "-dPDFSETTINGS=/printer";
51 } elsif ($cgi->param('preset') eq 'prepress') {
52 $pdfopts = "-dPDFSETTINGS=/prepress";
53 } elsif ($cgi->param('preset') eq 'default') {
57 if ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) {
60 # Yay, just a round through GhostScript
61 open PIPE, "| gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
65 while ($ret = read($file, $buf, 1024)) {
69 } elsif ($filename =~ /(.*)\.(bmp|png|jpe?g|xpm)$/i) {
72 # Run through ImageMagick first of all, then gs
73 open PIPE, "| convert $2:- pdf:- | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
77 while ($ret = read($file, $buf, 1024)) {
81 } elsif ($filename =~ /(.*)\.txt$/i) {
85 open PIPE, "| mpage -da -1 - | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
89 while ($ret = read($file, $buf, 1024)) {
93 } elsif ($filename =~ /(.*)\.(doc|xls|ppt|sxw|sxc|sxi)$/i) {
98 open DOC, ">output/$pdf_filename.$ext"
99 or die "output/$pdf_filename.$ext: $!";
101 while ($ret = read($file, $buf, 1024)) {
106 # Create PostScript from OOo :-)
107 system("/usr/lib/openoffice/program/soffice -display $pdfweb::config::xserver -headless -pt pdf $pdfweb::config::outputdir/$pdf_filename.$ext");
109 system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.pdf >&2");
110 } 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) {
114 open DOC, ">output/$pdf_filename.$ext"
115 or die "output/$pdf_filename.$ext: $!";
117 while ($ret = read($file, $buf, 1024)) {
122 (my $sanitized_filename = $filename) =~ tr/a-zA-Z0-9./_/c;
124 # This is just perverse :-P
125 system("vim -Z -R +\"set printheader=\%<$sanitized_filename\%h\%m\%=Page\\ \%N\" +\"ha >$pdfweb::config::outputdir/$pdf_filename.ps\" +:q output/$pdf_filename.$ext");
127 system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.ps >&2");
130 Content-type: text/html
132 <html><head><title>Error</title></head>
134 <p>You sent an unknown file type.</p>
140 my $size = -s "output/$pdf_filename";
142 (my $sanitized_outname = $outname) =~ tr/a-zA-Z0-9. -/_/c;
144 print "Content-type: application/pdf\n";
145 print "Content-disposition: attachment; filename=\"$sanitized_outname\"\n";
146 print "Content-length: $size\n\n";
148 system("cat output/$pdf_filename"); # yuck?