Skip to content

Commit

Permalink
Item12790: set static context rendering pdf
Browse files Browse the repository at this point in the history
also: fix encoding errors again; 't see when I finally got it :(



git-svn-id: http://svn.foswiki.org/trunk/GenPDFPrincePlugin@17389 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed Mar 18, 2014
1 parent c29052b commit 7531a46
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
4 changes: 3 additions & 1 deletion data/System/GenPDFPrincePlugin.txt
Expand Up @@ -53,11 +53,13 @@ Note, that !ZonePlugin is not required anymore in newer Foswiki engines > 1.1.0.
* Set SHORTDESCRIPTION = 'Generate PDF using Prince XML';
-->
| Author: | Foswiki:Main.MichaelDaum |
| Copyright: | © 2009-2013, Michael Daum http://michaeldaumconsulting.com |
| Copyright: | © 2009-2014, Michael Daum http://michaeldaumconsulting.com |
| License: | GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 18 Mar 2014 | fixed encoding content again; \
setting =static= context when delivering pdf output |
| 18 Mar 2013 | added outfile url param to specify the file name of the pdf being generated |
| 08 Mar 2012 | fixed encoding of charset fed to prince xml; \
rewrite img urls from http: to file: urls while prince works on the local filesystem generating pdf |
Expand Down
52 changes: 29 additions & 23 deletions lib/Foswiki/Plugins/GenPDFPrincePlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2009-2013 Michael Daum http://michaeldaumconsulting.com
# Copyright (C) 2009-2014 Michael Daum http://michaeldaumconsulting.com
#
# This license applies to GenPDFPrincePlugin *and also to any derivatives*
#
Expand All @@ -23,16 +23,19 @@ use warnings;

use Foswiki::Func ();
use Foswiki::Plugins ();
use Foswiki::Sandbox ();
use Error qw(:try);
use File::Path ();
use Encode ();
use File::Temp ();

our $VERSION = '1.40';
our $RELEASE = '1.40';
our $VERSION = '1.50';
our $RELEASE = '1.50';
our $SHORTDESCRIPTION = 'Generate PDF using Prince XML';
our $NO_PREFS_IN_TOPIC = 1;
our $baseTopic;
our $baseWeb;
our $doit = 0;

use constant DEBUG => 0; # toggle me

Expand All @@ -51,23 +54,34 @@ sub initPlugin {
return 0;
}

my $query = Foswiki::Func::getCgiQuery();
my $contenttype = $query->param("contenttype") || 'text/html';

if ($contenttype eq "application/pdf") {
$doit = 1;
Foswiki::Func::getContext()->{static} = 1;
} else {
$doit = 0;
}

return 1;
}

###############################################################################
sub completePageHandler {
#my($html, $httpHeaders) = @_;

my $query = Foswiki::Func::getCgiQuery();
my $contenttype = $query->param("contenttype") || 'text/html';
return unless $doit;

# is this a pdf view?
return unless $contenttype eq "application/pdf";
my $siteCharSet = $Foswiki::cfg{Site}{CharSet};

require File::Temp;
require Foswiki::Sandbox;
my $content = $_[0];

my $content = Encode::decode($Foswiki::cfg{Site}{CharSet}, $_[0]);
unless ($siteCharSet =~ /utf\-8/i) {
# convert to utf8
$content = Encode::decode($siteCharSet, $content);
$content = Encode::encode_utf8($content);
}

# remove left-overs
$content =~ s/([\t ]?)[ \t]*<\/?(nop|noautolink)\/?>/$1/gis;
Expand All @@ -79,7 +93,7 @@ sub completePageHandler {
$content =~ s/(href=["'])\?.*(#[^"'\s])+/$1$2/g;

# rewrite some urls to use file://..
#$content =~ s/(<link[^>]+href=["'])([^"']+)(["'])/$1.toFileUrl($2).$3/ge;
$content =~ s/(<link[^>]+href=["'])([^"']+)(["'])/$1.toFileUrl($2).$3/ge;
$content =~ s/(<img[^>]+src=["'])([^"']+)(["'])/$1.toFileUrl($2).$3/ge;

# create temp files
Expand All @@ -90,8 +104,6 @@ sub completePageHandler {
my ($pdfFilePath, $pdfFile) = getFileName($baseWeb, $baseTopic);

# creater html file
$content = Encode::encode($Foswiki::cfg{Site}{CharSet}, $content);

binmode($htmlFile);
print $htmlFile $content;
writeDebug("htmlFile=" . $htmlFile->filename);
Expand Down Expand Up @@ -124,8 +136,8 @@ sub completePageHandler {
$error = <$errorFile>;
}

writeDebug("GenPDFPrincePlugin - error=$error");
writeDebug("GenPDFPrincePlugin - output=$output");
writeDebug("error=$error");
writeDebug("output=$output");

if ($exit) {
my $html = $content;
Expand All @@ -142,6 +154,7 @@ sub completePageHandler {
#);
my $url = $Foswiki::cfg{PubUrlPath} . '/' . $baseWeb . '/' . $baseTopic . '/' . $pdfFile . '?t=' . time();

my $query = Foswiki::Func::getCgiQuery();
Foswiki::Func::redirectCgiQuery($query, $url);
}

Expand Down Expand Up @@ -194,14 +207,7 @@ sub toFileUrl {
sub modifyHeaderHandler {
my ($hopts, $request) = @_;

my $query = Foswiki::Func::getCgiQuery();
my $contenttype = $query->param("contenttype") || 'text/html';

# is this a pdf view?
return unless $contenttype eq "application/pdf";

# add disposition
$hopts->{'Content-Disposition'} = "inline;filename=$baseTopic.pdf";
$hopts->{'Content-Disposition'} = "inline;filename=$baseTopic.pdf" if $doit;
}

1;

0 comments on commit 7531a46

Please sign in to comment.