Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
Initial code import
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Morgan committed May 30, 2014
1 parent 319becc commit 17de2b4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
79 changes: 79 additions & 0 deletions code/extensions/PDFExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* PDFExtension
* Facilitate the generation of an SSViewer rendered PDF.
* @author Will Morgan <@willmorgan>
*/
class PDFExtension extends DataExtension {

/**
* @var string Absolute path to the wkhtmltopdf binary
*/
static private $wkhtmltopdf_binary = null;

/**
* @var string As the page is generated on the server, the public facing host
* will typically not be accessible, and instead be localhost.
*/
static private $render_host = 'http://localhost/';

/**
* @return string
*/
static public function get_wkhtmltopdf_binary() {
return static::config()->get('wkhtmltopdf_binary');
}

/**
* @return string
*/
static public function get_render_host() {
return static::config()->get('render_host');
}

/**
* @return Config_ForClass
*/
static public function config() {
return Config::inst()->forClass(get_called_class());
}

/**
* The template to use to render this PDF. The owner object can override this.
* @return string
*/
public function getPDFTemplate() {
return sprintf("%s_pdf", $this->owner->class);
}

/**
* Render the object using SSViewer
* @return string
*/
public function forPDF($variables = array()) {
Config::nest();
Config::inst()->update('Director', 'alternate_base_url', static::get_render_host());
$file = $this->owner->getPDFTemplate();
$viewer = new SSViewer($file);
$output = $viewer->process($this->owner, $variables);
Config::unnest();
return $output;
}

/**
* @param array $userOptions See \Knp\Snappy\Pdf->configure
* @param array $variables Variables for use in SSViewer
* @return string PDF file
*/
public function generatePDF($userOptions = array(), $variables = array()) {
require_once BASE_PATH . '/vendor/autoload.php';
$defaults = array(
'enable-javascript' => false,
);
$output = $this->forPDF();
$snappy = new \Knp\Snappy\Pdf(static::get_wkhtmltopdf_binary());
return $snappy->getOutputFromHTML($this->forPDF($variables), array_merge($defaults, $userOptions));
}

}

0 comments on commit 17de2b4

Please sign in to comment.