Replace CSV export with PDF export
The results export is now a PDF (Dompdf) with all interactive slides. By default private slides (anonymous/internal feedback) only show their number of participants; ?internal=1 creates the complete version incl. their answers, marked as confidential on every page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5cd1be1cf8
commit
d1facd32a6
21 files changed
+1102
-110
No files matched your search
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\View;
|
||||
|
||||
use Dompdf\Dompdf;
|
||||
use Dompdf\Options;
|
||||
|
||||
/**
|
||||
* Renders the template as HTML (layout "pdf") and converts it to a PDF with Dompdf.
|
||||
*
|
||||
* Remote resources are disabled: everything (styles, fonts) must be part of the HTML.
|
||||
*/
|
||||
class PdfView extends AppView
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $layout = 'pdf';
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public static function contentType(): string
|
||||
{
|
||||
return 'application/pdf';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function initialize(): void
|
||||
{
|
||||
parent::initialize();
|
||||
$this->response = $this->response->withType('pdf');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function render(?string $template = null, string|false|null $layout = null): string
|
||||
{
|
||||
return $this->toPdf(parent::render($template, $layout));
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the template as HTML only (e.g. for tests).
|
||||
*
|
||||
* @param string|null $template Template
|
||||
* @return string
|
||||
*/
|
||||
public function renderHtml(?string $template = null): string
|
||||
{
|
||||
return parent::render($template);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $html Complete HTML document
|
||||
* @return string PDF data
|
||||
*/
|
||||
protected function toPdf(string $html): string
|
||||
{
|
||||
$cache = TMP . 'dompdf';
|
||||
if (!is_dir($cache)) {
|
||||
mkdir($cache, 0775, true);
|
||||
}
|
||||
$options = new Options([
|
||||
'isRemoteEnabled' => false,
|
||||
'isPhpEnabled' => false,
|
||||
'isJavascriptEnabled' => false,
|
||||
'defaultFont' => 'DejaVu Sans',
|
||||
'defaultPaperSize' => 'a4',
|
||||
'fontCache' => $cache,
|
||||
'tempDir' => $cache,
|
||||
]);
|
||||
$dompdf = new Dompdf($options);
|
||||
$dompdf->loadHtml($html, 'UTF-8');
|
||||
$dompdf->render();
|
||||
$this->addPageNumbers($dompdf);
|
||||
|
||||
return (string)$dompdf->output();
|
||||
}
|
||||
|
||||
/**
|
||||
* "Seite X von Y" bottom right on every page (CSS counter(pages) is not supported by Dompdf).
|
||||
* Matches .page-footer in the layout: 8pt, 16mm right margin, 8mm above the bottom edge.
|
||||
*
|
||||
* @param \Dompdf\Dompdf $dompdf Rendered document
|
||||
* @return void
|
||||
*/
|
||||
protected function addPageNumbers(Dompdf $dompdf): void
|
||||
{
|
||||
$canvas = $dompdf->getCanvas();
|
||||
$metrics = $dompdf->getFontMetrics();
|
||||
$font = $metrics->getFont('DejaVu Sans');
|
||||
if ($font === null) {
|
||||
return;
|
||||
}
|
||||
$size = 8;
|
||||
$text = __('Seite {0} von {1}', '{PAGE_NUM}', '{PAGE_COUNT}');
|
||||
$mm = 72 / 25.4;
|
||||
// Placeholders are replaced per page, so the width is estimated with one-digit numbers.
|
||||
$width = $metrics->getTextWidth(__('Seite {0} von {1}', '0', '0'), $font, $size);
|
||||
$canvas->page_text(
|
||||
$canvas->get_width() - 16 * $mm - $width,
|
||||
$canvas->get_height() - 8 * $mm - $size * 1.45,
|
||||
$text,
|
||||
$font,
|
||||
$size,
|
||||
[0.365, 0.384, 0.502],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user