Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Do not show icon if binary is not available #30

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/Extensions/PdfExportControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use SilverStripe\Core\Extension;
use SilverStripe\Versioned\Versioned;
use SilverStripe\Dev\Deprecation;
use SilverStripe\CMS\Controllers\ContentController;

class PdfExportControllerExtension extends Extension
{
Expand All @@ -29,6 +30,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 @@ -80,12 +85,7 @@ 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;
Expand All @@ -105,7 +105,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