]> git.sesse.net Git - webpdf/blob - createpdf.pl
Support FG/BG colors in web pages.
[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         my @options = ("-f");
61
62         if (defined($cgi->param('color'))) {
63                 push @options, "-c";
64         }
65
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) {
71         $outname = "$1.pdf";
72
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"
75                 or die "gs: $!";
76                 
77         my ($buf, $ret);
78         while ($ret = read($file, $buf, 1024)) {
79                 print PIPE $buf;
80         }
81         close PIPE;
82 } elsif ($filename =~ /(.*)\.(bmp|png|jpe?g|xpm)$/i) {
83         $outname = "$1.pdf";
84
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"
87                 or die "convert: $!";
88
89         my ($buf, $ret);
90         while ($ret = read($file, $buf, 1024)) {
91                 print PIPE $buf;
92         }
93         close PIPE;
94 } elsif ($filename =~ /(.*)\.txt$/i) {
95         $outname = "$1.pdf";
96         
97         # Use mpage
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"
99                 or die "convert: $!";
100
101         my ($buf, $ret);
102         while ($ret = read($file, $buf, 1024)) {
103                 print PIPE $buf;
104         }
105         close PIPE;
106 } elsif ($filename =~ /(.*)\.(doc|xls|ppt)$/i) {
107         $outname = "$1.pdf";
108         my $ext = $2;
109
110         # Oh my, OpenOffice
111         open DOC, ">output/$pdf_filename.$ext"
112                 or die "output/$pdf_filename.$ext: $!";
113         my ($buf, $ret);
114         while ($ret = read($file, $buf, 1024)) {
115                 print DOC $buf;
116         }
117         close DOC;
118
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");
121
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
124         # we try both.
125         my $inp_ps = "output/$pdf_filename.$ext.pdf";
126         if (! -r $inp_ps) {
127                 $inp_ps = "output/$pdf_filename.pdf";
128         }
129
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) {
132         $outname = "$1.pdf";
133         my $ext = $2;
134
135         open DOC, ">output/$pdf_filename.$ext"
136                 or die "output/$pdf_filename.$ext: $!";
137         my ($buf, $ret);
138         while ($ret = read($file, $buf, 1024)) {
139                 print DOC $buf;
140         }
141         close DOC;
142
143         (my $sanitized_filename = $filename) =~ tr/a-zA-Z0-9./_/c;
144
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");
147
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");
149 } else {
150         print <<"EOF";
151 Content-type: text/html
152
153 <html><head><title>Error</title></head>
154 <body><h1>Error</h1>
155 <p>You sent an unknown file type.</p>
156 </body></html>
157 EOF
158         exit;
159 }
160
161 my $size = -s "output/$pdf_filename";
162
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";
169
170         if ($url =~ /^http/i) {
171                 $url =~ tr/\n//d;
172                 print DESC "$url\n";
173         } else {
174                 $filename =~ tr/\n//d;
175                 print DESC "$filename\n";
176         }
177
178         close DESC;
179 }
180         
181 (my $sanitized_outname = $outname) =~ tr/a-zA-Z0-9. -/_/c;
182
183 print "Content-type: application/pdf\n";
184 print "Content-disposition: attachment; filename=\"$sanitized_outname\"\n";
185 print "Content-length: $size\n\n";
186
187 system("cat output/$pdf_filename");  # yuck?