Skip to content

Commit 7c46d40

Browse files
AngelFQCclaude
andcommitted
Refactor: Reuse PDF class mPDF instance for single-page rendering
Instead of instantiating a second mPDF object inside singlePageHtmlToPdfDownload(), build a PDF instance via the constructor and reuse its $this->pdf. This leaves a single 'new Mpdf' call (and a single SafeMpdfHttpClient guard) in the whole class. To allow the zero-margin certificate layout through the constructor, the constructor now honors 'margin_header'/'margin_footer' from $params instead of hard-coding both to 8. Both still default to 8, so callers that don't pass them are unaffected. Behavior change: lp_tracking.php builds 'new PDF('A4', 'P', ['margin_footer' => 4, ...])'; that value was previously discarded (footer rendered at 8mm) and now takes effect (4mm), matching the caller's original intent. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent becf842 commit 7c46d40

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

public/main/inc/lib/pdf.lib.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function __construct(
5252
$params['right'] = $params['right'] ?? 15;
5353
$params['top'] = $params['top'] ?? 30;
5454
$params['bottom'] = $params['bottom'] ?? 30;
55+
$params['margin_header'] = $params['margin_header'] ?? 8;
5556
$params['margin_footer'] = $params['margin_footer'] ?? 8;
5657

5758
$this->params['filename'] = $params['filename'] ?? api_get_local_time();
@@ -77,8 +78,8 @@ public function __construct(
7778
'margin_right' => $params['right'],
7879
'margin_top' => $params['top'],
7980
'margin_bottom' => $params['bottom'],
80-
'margin_header' => 8,
81-
'margin_footer' => 8,
81+
'margin_header' => $params['margin_header'],
82+
'margin_footer' => $params['margin_footer'],
8283
];
8384

8485
// Default value is 96 set in the mpdf library file config.php
@@ -541,11 +542,10 @@ public function content_to_pdf(
541542
/**
542543
* Renders single-page HTML (e.g. a certificate) to a downloadable PDF.
543544
*
544-
* Unlike content_to_pdf()/html_to_pdf_with_template(), this method does NOT
545-
* call format_pdf(): it deliberately skips the book-layout decoration
546-
* (mirrorMargins, header/footer margins) that would otherwise insert blank
547-
* pages around a single-page document. Remote asset fetches are routed
548-
* through SafeMpdfHttpClient to prevent SSRF.
545+
* Reuses the SSRF-guarded mPDF instance built by the constructor, with all
546+
* margins set to 0 and without calling format_pdf(): this deliberately
547+
* skips the book-layout decoration (mirrorMargins, header/footer margins)
548+
* that would otherwise insert blank pages around a single-page document.
549549
*
550550
* @param string $html HTML content to render
551551
* @param string $fileName Output file name (without extension)
@@ -559,24 +559,21 @@ public static function singlePageHtmlToPdfDownload(
559559
string $orientation = 'landscape'
560560
): void {
561561
$pageFormat = 'landscape' === $orientation ? 'A4-L' : 'A4';
562+
$mpdfOrientation = 'landscape' === $orientation ? 'L' : 'P';
562563

563-
$mpdf = new Mpdf([
564-
'tempDir' => Container::getCacheDir(),
565-
'mode' => 'utf-8',
566-
'format' => $pageFormat,
567-
'orientation' => $orientation,
568-
'margin_left' => 0,
569-
'margin_right' => 0,
570-
'margin_top' => 0,
571-
'margin_bottom' => 0,
564+
$pdf = new self($pageFormat, $mpdfOrientation, [
565+
'left' => 0,
566+
'right' => 0,
567+
'top' => 0,
568+
'bottom' => 0,
572569
'margin_header' => 0,
573570
'margin_footer' => 0,
574-
], SafeMpdfHttpClient::container());
575-
$mpdf->mirrorMargins = 0;
571+
]);
572+
$pdf->pdf->mirrorMargins = 0;
576573

577-
@$mpdf->WriteHTML($html);
574+
@$pdf->pdf->WriteHTML($html);
578575

579-
$mpdf->Output(api_replace_dangerous_char($fileName).'.pdf', Destination::DOWNLOAD);
576+
$pdf->pdf->Output(api_replace_dangerous_char($fileName).'.pdf', Destination::DOWNLOAD);
580577
exit;
581578
}
582579

0 commit comments

Comments
 (0)