mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-08-05 15:42:14 +00:00
Takes over #690 by csoscd. The companion-view idea is theirs; this reworks it onto the shared page setup and fills in the gaps that stopped it landing. A `{template}_header` or `{template}_footer` view next to a template is rendered alongside it and repeated by Chromium on every page. The suffix resolves through the pdf_templates:: namespace too, so custom templates get it with no extra wiring. Two things had to change for that to be useful. Companion views are now hidden from the template picker. getFormattedTemplates() lists every .blade.php it finds, so an invoice1_footer would otherwise appear as a separately selectable template with no preview image -- the feature would have introduced that the moment anyone used it. And it does something out of the box. #690 shipped no companion views, so both of its margin settings were visible no-ops until someone hand-wrote a Blade file. Instead there is a pdf_page_numbers setting, off by default, that supplies a footer when a template has none. A template's own companion still wins, so turning page numbers on cannot overwrite a designed footer. The setting sits under Gotenberg because only Chromium can repeat a footer; dompdf has no equivalent. Its value is still carried by the dompdf form so saving from there cannot clear the choice -- the field is absent from that payload, and the controller only writes it when present. Margins come from the page setup rather than #690's separate header_margin and footer_margin. Chromium draws header and footer inside the page margin, so the existing margins are the space they occupy; two more settings for the same distance would have been a second way to say the same thing. Verified against a live gotenberg:8 on a two-page document: off produces no footer, on produces "1/2" and "2/2" on the respective pages, and a companion footer replaces both. #690's own test is not carried over. It asserted nothing: a bare View::shouldReceive('exists') is an allowance rather than an expectation, andReturn(false) never entered the companion branch, and the call sat inside try { } catch (Throwable) { }, so it passed with the feature deleted. Claude-Session: https://claude.ai/code/session_01QmECndmNZwzN65Zz9P87dF
152 lines
4.6 KiB
PHP
152 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Pdf;
|
|
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
class PdfTemplateUtils
|
|
{
|
|
/**
|
|
* Find the formatted template
|
|
*
|
|
* @param string $imageFormat
|
|
* @return array|null
|
|
*/
|
|
public static function findFormattedTemplate($templateType, $templateName, $imageFormat = 'base64')
|
|
{
|
|
foreach (array_reverse(self::getFormattedTemplates($templateType, $imageFormat)) as $formattedTemplate) {
|
|
if ($formattedTemplate['name'] === $templateName) {
|
|
return $formattedTemplate;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Return the available formatted template paths
|
|
*
|
|
* @param string $imageFormat
|
|
* @return array|array[]
|
|
*/
|
|
public static function getFormattedTemplates($templateType, $imageFormat = 'base64')
|
|
{
|
|
|
|
$files_native = array_map(function ($file) {
|
|
return [
|
|
'path' => $file,
|
|
'custom' => false,
|
|
];
|
|
}, Storage::disk('views')->files(sprintf('/app/pdf/%s', $templateType)));
|
|
|
|
$files_custom = array_map(function ($file) {
|
|
return [
|
|
'path' => $file,
|
|
'custom' => true,
|
|
];
|
|
}, Storage::disk('pdf_templates')->files(sprintf('/%s', $templateType)));
|
|
|
|
$files = array_merge($files_native, $files_custom);
|
|
$files = array_filter($files, function ($file) {
|
|
if (! Str::endsWith($file['path'], '.blade.php')) {
|
|
return false;
|
|
}
|
|
|
|
// `{template}_header` / `{template}_footer` are companions rendered
|
|
// alongside their template by the Gotenberg driver, not templates in
|
|
// their own right. Without this they show up in the picker as
|
|
// selectable entries with no preview image.
|
|
return ! Str::endsWith(
|
|
Str::before(basename($file['path']), '.blade.php'),
|
|
['_header', '_footer']
|
|
);
|
|
});
|
|
|
|
return array_map(function ($file) use ($templateType, $imageFormat) {
|
|
$templateName = Str::before(basename($file['path']), '.blade.php');
|
|
|
|
if ($file['custom']) {
|
|
$imagePath = self::getCustomTemplateFilePath($templateType, sprintf('%s.png', $templateName));
|
|
$isCustomTemplate = true;
|
|
} else {
|
|
$imagePath = resource_path('static/img/PDF/'.$templateName.'.png');
|
|
$isCustomTemplate = false;
|
|
}
|
|
|
|
if (empty($imageFormat)) {
|
|
$imageValue = '';
|
|
} elseif ($imageFormat == 'path') {
|
|
$imageValue = $imagePath;
|
|
} else {
|
|
$imageValue = File::exists($imagePath) ? ImageUtils::toBase64Src($imagePath) : '';
|
|
}
|
|
|
|
return [
|
|
'name' => $templateName,
|
|
'path' => $imageValue,
|
|
'custom' => $isCustomTemplate,
|
|
];
|
|
}, $files);
|
|
}
|
|
|
|
/**
|
|
* Returns custom template path
|
|
*
|
|
* @param string $fileName
|
|
*/
|
|
public static function getCustomTemplateFilePath($templateType, $fileName = ''): string
|
|
{
|
|
$path = ! empty($fileName) ? sprintf('/%s/%s', $templateType, $fileName) : sprintf('/%s/', $templateType);
|
|
|
|
return Storage::disk('pdf_templates')->path($path);
|
|
}
|
|
|
|
/**
|
|
* Check if custom template exists.
|
|
*
|
|
* @param $templateName
|
|
* @return string
|
|
*/
|
|
public static function customTemplateFileExists($templateType, $fileName)
|
|
{
|
|
return Storage::disk('pdf_templates')->exists(sprintf('/%s/%s', $templateType, $fileName));
|
|
}
|
|
|
|
/**
|
|
* Save template markup file
|
|
*
|
|
* @return bool|string
|
|
*/
|
|
public static function toCustomTemplateMarkupFile($contents, $templateType, $templateName)
|
|
{
|
|
return self::toCustomTemplateFile($contents, $templateType, $templateName.'.blade.php');
|
|
}
|
|
|
|
/**
|
|
* Save template image file
|
|
*
|
|
*
|
|
* @return bool|string
|
|
*/
|
|
public static function toCustomTemplateImageFile($contents, $templateType, $templateName, $imageType = 'png')
|
|
{
|
|
return self::toCustomTemplateFile($contents, $templateType, $templateName.'.'.$imageType);
|
|
}
|
|
|
|
/**
|
|
* Save file contents into a template file of specific template type.
|
|
*
|
|
*
|
|
* @return bool|string
|
|
*/
|
|
public static function toCustomTemplateFile($contents, $templateType, $fileName)
|
|
{
|
|
return Storage::disk('pdf_templates')->put(
|
|
sprintf('/%s/%s', $templateType, $fileName),
|
|
$contents
|
|
);
|
|
}
|
|
}
|