X-Git-Url: https://git.sesse.net/?p=webpdf;a=blobdiff_plain;f=createpdf.pl;h=e74ab0e0119a08a9e3297e5b4891d634d7d9aca1;hp=9ee7aa92985d66be94b0d7573ea709ef2b5390de;hb=50e9f3cceadc1fe03463d4d2daaeeb9bc6c0634f;hpb=33885bda9f692a30c3b9783b7ade631b3d2040d4 diff --git a/createpdf.pl b/createpdf.pl index 9ee7aa9..e74ab0e 100755 --- a/createpdf.pl +++ b/createpdf.pl @@ -29,6 +29,7 @@ $ENV{"HOME"} = $pdfweb::config::homedir; my $cgi = CGI->new; my $filename = $cgi->param('input'); my $file = $cgi->upload('input'); +my $url = $cgi->param('url'); # It kind of sucks that we just can't get the temporary file name from # CGI.pm, but OK, here goes :-) @@ -54,7 +55,14 @@ if ($cgi->param('preset') eq 'screen') { $psopts = ""; } -if ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) { +if ($url =~ /^http/i) { + $outname = "web.pdf"; + + # Render through Gecko + $ENV{'DISPLAY'} = $pdfweb::config::xserver; + system("gnome-web-print", "-f", $url, "$pdfweb::config::outputdir/$pdf_filename.ps"); + system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < $pdfweb::config::outputdir/$pdf_filename.ps >&2"); +} elsif ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) { $outname = "$1.pdf"; # Yay, just a round through GhostScript @@ -90,7 +98,7 @@ if ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) { print PIPE $buf; } close PIPE; -} elsif ($filename =~ /(.*)\.(doc|xls|ppt|sxw|sxc|sxi)$/i) { +} elsif ($filename =~ /(.*)\.(doc|xls|ppt)$/i) { $outname = "$1.pdf"; my $ext = $2; @@ -106,7 +114,15 @@ if ($filename =~ /(.*)\.(?:e?ps|pdf)$/i) { # Create PostScript from OOo :-) system("/usr/lib/openoffice/program/soffice -display $pdfweb::config::xserver -headless -pt pdf $pdfweb::config::outputdir/$pdf_filename.$ext"); - system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < output/$pdf_filename.$ext.pdf >&2"); + # This is quite hideous -- it looks like OO.o calls the file something slightly + # different depending on the time format, phase of the moon or something... So + # we try both. + my $inp_ps = "output/$pdf_filename.$ext.pdf"; + if (! -r $inp_ps) { + $inp_ps = "output/$pdf_filename.pdf"; + } + + system("gs $pdfopts -dCompatbilityLevel=1.4 -dNOPAUSE -dPATCH -sDEVICE=pdfwrite -dSAFER -sOutputFile=output/$pdf_filename -c '.setpdfwrite $psopts' -f - < $inp_ps >&2"); } 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) { $outname = "$1.pdf"; my $ext = $2; @@ -139,6 +155,24 @@ EOF my $size = -s "output/$pdf_filename"; +if (defined($size) && $size > 0) { + # Make a thumbnail from the finished PDF, for later reference. (Output to + # stdout is so we make sure we get only the first page; it's the simplest + # hack I can find offhand. :-) ) + system("convert -resize 192x192 output/$pdf_filename png:- | convert png:- png:- > output/$pdf_filename.png"); + open DESC, ">output/$pdf_filename.desc"; + + if ($url =~ /^http/i) { + $url =~ tr/\n//d; + print DESC "$url\n"; + } else { + $filename =~ tr/\n//d; + print DESC "$filename\n"; + } + + close DESC; +} + (my $sanitized_outname = $outname) =~ tr/a-zA-Z0-9. -/_/c; print "Content-type: application/pdf\n";