]> git.sesse.net Git - webpdf/blob - createpdf.pl
1af0b87771aa36957a63f00c2b4563696fcc7b7f
[webpdf] / createpdf.pl
1 #! /usr/bin/perl
2 #
3 # webpdf -- small set of scripts to make PDF files automatically from various
4 #           formats, via a web interface.
5 #
6 # Copyright 2005 Steinar H. Gunderson <sgunderson@bigfoot.com>. 
7 #
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.
11 #
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.
16 #
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
20
21 use CGI;
22 use POSIX;
23 use strict;
24 use File::Temp;
25 require './config.pm';
26
27 $ENV{"HOME"} = $pdfweb::config::homedir;
28
29 my $cgi = CGI->new;
30 my $filename = $cgi->param('input');
31 my $file = $cgi->upload('input');
32 my $url = $cgi->param('url');
33
34 # It kind of sucks that we just can't get the temporary file name from
35 # CGI.pm, but OK, here goes :-)
36
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
40 ]) . '.pdf';
41
42 my $pdfopts = "";
43 my $psopts = "/setdistillerparams { } /def";
44 my $outname;
45
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') {
55         $psopts = "";
56 }
57
58 if ($url =~ /^http/i) {
59         $outname = "web.pdf";
60
61         # Render through Gecko
62         $ENV{'DISPLAY'} = $pdfweb::config::xserver;
63         system("gnome-web-print", "-f", $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) {
66         $outname = "$1.pdf";
67
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"
70                 or die "gs: $!";
71                 
72         my ($buf, $ret);
73         while ($ret = read($file, $buf, 1024)) {
74                 print PIPE $buf;
75         }
76         close PIPE;
77 } elsif ($filename =~ /(.*)\.(bmp|png|jpe?g|xpm)$/i) {
78         $outname = "$1.pdf";
79
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"
82                 or die "convert: $!";
83
84         my ($buf, $ret);
85         while ($ret = read($file, $buf, 1024)) {
86                 print PIPE $buf;
87         }
88         close PIPE;
89 } elsif ($filename =~ /(.*)\.txt$/i) {
90         $outname = "$1.pdf";
91         
92         # Use mpage
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"
94                 or die "convert: $!";
95
96         my ($buf, $ret);
97         while ($ret = read($file, $buf, 1024)) {
98                 print PIPE $buf;
99         }
100         close PIPE;
101 } elsif ($filename =~ /(.*)\.(doc|xls|ppt)$/i) {
102         $outname = "$1.pdf";
103         my $ext = $2;
104
105         # Oh my, OpenOffice
106         open DOC, ">output/$pdf_filename.$ext"
107                 or die "output/$pdf_filename.$ext: $!";
108         my ($buf, $ret);
109         while ($ret = read($file, $buf, 1024)) {
110                 print DOC $buf;
111         }
112         close DOC;
113
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");
116
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
119         # we try both.
120         my $inp_ps = "output/$pdf_filename.$ext.pdf";
121         if (! -r $inp_ps) {
122                 $inp_ps = "output/$pdf_filename.pdf";
123         }
124
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) {
127         $outname = "$1.pdf";
128         my $ext = $2;
129
130         open DOC, ">output/$pdf_filename.$ext"
131                 or die "output/$pdf_filename.$ext: $!";
132         my ($buf, $ret);
133         while ($ret = read($file, $buf, 1024)) {
134                 print DOC $buf;
135         }
136         close DOC;
137
138         (my $sanitized_filename = $filename) =~ tr/a-zA-Z0-9./_/c;
139
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");
142
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");
144 } else {
145         print <<"EOF";
146 Content-type: text/html
147
148 <html><head><title>Error</title></head>
149 <body><h1>Error</h1>
150 <p>You sent an unknown file type.</p>
151 </body></html>
152 EOF
153         exit;
154 }
155
156 my $size = -s "output/$pdf_filename";
157
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";
164
165         if ($url =~ /^http/i) {
166                 $url =~ tr/\n//d;
167                 print DESC "$url\n";
168         } else {
169                 $filename =~ tr/\n//d;
170                 print DESC "$filename\n";
171         }
172
173         close DESC;
174 }
175         
176 (my $sanitized_outname = $outname) =~ tr/a-zA-Z0-9. -/_/c;
177
178 print "Content-type: application/pdf\n";
179 print "Content-disposition: attachment; filename=\"$sanitized_outname\"\n";
180 print "Content-length: $size\n\n";
181
182 system("cat output/$pdf_filename");  # yuck?