7 $ENV{"HOME"} = "/home/cassarossa/itk/sesse/sesse-pdf/";
10 my $filename = $cgi->param('input');
11 my $file = $cgi->upload('input');
13 # It kind of sucks that we just can't get the temporary file name from
14 # CGI.pm, but OK, here goes :-)
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
22 my $psopts = "/setdistillerparams { } /def";
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') {
37 if ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) {
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"
45 while ($ret = read($file, $buf, 1024)) {
49 } elsif ($filename =~ /(.*)\.(bmp|png|jpe?g|xpm)$/i) {
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"
57 while ($ret = read($file, $buf, 1024)) {
61 } elsif ($filename =~ /(.*)\.txt$/i) {
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"
69 while ($ret = read($file, $buf, 1024)) {
73 } elsif ($filename =~ /(.*)\.(doc|xls|ppt)$/i) {
78 open DOC, ">output/$pdf_filename.$ext"
79 or die "output/$pdf_filename.$ext: $!";
81 while ($ret = read($file, $buf, 1024)) {
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");
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) {
94 open DOC, ">output/$pdf_filename.$ext"
95 or die "output/$pdf_filename.$ext: $!";
97 while ($ret = read($file, $buf, 1024)) {
102 (my $sanitized_filename = $filename) =~ tr/a-zA-Z0-9./_/c;
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");
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");
110 Content-type: text/html
112 <html><head><title>Error</title></head>
114 <p>You sent an unknown file type.</p>
120 my $size = -s "output/$pdf_filename";
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";
126 system("cat output/$pdf_filename"); # yuck?