@kirill.novinskiy
Document is here.
code sample
<?php
class CreateCorporateRestorationReportFileForBanks implements ShouldQueue
{
public function handle(
RestorationReportFileService $restorationReportFileService,
$arrays
): void
{
$arrays->map(function($bank) {
$template = Storage::disk('local')->path("templates/restoration_reports/mr_corporation.xlsx");
$excel = new ExcelWriter($template);
$excel->save(Storage::path("uploaded_excel.xlsx"));
if((isProduction() || isStaging())) {
$api = new App\Utils\AsposeApi();
$api->convertWorkbook("uploaded_excel.xlsx", "uploaded_excel.pdf", from_disk: 'efs', to_disk: 'efs', calculate_formulas: true);
}
});
}
}
<?php
namespace App\Utils;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Utils;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\Response;
class AsposeApi
{
private const BASE_URL = 'https://api.aspose.cloud';
private readonly Client $client;
private readonly string $aspose_client_id;
private readonly string $aspose_client_secret;
private readonly ?string $aspose_fonts_folder;
private ?string $access_token = null;
private int $tries = 3;
private int $backoff = 60;
public function __construct()
{
$this->aspose_client_id = env('ASPOSE_API_CLIENT_ID')
?? throw new Exception();
$this->aspose_client_secret = env('ASPOSE_API_CLIENT_SECRET')
?? throw new Exception();
$this->aspose_fonts_folder = env('ASPOSE_API_FONTS_FOLDER');
$this->client = new Client([
'base_uri' => self::BASE_URL,
'headers' => [
'User-Agent' => 'Aspose-Cells-Cloud-PHP/24.7/php',
],
]);
}
private function authenticate(int $attempt = 1): void
{
try {
$response = $this->client->post(
'/connect/token',
[
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/json',
],
'form_params' => [
'grant_type' => 'client_credentials',
'client_id' => $this->aspose_client_id,
'client_secret' => $this->aspose_client_secret,
],
],
);
} catch(RequestException $ex) {
$response = $ex->getResponse();
}
if($response->getStatusCode() === Response::HTTP_OK) {
$this->access_token = json_decode($response->getBody()->getContents())->access_token;
} else if(in_array($response->getStatusCode(), [Response::HTTP_UNAUTHORIZED, Response::HTTP_NOT_FOUND], true)) {
throw new Exception();
} else {
if ($attempt >= $this->tries) {
if(isset($ex)) {
throw $ex;
}
throw new Exception();
} else {
sleep($this->backoff);
$this->authenticate($attempt + 1);
}
}
}
public function convertWorkbook(
string $from_file_path,
string $to_file_path,
string $to_format = 'pdf',
string $from_disk = 'local',
string $to_disk = 'local',
bool $calculate_formulas = false
): void {
if ($calculate_formulas) {
$this->calculateFormulasAndConvertWorkbook(
$from_file_path,
$to_file_path,
$to_format,
$from_disk,
$to_disk
);
} else {
$this->convertLocalWorkbook(
$from_file_path,
$to_file_path,
$to_format,
$from_disk,
$to_disk
);
}
}
private function convertLocalWorkbook(
string $from_file_path,
string $to_file_path,
string $to_format = 'pdf',
string $from_disk = 'local',
string $to_disk = 'local'
): void {
if (! $this->access_token) {
$this->authenticate();
}
$response = $this->client->put(
'/v3.0/cells/convert',
[
'headers' => [
'Authorization' => 'Bearer '.$this->access_token,
],
'sink' => Storage::disk($to_disk)->path($to_file_path),
'query' => [
'format' => $to_format,
'region' => "Japan",
...(
$this->aspose_fonts_folder
? ['fontsFolder' => $this->aspose_fonts_folder]
: []
),
],
'multipart' => [[
'name' => 'file',
'contents' => Utils::tryFopen(
Storage::disk($from_disk)->path($from_file_path),
'rb'
),
]],
]
);
if ($response->getStatusCode() !== 200) {
throw new Exception();
}
}
private function calculateFormulasAndConvertWorkbook(
string $from_file_path,
string $to_file_path,
string $to_format = 'pdf',
string $from_disk = 'local',
string $to_disk = 'local'
): void {
$remote_file_path = 'tmp_files/'.uniqid().'.xlsx';
$this->uploadFile(
$from_file_path,
$remote_file_path,
$from_disk
);
$this->calculateFormulas($remote_file_path);
$this->convertRemoteWorkbook(
$remote_file_path,
$to_file_path,
$to_format,
$to_disk
);
}
public function uploadFile(
string $local_file_path,
string $remote_file_path,
string $local_disk = 'local',
int $attempt = 1
): void {
if (! $this->access_token) {
$this->authenticate();
}
try {
$response = $this->client->put(
'/v3.0/cells/storage/file/'.$remote_file_path,
[
'headers' => [
'Authorization' => 'Bearer '.$this->access_token,
],
'multipart' => [[
'name' => 'file',
'contents' => Utils::tryFopen(
Storage::disk($local_disk)->path($local_file_path),
'rb'
),
]],
]
);
} catch(RequestException $ex) {
$response = $ex->getResponse();
}
if($response->getStatusCode() === Response::HTTP_OK) {
return;
} else if(in_array($response->getStatusCode(), [Response::HTTP_UNAUTHORIZED, Response::HTTP_NOT_FOUND], true)) {
throw new Exception();
} else {
if ($attempt >= $this->tries) {
if(isset($ex)) {
throw $ex;
}
throw new Exception();
} else {
sleep($this->backoff);
$this->uploadFile($local_file_path, $remote_file_path, $local_disk, $attempt + 1);
}
}
if ($response->getStatusCode() !== 200) {
throw new Exception();
}
}
public function calculateFormulas(
string $remote_file_path,
int $attempt = 1
): void
{
if (! $this->access_token) {
$this->authenticate();
}
$base_name = basename($remote_file_path);
$dir_name = dirname($remote_file_path);
try {
$response = $this->client->post(
'/v3.0/cells/'.$base_name.'/calculateformula',
[
'headers' => [
'Authorization' => 'Bearer '.$this->access_token,
],
'query' => [
...($dir_name !== '.' ? ['folder' => $dir_name] : []),
],
'json' => [
'name' => $remote_file_path,
'options' => [],
],
]
);
} catch(RequestException $ex) {
$response = $ex->getResponse();
}
if($response->getStatusCode() === Response::HTTP_OK) {
return;
} else if(in_array($response->getStatusCode(), [Response::HTTP_UNAUTHORIZED, Response::HTTP_NOT_FOUND], true)) {
throw new Exception();
} else {
if ($attempt >= $this->tries) {
if(isset($ex)) {
throw $ex;
}
throw new Exception();
} else {
sleep($this->backoff);
$this->calculateFormulas($remote_file_path, $attempt + 1);
}
}
}
public function convertRemoteWorkbook(
string $remote_file_path,
string $to_file_path,
string $to_format = 'pdf',
string $to_disk = 'local',
int $attempt = 1
): void {
if (! $this->access_token) {
$this->authenticate();
}
$base_name = basename($remote_file_path);
$dir_name = dirname($remote_file_path);
try {
$response = $this->client->get(
'/v3.0/cells/'.$base_name,
[
'headers' => [
'Authorization' => 'Bearer '.$this->access_token,
],
'sink' => Storage::disk($to_disk)->path($to_file_path),
'query' => [
'format' => $to_format,
'folder' => $dir_name,
'region' => "Japan",
...(
$this->aspose_fonts_folder
? ['fontsFolder' => $this->aspose_fonts_folder]
: []
),
],
]
);
} catch(RequestException $ex) {
$response = $ex->getResponse();
}
if($response->getStatusCode() === Response::HTTP_OK) {
return;
} else if(in_array($response->getStatusCode(), [Response::HTTP_UNAUTHORIZED, Response::HTTP_NOT_FOUND], true)) {
throw new Exception();
} else {
if ($attempt >= $this->tries) {
if(isset($ex)) {
throw $ex;
}
throw new Exception();
} else {
sleep($this->backoff);
$this->convertRemoteWorkbook($remote_file_path, $to_file_path, $to_format, $to_disk, $attempt + 1);
}
}
}
}