]> git.sesse.net Git - webpdf/blob - createpdf.pl
Upgraded ImageMagick on cirkus, so no problems with PDF output from ImageMagick anymore.
[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         system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.pdf >&2");
110 } 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) {
111         $outname = "$1.pdf";
112         my $ext = $2;
113
114         open DOC, ">output/$pdf_filename.$ext"
115                 or die "output/$pdf_filename.$ext: $!";
116         my ($buf, $ret);
117         while ($ret = read($file, $buf, 1024)) {
118                 print DOC $buf;
119         }
120         close DOC;
121
122         (my $sanitized_filename = $filename) =~ tr/a-zA-Z0-9./_/c;
123
124         # This is just perverse :-P 
125         system("vim -Z -R +\"set printheader=\%<$sanitized_filename\%h\%m\%=Page\\ \%N\" +\"ha >$pdfweb::config::outputdir/$pdf_filename.ps\" +:q output/$pdf_filename.$ext");
126
127         system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.ps >&2");
128 } else {
129         print <<"EOF";
130 Content-type: text/html
131
132 <html><head><title>Error</title></head>
133 <body><h1>Error</h1>
134 <p>You sent an unknown file type.</p>
135 </body></html>
136 EOF
137         exit;
138 }
139
140 my $size = -s "output/$pdf_filename";
141
142 (my $sanitized_outname = $outname) =~ tr/a-zA-Z0-9. -/_/c;
143
144 print "Content-type: application/pdf\n";
145 print "Content-disposition: attachment; filename=\"$sanitized_outname\"\n";
146 print "Content-length: $size\n\n";
147
148 system("cat output/$pdf_filename");  # yuck?