Skip to content

Commit

Permalink
ENH Do not show icon if binary is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Dec 5, 2022
1 parent 69fa81a commit 37a22f9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
29 changes: 19 additions & 10 deletions src/Extensions/PdfExportControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SilverStripe\Core\Extension;
use SilverStripe\Versioned\Versioned;
use SilverStripe\Dev\Deprecation;
use SilverStripe\CMS\Controllers\ContentController;

class PdfExportControllerExtension extends Extension
{
Expand All @@ -28,6 +29,10 @@ public function downloadpdf()
if (!$this->owner->data()->config()->get('pdf_export')) {
return false;
}
$binaryPath = $this->getBinaryPath();
if (!$binaryPath) {
return HTTPResponse::create('PDF download is not available', 400);
}

// We only allow producing live pdf. There is no way to secure the draft files.
Versioned::set_stage(Versioned::LIVE);
Expand Down Expand Up @@ -79,17 +84,8 @@ public function getPDFProxy($pdfBaseUrl)
return $proxy;
}

/**
* Render the page as PDF using wkhtmltopdf.
*
* @return HTTPResponse|false
*/
public function generatePDF()
private function getBinaryPath()
{
if (!$this->owner->data()->config()->get('pdf_export')) {
return false;
}

$binaryPath = $this->owner->data()->config()->get('wkhtmltopdf_binary');
if ($binaryPath) {
Deprecation::notice('3.0', 'wkhtmltopdf_binary config is deprecated. '.
Expand All @@ -102,7 +98,20 @@ public function generatePDF()
$binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY');
}
}
return $binaryPath;
}

/**
* Render the page as PDF using wkhtmltopdf.
*
* @return HTTPResponse|false
*/
public function generatePDF()
{
if (!$this->owner->data()->config()->get('pdf_export')) {
return false;
}
$binaryPath = $this->getBinaryPath();
if (!$binaryPath) {
user_error(
'Neither WKHTMLTOPDF_BINARY nor ' . get_class($this->owner->data()) . '.wkhtmltopdf_binary are defined',
Expand Down
26 changes: 26 additions & 0 deletions src/Extensions/PdfExportExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use SilverStripe\Control\Director;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Versioned\Versioned;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Core\Environment;

class PdfExportExtension extends DataExtension
{
Expand All @@ -23,6 +25,12 @@ class PdfExportExtension extends DataExtension
*/
private static $pdf_base_url = '';

/**
* Used to bypass the check to see if the wkhtmltopdf binary is avilable
* Only used for unit testing
*/
private static bool $bypass_pdf_binary_check = false;

/**
* Allow custom overriding of the path to the WKHTMLTOPDF binary, in cases
* where multiple versions of the binary are available to choose from. This
Expand Down Expand Up @@ -68,6 +76,24 @@ public function PdfLink()
return false;
}

if (!$this->owner->config()->get('bypass_pdf_binary_check')) {
$binaryPath = $this->owner->config()->get('wkhtmltopdf_binary');
if ($binaryPath) {
Deprecation::notice('3.0', 'wkhtmltopdf_binary config is deprecated. '.
'Use WKHTMLTOPDF_BINARY env var instead.');
}
if (!$binaryPath || !is_executable($binaryPath ?? '')) {
if (Environment::getEnv('WKHTMLTOPDF_BINARY')
&& is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY') ?? '')
) {
$binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY');
}
}
if (!$binaryPath) {
return false;
}
}

$path = $this->getPdfFilename();

if ((Versioned::get_stage() === Versioned::LIVE) && file_exists($path ?? '')) {
Expand Down
3 changes: 2 additions & 1 deletion tests/Extensions/PdfExportExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ protected function setUp(): void

Config::modify()
->set(BasePage::class, 'pdf_export', true)
->set(BasePage::class, 'generated_pdf_path', 'assets/_generated_pdfs');
->set(BasePage::class, 'generated_pdf_path', 'assets/_generated_pdfs')
->set(BasePage::class, 'bypass_pdf_binary_check', true);
}

/**
Expand Down

0 comments on commit 37a22f9

Please sign in to comment.