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