Skip to content

Commit

Permalink
Item12679: align with changes in GenPDFPrincePlugin
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/GenPDFWebkitPlugin@17136 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed Dec 11, 2013
1 parent c7a169e commit f9d3180
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 23 deletions.
7 changes: 4 additions & 3 deletions data/System/GenPDFWebkitPlugin.txt
Expand Up @@ -14,8 +14,8 @@ Here's the complete Foswiki documentation in [[%SCRIPTURL{"view"}%/%SYSTEMWEB%/C

---++ Installation

This extension comes with a static =wkhtmltopdf= binary version 0.9.1 for linux in your foswiki's tools directory.
If you run on a different platform you will have to download it from http://code.google.com/p/wkhtmltopdf/.
Download =wkhtmltopdf= from http://code.google.com/p/wkhtmltopdf/ matching your platform and install it into the =tools/= directory of your
Foswiki installation. In case you installed it somewhere else use =configure= to point to the proper location.

%$INSTALL_INSTRUCTIONS%

Expand All @@ -39,11 +39,12 @@ as of wkhtmltopdf-0.8.3
* Set SHORTDESCRIPTION = 'Generate PDF using Webkit';
-->
| Author: | Foswiki:Main.MichaelDaum |
| Copyright: | © 2009-2010, Michael Daum http://michaeldaumconsulting.com |
| Copyright: | © 2009-2013, 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; |
| 12 Dec 2013 | align with changes in Foswiki:Extensions/GenPDFPrincePlugin |
| 12 Feb 2010 | upgraded to wkhtml-0.9.1 |
| 17 Nov 2009 | fixed use of uninitialized value |
| 09 Sep 2009 | initial release |
Expand Down
93 changes: 75 additions & 18 deletions lib/Foswiki/Plugins/GenPDFWebkitPlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2009-2010 Michael Daum http://michaeldaumconsulting.com
# Copyright (C) 2009-2013 Michael Daum http://michaeldaumconsulting.com
#
# This license applies to GenPDFWebkitPlugin *and also to any derivatives*
#
Expand All @@ -19,13 +19,16 @@
package Foswiki::Plugins::GenPDFWebkitPlugin;

use strict;
use warnings;

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

our $VERSION = '$Rev: 4419 (2009-07-03) $';
our $RELEASE = '1.00';
our $VERSION = '1.40';
our $RELEASE = '1.40';
our $SHORTDESCRIPTION = 'Generate PDF using Webkit';
our $NO_PREFS_IN_TOPIC = 1;
our $baseTopic;
Expand Down Expand Up @@ -64,52 +67,80 @@ sub completePageHandler {
require File::Temp;
require Foswiki::Sandbox;

my $content = Encode::decode($Foswiki::cfg{Site}{CharSet}, $_[0]);

# remove left-overs
$_[0] =~ s/([\t ]?)[ \t]*<\/?(nop|noautolink)\/?>/$1/gis;
$content =~ s/([\t ]?)[ \t]*<\/?(nop|noautolink)\/?>/$1/gis;
$content =~ s/<!--.*?-->//g;
$content =~ s/[\0-\x08\x0B\x0C\x0E-\x1F\x7F]+/ /g;

# clean url params in anchors as prince can't generate proper xrefs otherwise;
# hope this gets fixed in prince at some time
$content =~ s/(href=["'])\?.*(#[^"'\s])+/$1$2/g;

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

# create temp files
my $htmlFile = new File::Temp(SUFFIX => '.html', UNLINK => (DEBUG?0:1));
my $errorFile = new File::Temp(SUFFIX => '.log', UNLINK => (DEBUG?0:1));
my $htmlFile = new File::Temp(SUFFIX => '.html', UNLINK => (DEBUG ? 0 : 1));
my $errorFile = new File::Temp(SUFFIX => '.log', UNLINK => (DEBUG ? 0 : 1));

# create output filename
my ($pdfFilePath, $pdfFile) = getFileName($baseWeb, $baseTopic);

# creater html file
print $htmlFile "$_[0]";
#writeDebug("htmlFile=".$_[0]);
$content = Encode::encode($Foswiki::cfg{Site}{CharSet}, $content);

binmode($htmlFile);
print $htmlFile $content;
writeDebug("htmlFile=" . $htmlFile->filename);

# create webkit command
my $session = $Foswiki::Plugins::SESSION;
my $pubUrl = $session->getPubUrl(1); # SMELL: unofficial api
my $pdfCmd = $Foswiki::cfg{GenPDFWebkitPlugin}{WebkitCmd} ||
"$Foswiki::cfg{DataDir}/../tools/wkhtmltopdf -q --enable-plugins --outline --print-media-type %INFILE|F% %OUTFILE|F%";
'$Foswiki::cfg{ToolsDir}/wkhtmltopdf -q --enable-plugins --outline --print-media-type %INFILE|F% %OUTFILE|F%';

writeDebug("pdfCmd=$pdfCmd");
writeDebug("BASEURL=$pubUrl");

# execute
my ($output, $exit) = Foswiki::Sandbox->sysCommand(
$pdfCmd,
BASEURL => $pubUrl,
OUTFILE => $pdfFilePath,
INFILE => $htmlFile->filename,
ERROR => $errorFile->filename,
);

local $/ = undef;

writeDebug("errorFile=" . $errorFile->filename);
writeDebug("htmlFile=" . $htmlFile->filename);

my $error = '';
if ($exit || DEBUG) {
$error = <$errorFile>;
}

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

if ($exit) {
my $html = $_[0];
my $html = $content;
my $line = 1;
$html = '00000: '.$html;
$html =~ s/\n/"\n".(sprintf "\%05d", $line++).": "/ge;
throw Error::Simple("execution of wkhtmltopdf failed ($exit): \n\n$error\n\n$html");
}

my $url = Foswiki::Func::getScriptUrl($baseWeb, $baseTopic, 'viewfile',
filename=>$pdfFile,
t=>time(),
);
# not using viewfile to let the web server decide how to deliver the static file
#my $url = Foswiki::Func::getScriptUrl($baseWeb, $baseTopic, 'viewfile',
# filename=>$pdfFile,
# t=>time(),
#);
my $url = $Foswiki::cfg{PubUrlPath} . '/' . $baseWeb . '/' . $baseTopic . '/' . $pdfFile . '?t=' . time();

Foswiki::Func::redirectCgiQuery($query, $url);
}
Expand All @@ -118,7 +149,9 @@ sub completePageHandler {
sub getFileName {
my ($web, $topic) = @_;

my $fileName = $topic;
my $query = Foswiki::Func::getCgiQuery();
my $fileName = $query->param("outfile") || 'genpdf_'.$topic.'.pdf';

$fileName =~ s{[\\/]+$}{};
$fileName =~ s!^.*[\\/]!!;
$fileName =~ s/$Foswiki::regex{filenameInvalidCharRegex}//go;
Expand All @@ -127,12 +160,36 @@ sub getFileName {
my $filePath = Foswiki::Func::getPubDir().'/'.$web.'/'.$topic;
File::Path::mkpath($filePath);

$fileName = 'genpdf_'.$fileName.'.pdf';
$filePath .= '/'.$fileName;

return ($filePath, $fileName);
}

###############################################################################
sub toFileUrl {
my $url = shift;

my $fileUrl = $url;

if ($fileUrl =~ /^(?:$Foswiki::cfg{DefaultUrlHost})?$Foswiki::cfg{PubUrlPath}(.*)$/) {
$fileUrl = $1;
$fileUrl =~ s/\?.*$//;
if ($fileUrl =~ /^\/(.*)\/([^\/]+)\/[^\/]+$/) {
my $web = $1;
my $topic = $2;
my $wikiName = Foswiki::Func::getWikiName();
writeDebug("checking access for $wikiName on $web.$topic");
return '' unless Foswiki::Func::checkAccessPermission("VIEW", $wikiName, undef, $topic, $web);
}
$fileUrl = "file://".$Foswiki::cfg{PubDir}.$fileUrl;
} else {
writeDebug("url=$url does not point to the local server");
}

writeDebug("url=$url, fileUrl=$fileUrl");
return $fileUrl;
}

###############################################################################
sub modifyHeaderHandler {
my ($hopts, $request) = @_;
Expand Down
2 changes: 1 addition & 1 deletion lib/Foswiki/Plugins/GenPDFWebkitPlugin/Config.spec
Expand Up @@ -4,5 +4,5 @@
# wkhtmltopdf executable including complete path
# downloadable from http://code.google.com/p/wkhtmltopdf;
# note that this plugin comes with a wkhtmltopdf binary for linux in the tools directory.
$Foswiki::cfg{GenPDFWebkitPlugin}{WebkitCmd} = "$Foswiki::cfg{DataDir}/../tools/wkhtmltopdf -q --enable-plugins --outline --print-media-type %INFILE|F% %OUTFILE|F%";
$Foswiki::cfg{GenPDFWebkitPlugin}{WebkitCmd} = '$Foswiki::cfg{DataDir}/../tools/wkhtmltopdf -q --enable-plugins --outline --print-media-type %INFILE|F% %OUTFILE|F%';
1;
1 change: 0 additions & 1 deletion lib/Foswiki/Plugins/GenPDFWebkitPlugin/MANIFEST
@@ -1,4 +1,3 @@
data/System/GenPDFWebkitPlugin.txt 0644
lib/Foswiki/Plugins/GenPDFWebkitPlugin/Config.spec 0644
lib/Foswiki/Plugins/GenPDFWebkitPlugin.pm 0644
tools/wkhtmltopdf 0755
Binary file removed tools/wkhtmltopdf
Binary file not shown.

0 comments on commit f9d3180

Please sign in to comment.