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