]> git.sesse.net Git - webpdf/blob - createpdf.pl
bd39e9c386e4958de35aa3f891de92a4e1efe34c
[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
26 $ENV{"HOME"} = "/home/cassarossa/itk/sesse/sesse-pdf/";
27
28 my $cgi = CGI->new;
29 my $filename = $cgi->param('input');
30 my $file = $cgi->upload('input');
31
32 # It kind of sucks that we just can't get the temporary file name from
33 # CGI.pm, but OK, here goes :-)
34
35 my $pdf_filename = join('', (0..9, 'A'..'Z', 'a'..'z')[
36         rand 62, rand 62, rand 62, rand 62, rand 62, 
37         rand 62, rand 62, rand 62, rand 62, rand 62
38 ]) . '.pdf';
39
40 my $pdfopts = "";
41 my $psopts = "/setdistillerparams { } /def";
42 my $outname;
43
44 if ($cgi->param('preset') eq 'screen') {
45         $pdfopts = "-dPDFSETTINGS=/screen";
46 } elsif ($cgi->param('preset') eq 'ebook') {
47         $pdfopts = "-dPDFSETTINGS=/ebook";
48 } elsif ($cgi->param('preset') eq 'printer') {
49         $pdfopts = "-dPDFSETTINGS=/printer";
50 } elsif ($cgi->param('preset') eq 'prepress') {
51         $pdfopts = "-dPDFSETTINGS=/prepress";
52 } elsif ($cgi->param('preset') eq 'default') {
53         $psopts = "";
54 }
55
56 if ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) {
57         $outname = "$1.pdf";
58
59         # Yay, just a round through GhostScript
60         open PIPE, "| gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
61                 or die "gs: $!";
62                 
63         my ($buf, $ret);
64         while ($ret = read($file, $buf, 1024)) {
65                 print PIPE $buf;
66         }
67         close PIPE;
68 } elsif ($filename =~ /(.*)\.(bmp|png|jpe?g|xpm)$/i) {
69         $outname = "$1.pdf";
70
71         # Run through ImageMagick first of all, then gs
72         open PIPE, "| convert $2:- ps:- | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
73                 or die "convert: $!";
74
75         my ($buf, $ret);
76         while ($ret = read($file, $buf, 1024)) {
77                 print PIPE $buf;
78         }
79         close PIPE;
80 } elsif ($filename =~ /(.*)\.txt$/i) {
81         $outname = "$1.pdf";
82         
83         # Use mpage
84         open PIPE, "| mpage -da -1 - | gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - >&2"
85                 or die "convert: $!";
86
87         my ($buf, $ret);
88         while ($ret = read($file, $buf, 1024)) {
89                 print PIPE $buf;
90         }
91         close PIPE;
92 } elsif ($filename =~ /(.*)\.(doc|xls|ppt)$/i) {
93         $outname = "$1.pdf";
94         my $ext = $2;
95
96         # Oh my, OpenOffice
97         open DOC, ">output/$pdf_filename.$ext"
98                 or die "output/$pdf_filename.$ext: $!";
99         my ($buf, $ret);
100         while ($ret = read($file, $buf, 1024)) {
101                 print DOC $buf;
102         }
103         close DOC;
104
105         # Create PostScript from OOo :-)
106         system("/usr/lib/openoffice/program/soffice -display :25 -headless -pt pdf /home/cassarossa/itk/sesse/public_html/pdf/output/$pdf_filename.$ext");
107
108         system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.pdf >&2");
109 } 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) {
110         $outname = "$1.pdf";
111         my $ext = $2;
112
113         open DOC, ">output/$pdf_filename.$ext"
114                 or die "output/$pdf_filename.$ext: $!";
115         my ($buf, $ret);
116         while ($ret = read($file, $buf, 1024)) {
117                 print DOC $buf;
118         }
119         close DOC;
120
121         (my $sanitized_filename = $filename) =~ tr/a-zA-Z0-9./_/c;
122
123         # This is just perverse :-P 
124         system("vim -Z -R +\"set printheader=\%<$sanitized_filename\%h\%m\%=Page\\ \%N\" +\"ha >/home/cassarossa/itk/sesse/public_html/pdf/output/$pdf_filename.ps\" +:q output/$pdf_filename.$ext");
125
126         system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.ps >&2");
127 } else {
128         print <<"EOF";
129 Content-type: text/html
130
131 <html><head><title>Error</title></head>
132 <body><h1>Error</h1>
133 <p>You sent an unknown file type.</p>
134 </body></html>
135 EOF
136         exit;
137 }
138
139 my $size = -s "output/$pdf_filename";
140
141 print "Content-type: application/pdf\n";
142 print "Content-disposition: attachment; filename=\"$outname\"\n";  # FIXME: XSS problems?
143 print "Content-length: $size\n\n";
144
145 system("cat output/$pdf_filename");  # yuck?