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');
32 my $url = $cgi->param('url');
34 # It kind of sucks that we just can't get the temporary file name from
35 # CGI.pm, but OK, here goes :-)
37 my $pdf_filename = join('', (0..9, 'A'..'Z', 'a'..'z')[
38 rand 62, rand 62, rand 62, rand 62, rand 62,
39 rand 62, rand 62, rand 62, rand 62, rand 62
43 my $psopts = "/setdistillerparams { } /def";
46 if ($cgi->param('preset') eq 'screen') {
47 $pdfopts = "-dPDFSETTINGS=/screen";
48 } elsif ($cgi->param('preset') eq 'ebook') {
49 $pdfopts = "-dPDFSETTINGS=/ebook";
50 } elsif ($cgi->param('preset') eq 'printer') {
51 $pdfopts = "-dPDFSETTINGS=/printer";
52 } elsif ($cgi->param('preset') eq 'prepress') {
53 $pdfopts = "-dPDFSETTINGS=/prepress";
54 } elsif ($cgi->param('preset') eq 'default') {
58 if ($url =~ /^http/i) {
61 # Render through Gecko
62 $ENV{'DISPLAY'} = $pdfweb::config::xserver;
63 system("gnome-web-print", $url, "$pdfweb::config::outputdir/$pdf_filename.ps");
64 system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < $pdfweb::config::outputdir/$pdf_filename.ps >&2");
65 } elsif ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) {
68 # Yay, just a round through GhostScript
69 open PIPE, "| gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
73 while ($ret = read($file, $buf, 1024)) {
77 } elsif ($filename =~ /(.*)\.(bmp|png|jpe?g|xpm)$/i) {
80 # Run through ImageMagick first of all, then gs
81 open PIPE, "| convert $2:- pdf:- | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
85 while ($ret = read($file, $buf, 1024)) {
89 } elsif ($filename =~ /(.*)\.txt$/i) {
93 open PIPE, "| mpage -da -1 - | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
97 while ($ret = read($file, $buf, 1024)) {
101 } elsif ($filename =~ /(.*)\.(doc|xls|ppt)$/i) {
106 open DOC, ">output/$pdf_filename.$ext"
107 or die "output/$pdf_filename.$ext: $!";
109 while ($ret = read($file, $buf, 1024)) {
114 # Create PostScript from OOo :-)
115 system("/usr/lib/openoffice/program/soffice -display $pdfweb::config::xserver -headless -pt pdf $pdfweb::config::outputdir/$pdf_filename.$ext");
117 # This is quite hideous -- it looks like OO.o calls the file something slightly
118 # different depending on the time format, phase of the moon or something... So
120 my $inp_ps = "output/$pdf_filename.$ext.pdf";
122 $inp_ps = "output/$pdf_filename.pdf";
125 system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < $inp_ps >&2");
126 } 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) {
130 open DOC, ">output/$pdf_filename.$ext"
131 or die "output/$pdf_filename.$ext: $!";
133 while ($ret = read($file, $buf, 1024)) {
138 (my $sanitized_filename = $filename) =~ tr/a-zA-Z0-9./_/c;
140 # This is just perverse :-P
141 system("vim -Z -R +\"set printheader=\%<$sanitized_filename\%h\%m\%=Page\\ \%N\" +\"ha >$pdfweb::config::outputdir/$pdf_filename.ps\" +:q output/$pdf_filename.$ext");
143 system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.ps >&2");
146 Content-type: text/html
148 <html><head><title>Error</title></head>
150 <p>You sent an unknown file type.</p>
156 my $size = -s "output/$pdf_filename";
158 if (defined($size) && $size > 0) {
159 # Make a thumbnail from the finished PDF, for later reference. (Output to
160 # stdout is so we make sure we get only the first page; it's the simplest
161 # hack I can find offhand. :-) )
162 system("convert -resize 192x192 output/$pdf_filename png:- > output/$pdf_filename.png");
163 open DESC, ">output/$pdf_filename.desc";
165 if ($url =~ /^http/i) {
169 $filename =~ tr/\n//d;
170 print DESC "$filename\n";
176 (my $sanitized_outname = $outname) =~ tr/a-zA-Z0-9. -/_/c;
178 print "Content-type: application/pdf\n";
179 print "Content-disposition: attachment; filename=\"$sanitized_outname\"\n";
180 print "Content-length: $size\n\n";
182 system("cat output/$pdf_filename"); # yuck?