From 6d3510b8f2ed2b02ae4f08a8c30c9d06b3537d59 Mon Sep 17 00:00:00 2001 From: aynsix Date: Fri, 11 Feb 2022 12:15:31 +0300 Subject: [PATCH 1/2] title as pdf name --- .../Controller/Prod/PrinterController.php | 31 +++++++++++++++++++ lib/Alchemy/Phrasea/Helper/Record/Printer.php | 19 ++++++++++++ lib/Alchemy/Phrasea/Out/Module/PDFRecords.php | 2 +- .../prod/actions/printer_default.html.twig | 3 +- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/lib/Alchemy/Phrasea/Controller/Prod/PrinterController.php b/lib/Alchemy/Phrasea/Controller/Prod/PrinterController.php index cfba4d2d33..4ba69bd172 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/PrinterController.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/PrinterController.php @@ -11,10 +11,12 @@ namespace Alchemy\Phrasea\Controller\Prod; use Alchemy\Phrasea\Application\Helper\DataboxLoggerAware; use Alchemy\Phrasea\Controller\Controller; +use Alchemy\Phrasea\Controller\RecordsRequest; use Alchemy\Phrasea\Helper\Record as RecordHelper; use Alchemy\Phrasea\Out\Module\PDFRecords; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\ResponseHeaderBag; class PrinterController extends Controller { @@ -31,9 +33,25 @@ class PrinterController extends Controller } } + $pdfTitle = ''; + $storyId = null; + + if ($printer->is_basket()) { + $pdfTitle = $printer->get_original_basket()->getName(); + } + + $r = RecordsRequest::fromRequest($this->app, $request, false); + + if ($r->isSingleStory()) { + $pdfTitle = $r->singleStory()->get_title(); + $storyId = $r->singleStory()->getId(); + } + return $this->render('prod/actions/printer_default.html.twig', [ 'printer' => $printer, 'message' => '', + 'storyId' => $storyId, + 'pdfTitle'=> $pdfTitle, 'basketFeedbackId' => $basketFeedbackId, ]); } @@ -66,10 +84,23 @@ class PrinterController extends Controller $PDF = new PDFRecords($this->app, $printer, $layout, $title, $description, $userPassword, $canDownload, $downloadSubdef); + $pdfName = ''; + + if (!empty($title)) { + $pdfName = $printer->normalizeString($title); + $pdfName .= '.pdf'; + } + $response = new Response($PDF->render(), 200, array('Content-Type' => 'application/pdf')); $response->headers->set('Pragma', 'public', true); $response->setMaxAge(0); + $disposition = $response->headers->makeDisposition( + ResponseHeaderBag::DISPOSITION_INLINE, + $pdfName + ); + $response->headers->set('Content-Disposition', $disposition); + return $response; } diff --git a/lib/Alchemy/Phrasea/Helper/Record/Printer.php b/lib/Alchemy/Phrasea/Helper/Record/Printer.php index 11fdca4f8c..6568ae87d7 100644 --- a/lib/Alchemy/Phrasea/Helper/Record/Printer.php +++ b/lib/Alchemy/Phrasea/Helper/Record/Printer.php @@ -14,6 +14,8 @@ namespace Alchemy\Phrasea\Helper\Record; use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Helper\Record\Helper as RecordHelper; use Alchemy\Phrasea\Media\Subdef\Subdef; +use Alchemy\Phrasea\Out\Module\PDFRecords; +use Cocur\Slugify\Slugify; use Symfony\Component\HttpFoundation\Request; class Printer extends RecordHelper @@ -23,6 +25,7 @@ class Printer extends RecordHelper private $previewName = 'preview'; private $urlTtl = null; private $titleAsDownloadName = true; + private $slugify; /** * @var \ACL @@ -211,4 +214,20 @@ class Printer extends RecordHelper return $this->titleAsDownloadName; } + public function sanitizeString($string) + { + return str_replace(['/', '\\'], '', $string); + } + + public function normalizeString($string) + { + if (!isset($this->slugify)) { + $this->slugify = new Slugify(); + } + + $string = $this->sanitizeString($string); + + return mb_substr($this->slugify->slugify($string, '-'), 0, PDFRecords::$maxFilenameLength); + } + } diff --git a/lib/Alchemy/Phrasea/Out/Module/PDFRecords.php b/lib/Alchemy/Phrasea/Out/Module/PDFRecords.php index 482f615361..03ffc65636 100644 --- a/lib/Alchemy/Phrasea/Out/Module/PDFRecords.php +++ b/lib/Alchemy/Phrasea/Out/Module/PDFRecords.php @@ -20,7 +20,7 @@ use \IntlDateFormatter as DateFormatter; class PDFRecords extends PDF { - private static $maxFilenameLength = 256; + public static $maxFilenameLength = 256; /** @var Printer */ private $printer; diff --git a/templates/web/prod/actions/printer_default.html.twig b/templates/web/prod/actions/printer_default.html.twig index bd2596f3fd..e8bf106ec3 100644 --- a/templates/web/prod/actions/printer_default.html.twig +++ b/templates/web/prod/actions/printer_default.html.twig @@ -59,7 +59,7 @@

{{ 'print:: some options' | trans }}

{{ 'print:: pdf title' | trans }}

- +

{{ 'print:: pdf description' | trans }}

@@ -158,6 +158,7 @@ {% if printer.get_count_thumbnail() > 0 or printer.get_count_preview() > 0 %}
+
{% endif %} From 640823dfbbf31b81e350160d234e2d3c104e3205 Mon Sep 17 00:00:00 2001 From: aynsix Date: Fri, 11 Feb 2022 18:36:00 +0300 Subject: [PATCH 2/2] some fix --- lib/Alchemy/Phrasea/Out/Module/PDFRecords.php | 2 +- .../web/prod/actions/printer_default.html.twig | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Alchemy/Phrasea/Out/Module/PDFRecords.php b/lib/Alchemy/Phrasea/Out/Module/PDFRecords.php index 03ffc65636..6109f3bc90 100644 --- a/lib/Alchemy/Phrasea/Out/Module/PDFRecords.php +++ b/lib/Alchemy/Phrasea/Out/Module/PDFRecords.php @@ -992,7 +992,7 @@ class PDFRecords extends PDF $this->pdfTitle, '', 1, 'C', false); - $this->pdf->Write(20, "\n"); + $this->pdf->SetY($this->pdf->GetY() + 15); $this->pdf->SetFont(PhraseaPDF::FONT, '', 12); $this->pdf->writeHTML($this->pdfDescription); diff --git a/templates/web/prod/actions/printer_default.html.twig b/templates/web/prod/actions/printer_default.html.twig index e8bf106ec3..66c8656878 100644 --- a/templates/web/prod/actions/printer_default.html.twig +++ b/templates/web/prod/actions/printer_default.html.twig @@ -2,6 +2,14 @@
+
+

{{ 'print:: pdf title' | trans }}

+ +
+
+

{{ 'print:: pdf description' | trans }}

+ +

{{ 'print:: choose model' | trans }}

{% set total_count = printer.get_count_actionable() %} @@ -57,14 +65,6 @@ {% endif %}

{{ 'print:: some options' | trans }}

-
-

{{ 'print:: pdf title' | trans }}

- -
-
-

{{ 'print:: pdf description' | trans }}

- -

{{ 'print:: add and remember password to protect the pdf' | trans }}