Skip to content

Commit 4182bee

Browse files
committed
refactor: migrated PDF export (#3834)
1 parent 09e114c commit 4182bee

File tree

5 files changed

+102
-169
lines changed

5 files changed

+102
-169
lines changed

phpmyfaq/pdf.php

Lines changed: 0 additions & 164 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* PDF Controller
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
12+
* @author Peter Beauvain <pbeauvain@web.de>
13+
* @author Olivier Plathey <olivier@fpdf.org>
14+
* @author Krzysztof Kruszynski <thywolf@wolf.homelinux.net>
15+
* @author Matteo Scaramuccia <matteo@phpmyfaq.de>
16+
* @copyright 2003-2026 phpMyFAQ Team
17+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
18+
* @link https://www.phpmyfaq.de
19+
* @since 2003-02-12
20+
*/
21+
22+
namespace phpMyFAQ\Controller\Frontend;
23+
24+
use DateTime;
25+
use League\CommonMark\Exception\CommonMarkException;
26+
use phpMyFAQ\Attachment\AttachmentException;
27+
use phpMyFAQ\Attachment\AttachmentFactory;
28+
use phpMyFAQ\Category;
29+
use phpMyFAQ\Core\Exception;
30+
use phpMyFAQ\Export\Pdf;
31+
use phpMyFAQ\Filter;
32+
use phpMyFAQ\Helper\AttachmentHelper;
33+
use phpMyFAQ\User\CurrentUser;
34+
use Symfony\Component\HttpFoundation\RedirectResponse;
35+
use Symfony\Component\HttpFoundation\Request;
36+
use Symfony\Component\HttpFoundation\Response;
37+
use Symfony\Component\Routing\Attribute\Route;
38+
39+
final class PdfController extends AbstractFrontController
40+
{
41+
/**
42+
* @throws Exception|\Exception|CommonMarkException
43+
*/
44+
#[Route(path: '/pdf/{categoryId}/{faqId}/{faqLanguage}', name: 'public.pdf.faq')]
45+
public function index(Request $request): Response
46+
{
47+
$categoryId = Filter::filterVar($request->attributes->get('categoryId'), FILTER_VALIDATE_INT);
48+
$faqId = Filter::filterVar($request->attributes->get('faqId'), FILTER_VALIDATE_INT);
49+
$faqLanguage = Filter::filterVar($request->attributes->get('faqLanguage'), FILTER_SANITIZE_SPECIAL_CHARS);
50+
51+
if ($categoryId === false || $categoryId === null || $faqId === false || $faqId === null) {
52+
return new RedirectResponse($this->configuration->getDefaultUrl());
53+
}
54+
55+
[$currentUser, $currentGroups] = CurrentUser::getCurrentUserGroupId($this->currentUser);
56+
57+
$faq = $this->container->get('phpmyfaq.faq');
58+
$faq->setUser($currentUser);
59+
$faq->setGroups($currentGroups);
60+
61+
$category = new Category($this->configuration, $currentGroups, true);
62+
$category->setUser($currentUser);
63+
64+
$tags = $this->container->get('phpmyfaq.tags');
65+
$tags->setUser($currentUser)->setGroups($currentGroups);
66+
67+
$pdf = new Pdf($faq, $category, $this->configuration);
68+
69+
$faq->getFaq($faqId);
70+
$faq->faqRecord['category_id'] = $categoryId;
71+
72+
if (!$this->configuration->get('records.disableAttachments') && 'yes' === $faq->faqRecord['active']) {
73+
try {
74+
$attachmentHelper = new AttachmentHelper();
75+
$attList = AttachmentFactory::fetchByRecordId($this->configuration, $faqId);
76+
$faq->faqRecord['attachmentList'] = $attachmentHelper->getAttachmentList($attList);
77+
} catch (AttachmentException) {
78+
$faq->faqRecord['attachmentList'] = '';
79+
}
80+
}
81+
82+
$filename = 'FAQ-' . $faqId . '-' . $faqLanguage . '.pdf';
83+
$pdfFile = $pdf->generateFile($faq->faqRecord, $filename);
84+
85+
$response = new Response();
86+
$response->setExpires(new DateTime());
87+
$response->headers->set('Content-Type', 'application/pdf');
88+
$response->setContent($pdfFile);
89+
90+
return $response;
91+
}
92+
}

phpmyfaq/src/phpMyFAQ/Services.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function setQuestion(string $question): void
100100
public function getPdfLink(): string
101101
{
102102
return sprintf(
103-
'%spdf.php?cat=%d&id=%d&artlang=%s',
103+
'%spdf/%d/%d/%s',
104104
$this->configuration->getDefaultUrl(),
105105
$this->getCategoryId(),
106106
$this->getFaqId(),
@@ -114,7 +114,7 @@ public function getPdfLink(): string
114114
public function getPdfApiLink(): string
115115
{
116116
return sprintf(
117-
'%spdf.php?cat=%d&id=%d&artlang=%s',
117+
'%spdf/%d/%d/%s',
118118
$this->configuration->getDefaultUrl(),
119119
$this->getCategoryId(),
120120
$this->getFaqId(),

phpmyfaq/src/public-routes.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use phpMyFAQ\Controller\Frontend\AttachmentController;use phpMyFAQ\Controller\Frontend\ContactController;
2222
use phpMyFAQ\Controller\Frontend\GlossaryController;use phpMyFAQ\Controller\Frontend\LoginController;use phpMyFAQ\Controller\Frontend\OverviewController;
2323
use phpMyFAQ\Controller\Frontend\PageNotFoundController;
24-
use phpMyFAQ\Controller\Frontend\SitemapController as FrontendSitemapController;
24+
use phpMyFAQ\Controller\Frontend\PdfController;use phpMyFAQ\Controller\Frontend\SitemapController as FrontendSitemapController;
2525
use phpMyFAQ\Controller\Frontend\WebAuthnController;
2626
use phpMyFAQ\Controller\LlmsController;
2727
use phpMyFAQ\Controller\RobotsController;
@@ -62,6 +62,11 @@
6262
'controller' => [OverviewController::class, 'index'],
6363
'methods' => 'GET',
6464
],
65+
'public.pdf.faq' => [
66+
'path' => '/pdf/{categoryId}/{faqId}/{faqLanguage}',
67+
'controller' => [PdfController::class, 'index'],
68+
'methods' => 'GET',
69+
],
6570
'public.sitemap' => [
6671
'path' => '/sitemap/{letter}/{language}.html',
6772
'controller' => [FrontendSitemapController::class, 'index'],

tests/phpMyFAQ/ServicesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testGetPdfLink(): void
3434
$services->setLanguage('en');
3535

3636
// Test getPdfLink method
37-
$expected = 'http://example.com/pdf.php?cat=1&id=123&artlang=en';
37+
$expected = 'http://example.com/pdf/1/123/en';
3838
$this->assertEquals($expected, $services->getPdfLink());
3939
}
4040

@@ -51,7 +51,7 @@ public function testGetPdfApiLink(): void
5151
$services->setLanguage('en');
5252

5353
// Test getPdfApiLink method
54-
$expected = 'http://example.com/pdf.php?cat=1&id=123&artlang=en';
54+
$expected = 'http://example.com/pdf/1/123/en';
5555
$this->assertEquals($expected, $services->getPdfApiLink());
5656
}
5757
}

0 commit comments

Comments
 (0)