Skip to content

Commit

Permalink
Item14112: added option pdfdisposition
Browse files Browse the repository at this point in the history
- remove leftovers from copy-pasting GenPDFPrincePlugin
  • Loading branch information
MichaelDaum committed Jul 11, 2016
1 parent d63d7a4 commit 2985fef
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 41 deletions.
14 changes: 7 additions & 7 deletions .gitignore
@@ -1,10 +1,10 @@
*,v
*.gz
*.swp
GenPDFWeasyPlugin.md5
GenPDFWeasyPlugin.sha1
GenPDFWeasyPlugin.tgz
GenPDFWeasyPlugin.txt
GenPDFWeasyPlugin.zip
GenPDFWeasyPlugin_installer
GenPDFWeasyPlugin_installer.pl
/GenPDFWeasyPlugin.md5
/GenPDFWeasyPlugin.sha1
/GenPDFWeasyPlugin.tgz
/GenPDFWeasyPlugin.txt
/GenPDFWeasyPlugin.zip
/GenPDFWeasyPlugin_installer
/GenPDFWeasyPlugin_installer.pl
25 changes: 17 additions & 8 deletions data/System/GenPDFWeasyPlugin.txt
@@ -1,6 +1,6 @@
%META:TOPICINFO{author="ProjectContributor" comment="" date="1442419725" format="1.1" version="1"}%
%META:TOPICINFO{author="ProjectContributor" comment="" date="1468228032" format="1.1" version="1"}%
---+!! %TOPIC%
%$SHORTDESCRIPTION%
%FORMFIELD{"Description"}%

%TOC%

Expand Down Expand Up @@ -55,22 +55,31 @@ probably need to tune the =$Foswiki::cfg{PluginsOrder}= to list the %TOPIC% behi
$Foswiki::cfg{PluginsOrder} = 'SpreadSheetPlugin,PageOptimizerPlugin,NatSkinPlugin,GenPDFWeasyPlugin,SlideShowPlugin';
</verbatim>

---++ Fonts
Note that =weasyprint= does not (yet) understand =@font-face=. To make use of custom fonts you will need to install them on the server itself.
Recommended font packages on a debian/ubuntu system:

<verbatim class="bash">
apt-get install fonts-font-awesome fonts-lato fonts-roboto ttf-mscorefonts-installer
</verbatim>

%$INSTALL_INSTRUCTIONS%

---++ Dependencies
%$DEPENDENCIES%

---++ Change History

%TABLE{columnwidths="7em" tablewidth="100%"}%
| 16 Sep 2015 | initial release |
| 11 Jul 2016 | added option =pdfdisposition= to inline or redirect content; remove leftovers from copy-pasting <nop>GenPDFPrincePlugin |
| 16 Sep 2015 | initial release |

%META:FORM{name="PackageForm"}%
%META:FIELD{name="Author" title="Author" value="Michael Daum"}%
%META:FIELD{name="Author" title="Author" value="Michael Daum"}
%META:FIELD{name="Version" title="Version" value="%25$VERSION%25"}%
%META:FIELD{name="Release" title="Release" value="%25$RELEASE%25"}%
%META:FIELD{name="Description" title="Description" value="%25$SHORTDESCRIPTION%25"}%
%META:FIELD{name="Repository" title="Repository" value="https://github.com/foswiki/%25$ROOTMODULE%25"}%
%META:FIELD{name="Copyright" title="Copyright" value="2015 Michael Daum http://michaeldaumconsulting.com"}%
%META:FIELD{name="Copyright" title="Copyright" value="2015-2016 Michael Daum http://michaeldaumconsulting.com"}%
%META:FIELD{name="License" title="License" value="GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]])"}%
%META:FIELD{name="Home" title="Home" value="http://foswiki.org/Extensions/%25$ROOTMODULE%25"}%
%META:FIELD{name="Support" title="Support" value="http://foswiki.org/Support/%25$ROOTMODULE%25"}%
%META:FIELD{name="Home" title="Home" value="https://foswiki.org/Extensions/%25$ROOTMODULE%25"}%
%META:FIELD{name="Support" title="Support" value="https://foswiki.org/Support/%25$ROOTMODULE%25"}%
78 changes: 52 additions & 26 deletions lib/Foswiki/Plugins/GenPDFWeasyPlugin.pm
@@ -1,6 +1,8 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# GenPDFWeasyPlugin is Copyright (C) 2015 Michael Daum http://michaeldaumconsulting.com
# Copyright (C) 2015-2016 Michael Daum http://michaeldaumconsulting.com
#
# This license applies to GenPDFPrincePlugin *and also to any derivatives*
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -19,10 +21,16 @@ use strict;
use warnings;

use Foswiki::Func ();

our $VERSION = '1.00';
our $RELEASE = '1.00';
our $SHORTDESCRIPTION = 'Generate PDF using WeasyPrint';
use Foswiki::Plugins ();
use Foswiki::Sandbox ();
use Foswiki::Plugins::JQueryPlugin ();
use File::Path ();
use Encode ();
use File::Temp ();

our $VERSION = '2.00';
our $RELEASE = '11 Jul 2016';
our $SHORTDESCRIPTION = 'Generate PDF using <nop>WeasyPrint';
our $NO_PREFS_IN_TOPIC = 1;
our $baseTopic;
our $baseWeb;
Expand Down Expand Up @@ -68,9 +76,9 @@ sub completePageHandler {

my $content = $_[0];

# convert to utf8
$content = Encode::decode($siteCharSet, $content) unless $Foswiki::UNICODE;
$content = Encode::encode_utf8($content);
# convert to utf8
$content = Encode::decode($siteCharSet, $content) unless $Foswiki::UNICODE;
$content = Encode::encode_utf8($content);

# remove left-overs and some basic clean-up
$content =~ s/([\t ]?)[ \t]*<\/?(nop|noautolink)\/?>/$1/gis;
Expand All @@ -79,9 +87,8 @@ sub completePageHandler {
$content =~ s/(<\/html>).*?$/$1/gs;
$content =~ s/^\s*$//gms;

# 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;
# clean url params in anchors
$content =~ s/(href=["'])\?.*(#[^"'\s])+/$1$2/g;

# rewrite some urls to use file://..
$content =~ s/(<link[^>]+href=["'])([^"']+)(["'])/$1.toFileUrl($2).$3/ge;
Expand All @@ -98,7 +105,7 @@ sub completePageHandler {
print $htmlFile $content;
writeDebug("htmlFile=" . $htmlFile->filename);

# create prince command
# create print command
my $pubUrl = getPubUrl();
my $cmd = $Foswiki::cfg{GenPDFWeasyPlugin}{WeasyCmd}
|| '/usr/local/bin/weasyprint --base-url %BASEURL|U% --media-type print --encoding utf-8 %INFILE|F% %OUTFILE|F%';
Expand All @@ -107,7 +114,7 @@ sub completePageHandler {
writeDebug("BASEURL=$pubUrl");

# execute
my ($output, $exit) = Foswiki::Sandbox->sysCommand(
my ($output, $exit, $error) = Foswiki::Sandbox->sysCommand(
$cmd,
BASEURL => $pubUrl,
OUTFILE => $pdfFilePath,
Expand All @@ -118,24 +125,26 @@ sub completePageHandler {

writeDebug("htmlFile=" . $htmlFile->filename);
writeDebug("output=$output");
#writeDebug("exit=$exit");
#writeDebug("error=$error");

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

# 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();

my $query = Foswiki::Func::getCgiQuery();
Foswiki::Func::redirectCgiQuery($query, $url);
if (($query->param("pdfdisposition") || '') eq 'inline') {
my $session = $Foswiki::Plugins::SESSION;
my $pdf = readFile($pdfFilePath);
$session->{response}->body($pdf);

# SMELL: prevent compression
$ENV{'HTTP_ACCEPT_ENCODING'} = '';
$ENV{'HTTP2'} = '';
} else {
my $url = $Foswiki::cfg{PubUrlPath} . '/' . $baseWeb . '/' . $baseTopic . '/' . $pdfFile . '?t=' . time();
Foswiki::Func::redirectCgiQuery($query, $url);
}
}

###############################################################################
Expand Down Expand Up @@ -198,4 +207,21 @@ sub getPubUrl {
return Foswiki::Func::getPubUrlPath(undef, undef, undef, absolute=>1);
}

###############################################################################
sub readFile {
my $name = shift;
my $data = '';
my $IN_FILE;

open($IN_FILE, '<', $name) || return '';
binmode $IN_FILE;

local $/ = undef; # set to read to EOF
$data = <$IN_FILE>;
close($IN_FILE);

$data = '' unless $data; # no undefined
return $data;
}

1;

0 comments on commit 2985fef

Please sign in to comment.