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) {
62 if (defined($cgi->param('color'))) {
66 # Render through Gecko
67 $ENV{'DISPLAY'} = $pdfweb::config::xserver;
68 system("gnome-web-print", @options, $url, "$pdfweb::config::outputdir/$pdf_filename.ps");
69 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");
70 } elsif ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) {
73 # Yay, just a round through GhostScript
74 open PIPE, "| gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
78 while ($ret = read($file, $buf, 1024)) {
82 } elsif ($filename =~ /(.*)\.(bmp|png|jpe?g|xpm)$/i) {
85 # Run through ImageMagick first of all, then gs
86 open PIPE, "| convert $2:- pdf:- | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
90 while ($ret = read($file, $buf, 1024)) {
94 } elsif ($filename =~ /(.*)\.txt$/i) {
98 open PIPE, "| mpage -da -1 - | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
102 while ($ret = read($file, $buf, 1024)) {
106 } elsif ($filename =~ /(.*)\.(doc|xls|ppt)$/i) {
111 open DOC, ">output/$pdf_filename.$ext"
112 or die "output/$pdf_filename.$ext: $!";
114 while ($ret = read($file, $buf, 1024)) {
119 # Create PostScript from OOo :-)
120 system("/usr/lib/openoffice/program/soffice -display $pdfweb::config::xserver -headless -pt pdf $pdfweb::config::outputdir/$pdf_filename.$ext");
122 # This is quite hideous -- it looks like OO.o calls the file something slightly
123 # different depending on the time format, phase of the moon or something... So
125 my $inp_ps = "output/$pdf_filename.$ext.pdf";
127 $inp_ps = "output/$pdf_filename.pdf";
130 system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < $inp_ps >&2");
131 } 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) {
135 open DOC, ">output/$pdf_filename.$ext"
136 or die "output/$pdf_filename.$ext: $!";
138 while ($ret = read($file, $buf, 1024)) {
143 (my $sanitized_filename = $filename) =~ tr/a-zA-Z0-9./_/c;
145 # This is just perverse :-P
146 system("vim -Z -R +\"set printheader=\%<$sanitized_filename\%h\%m\%=Page\\ \%N\" +\"ha >$pdfweb::config::outputdir/$pdf_filename.ps\" +:q output/$pdf_filename.$ext");
148 system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.ps >&2");
151 Content-type: text/html
153 <html><head><title>Error</title></head>
155 <p>You sent an unknown file type.</p>
161 my $size = -s "output/$pdf_filename";
163 if (defined($size) && $size > 0) {
164 # Make a thumbnail from the finished PDF, for later reference. (Output to
165 # stdout is so we make sure we get only the first page; it's the simplest
166 # hack I can find offhand. :-) )
167 system("convert -resize 192x192 output/$pdf_filename png:- | convert png:- png:- > output/$pdf_filename.png");
168 open DESC, ">output/$pdf_filename.desc";
170 if ($url =~ /^http/i) {
174 $filename =~ tr/\n//d;
175 print DESC "$filename\n";
181 (my $sanitized_outname = $outname) =~ tr/a-zA-Z0-9. -/_/c;
183 print "Content-type: application/pdf\n";
184 print "Content-disposition: attachment; filename=\"$sanitized_outname\"\n";
185 print "Content-length: $size\n\n";
187 system("cat output/$pdf_filename"); # yuck?