1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 00:11:35 +02:00
invoiceninja/app/Jobs/ConvertProposalToPdf.php

51 lines
1.5 KiB
PHP
Raw Normal View History

2018-03-22 08:31:47 +01:00
<?php
namespace App\Jobs;
use App\Libraries\CurlUtils;
2019-03-26 07:40:03 +01:00
use Utils;
2018-03-22 08:31:47 +01:00
class ConvertProposalToPdf extends Job
{
public function __construct($proposal)
{
$this->proposal = $proposal;
}
public function handle()
{
2019-03-26 07:40:03 +01:00
if (! env('PHANTOMJS_CLOUD_KEY') && ! env('PHANTOMJS_BIN_PATH')) {
return false;
}
if (Utils::isTravis()) {
return false;
}
2018-03-22 08:31:47 +01:00
2019-03-26 07:40:03 +01:00
$proposal = $this->proposal;
$link = $proposal->getLink(true, true);
$phantomjsSecret = env('PHANTOMJS_SECRET');
$phantomjsLink = sprintf('%s?phantomjs=true&phantomjs_secret=%s', $link, $phantomjsSecret);
2018-03-22 08:31:47 +01:00
$filename = sprintf('%s/storage/app/%s.pdf', base_path(), strtolower(str_random(RANDOM_KEY_LENGTH)));
2019-03-26 07:40:03 +01:00
try {
$pdf = CurlUtils::renderPDF($phantomjsLink, $filename);
if (! $pdf && ($key = env('PHANTOMJS_CLOUD_KEY'))) {
$url = "http://api.phantomjscloud.com/api/browser/v2/{$key}/?request=%7Burl:%22{$link}?phantomjs=true%26phantomjs_secret={$phantomjsSecret}%22,renderType:%22pdf%22%7D";
$pdf = CurlUtils::get($url);
}
} catch (\Exception $exception) {
Utils::logError("PhantomJS - Failed to load {$phantomjsLink}: {$exception->getMessage()}");
return false;
}
if (! $pdf || strlen($pdf) < 200) {
Utils::logError("PhantomJS - Invalid response {$phantomjsLink}: {$pdf}");
return false;
}
2018-03-22 08:31:47 +01:00
return $pdf;
}
}