Merge pull request #3964 from alchemy-fr/PHRAS-3633-change-pdf-filename

PHRAS-3633 merge title as the pdf filename
This commit is contained in:
Nicolas Maillat
2022-02-14 16:06:22 +01:00
committed by GitHub
4 changed files with 61 additions and 10 deletions

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -20,7 +20,7 @@ use \IntlDateFormatter as DateFormatter;
class PDFRecords extends PDF
{
private static $maxFilenameLength = 256;
public static $maxFilenameLength = 256;
/** @var Printer */
private $printer;
@@ -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);

View File

@@ -2,6 +2,14 @@
<form target="_blank" name="formprintpage" method="POST" action="{{ path('prod_printer_print') }}">
<div id="printBox">
<div style="padding:10px;">
<div style="margin: 20px 5px;">
<h4 style="margin-left: 0px;">{{ 'print:: pdf title' | trans }}</h4>
<input type="text" value="{{ pdfTitle }}" name="print-pdf-title" class="print-pdf-title" style="margin-left: 0px;width: 530px;padding-left: 0px;padding-right: 0px;margin-right: 0px;"/>
</div>
<div style="margin: 20px 5px;">
<h4 style="margin-left: 0px;">{{ 'print:: pdf description' | trans }}</h4>
<textarea name="print-pdf-description" class="print-pdf-description" id="print-pdf-description" cols="0" rows="5" style="margin-left: 20px;width:660px;"></textarea>
</div>
<h3 style="margin-left: 5px;">{{ 'print:: choose model' | trans }}</h3>
{% set total_count = printer.get_count_actionable() %}
@@ -57,14 +65,6 @@
{% endif %}
<h3 style="margin-left: 5px;">{{ 'print:: some options' | trans }}</h3>
<div style="margin: 20px 5px;">
<h4 style="margin-left: 0px;">{{ 'print:: pdf title' | trans }}</h4>
<input type="text" value="" name="print-pdf-title" class="print-pdf-title" style="margin-left: 0px;width: 530px;padding-left: 0px;padding-right: 0px;margin-right: 0px;"/>
</div>
<div style="margin: 20px 5px;">
<h4 style="margin-left: 0px;">{{ 'print:: pdf description' | trans }}</h4>
<textarea name="print-pdf-description" class="print-pdf-description" id="" cols="0" rows="5" style="margin-left: 20px;width:530px;"></textarea>
</div>
<div style="margin: 20px 5px;">
<h4 style="margin-left: 0px;">{{ 'print:: add and remember password to protect the pdf' | trans }}</h4>
<input type="text" value="" name="print-pdf-password" style="margin-left: 0px;width: 530px;padding-left: 0px;padding-right: 0px;margin-right: 0px;"/>
@@ -158,6 +158,7 @@
{% if printer.get_count_thumbnail() > 0 or printer.get_count_preview() > 0 %}
<div style="margin-bottom: 10px; text-align: center;">
<input type="hidden" name="lst" value="{{ printer.get_serialize_list }}" />
<input type="hidden" name="storyId" value="{{ storyId }}" />
<button type="submit" class="btn btn-inverse">{{ 'boutton::imprimer' | trans }}</button>
</div>
{% endif %}