feat: customize PDF download header/footer

Introduced a new PaperPDF class extending TCPDF to add custom headers and footers, including the site's logo and name in the header and page numbers in the footer. Updated the PDF creation process in functions.php to utilize this new class, enhancing the branding of downloaded PDFs.
This commit is contained in:
Kumi 2024-08-02 22:05:49 +02:00
parent 4dbf366b1c
commit 77949dd0c1
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 27 additions and 1 deletions

24
PaperPDF.php Normal file
View file

@ -0,0 +1,24 @@
<?php
require_once get_template_directory() . '/vendor/autoload.php';
class PaperPDF extends TCPDF {
// Page header
public function Header() {
$logo_url = wp_get_attachment_image_url(get_theme_mod('custom_logo'), 'full');
if ($logo_url) {
$this->Image($logo_url, 10, 10, 50, '', '', '', 'T', false, 300, '', false, false, 0, false, false, false);
}
$this->SetFont('helvetica', 'B', 20);
$this->Cell(0, 15, get_bloginfo('name'), 0, false, 'C', 0, '', 0, false, 'T', 'M');
$this->Ln(20);
}
// Page footer
public function Footer() {
$this->SetY(-15);
$this->SetFont('helvetica', 'I', 8);
$this->Cell(0, 10, 'Page ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}

View file

@ -775,6 +775,8 @@ function handle_pdf_download()
}
if (is_tcpdf_available()) {
require_once get_template_directory() . '/PaperPDF.php';
$post_id = get_query_var('download_pdf');
if ($post_id && is_numeric($post_id)) {
@ -782,7 +784,7 @@ function handle_pdf_download()
if ($post && $post->post_type == 'article') {
// Create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf = new PaperPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// Set document information
$pdf->SetCreator(PDF_CREATOR);