feat: add PDF generation and download feature

- Introduced dependency management with Composer for easier package handling.
- Added TCPDF library to generate PDFs of articles.
- Implemented PDF download functionality via a custom endpoint.
- Included admin notices to ensure Composer dependencies are installed.
- Updated single article view with a download button for PDFs.
This commit is contained in:
Kumi 2024-08-02 21:48:07 +02:00
parent 72ef4d8c0c
commit d901010399
Signed by: kumi
GPG key ID: ECBCC9082395383F
5 changed files with 212 additions and 9 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
composer.phar
vendor/

5
composer.json Normal file
View file

@ -0,0 +1,5 @@
{
"require": {
"tecnickcom/tcpdf": "^6.7"
}
}

91
composer.lock generated Normal file
View file

@ -0,0 +1,91 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "a607f866b3f01ae9ad4f413456987bbb",
"packages": [
{
"name": "tecnickcom/tcpdf",
"version": "6.7.5",
"source": {
"type": "git",
"url": "https://github.com/tecnickcom/TCPDF.git",
"reference": "951eabf0338ec2522bd0d5d9c79b08a3a3d36b36"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/tecnickcom/TCPDF/zipball/951eabf0338ec2522bd0d5d9c79b08a3a3d36b36",
"reference": "951eabf0338ec2522bd0d5d9c79b08a3a3d36b36",
"shasum": ""
},
"require": {
"php": ">=5.5.0"
},
"type": "library",
"autoload": {
"classmap": [
"config",
"include",
"tcpdf.php",
"tcpdf_parser.php",
"tcpdf_import.php",
"tcpdf_barcodes_1d.php",
"tcpdf_barcodes_2d.php",
"include/tcpdf_colors.php",
"include/tcpdf_filters.php",
"include/tcpdf_font_data.php",
"include/tcpdf_fonts.php",
"include/tcpdf_images.php",
"include/tcpdf_static.php",
"include/barcodes/datamatrix.php",
"include/barcodes/pdf417.php",
"include/barcodes/qrcode.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-3.0-or-later"
],
"authors": [
{
"name": "Nicola Asuni",
"email": "info@tecnick.com",
"role": "lead"
}
],
"description": "TCPDF is a PHP class for generating PDF documents and barcodes.",
"homepage": "http://www.tcpdf.org/",
"keywords": [
"PDFD32000-2008",
"TCPDF",
"barcodes",
"datamatrix",
"pdf",
"pdf417",
"qrcode"
],
"support": {
"issues": "https://github.com/tecnickcom/TCPDF/issues",
"source": "https://github.com/tecnickcom/TCPDF/tree/6.7.5"
},
"funding": [
{
"url": "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&currency_code=GBP&business=paypal@tecnick.com&item_name=donation%20for%20tcpdf%20project",
"type": "custom"
}
],
"time": "2024-04-20T17:25:10+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.6.0"
}

View file

@ -721,3 +721,105 @@ function custom_login_logo_url_title()
} }
add_filter('login_headertext', 'custom_login_logo_url_title'); add_filter('login_headertext', 'custom_login_logo_url_title');
// Check if TCPDF is available
function is_tcpdf_available()
{
return class_exists('TCPDF');
}
// Check if the Composer autoloader is available
function is_autoloader_available()
{
return file_exists(get_template_directory() . '/vendor/autoload.php');
}
// Display admin notice if TCPDF or autoloader is missing
function tcpdf_missing_notice()
{
if (!is_autoloader_available()) {
echo '<div class="notice notice-error"><p>';
_e('Composer autoloader is missing. Please run "composer install" to install the required dependencies.', 'duck-behavior-journal');
echo '</p></div>';
} elseif (!is_tcpdf_available()) {
echo '<div class="notice notice-error"><p>';
_e('TCPDF library is missing. Please install it via Composer to enable the PDF download feature.', 'duck-behavior-journal');
echo '</p></div>';
}
}
add_action('admin_notices', 'tcpdf_missing_notice');
// Add custom endpoint for PDF download
function add_pdf_download_endpoint()
{
add_rewrite_rule('^download-pdf/([0-9]+)/?', 'index.php?download_pdf=$matches[1]', 'top');
}
add_action('init', 'add_pdf_download_endpoint');
// Add query vars for PDF download
function add_pdf_query_vars($vars)
{
$vars[] = 'download_pdf';
return $vars;
}
add_filter('query_vars', 'add_pdf_query_vars');
// Handle PDF download
function handle_pdf_download()
{
if (is_autoloader_available()) {
require_once get_template_directory() . '/vendor/autoload.php';
}
if (is_tcpdf_available()) {
$post_id = get_query_var('download_pdf');
if ($post_id && is_numeric($post_id)) {
$post = get_post($post_id);
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);
// Set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor(get_bloginfo('name'));
$pdf->SetTitle($post->post_title);
$pdf->SetSubject('Scientific Paper');
// Add a page
$pdf->AddPage();
// Set title
$pdf->SetFont('helvetica', 'B', 20);
$pdf->Cell(0, 10, $post->post_title, 0, 1, 'C');
// Set authors
$pdf->SetFont('helvetica', '', 12);
$authors = get_post_meta($post_id, 'article_authors', true);
if (!empty($authors)) {
$pdf->Cell(0, 10, implode(', ', array_map('esc_html', $authors)), 0, 1, 'C');
}
// Set abstract
$pdf->Ln(10);
$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 10, 'Abstract', 0, 1);
$pdf->SetFont('helvetica', '', 12);
$pdf->MultiCell(0, 10, $post->post_excerpt);
// Set content
$pdf->Ln(10);
$pdf->SetFont('helvetica', 'B', 16);
$pdf->Cell(0, 10, 'Content', 0, 1);
$pdf->SetFont('helvetica', '', 12);
$pdf->writeHTML($post->post_content);
// Output PDF document
$pdf->Output(sanitize_title($post->post_title) . '.pdf', 'D');
exit;
}
}
}
}
add_action('template_redirect', 'handle_pdf_download');

View file

@ -24,15 +24,15 @@
</p> </p>
<p class="text-muted"> <p class="text-muted">
<strong>Status:</strong> <?php <strong>Status:</strong> <?php
$terms = get_the_terms(get_the_ID(), 'publication_status'); $terms = get_the_terms(get_the_ID(), 'publication_status');
if (!empty($terms) && !is_wp_error($terms)) { if (!empty($terms) && !is_wp_error($terms)) {
$term_list = array(); $term_list = array();
foreach ($terms as $term) { foreach ($terms as $term) {
$term_list[] = $term->name; $term_list[] = $term->name;
} }
echo implode(', ', $term_list); echo implode(', ', $term_list);
} }
?> ?>
</p> </p>
<?php if (has_excerpt()) : ?> <?php if (has_excerpt()) : ?>
<p class="text-muted"><strong>Abstract:</strong> <?php echo get_the_excerpt(); ?></p> <p class="text-muted"><strong>Abstract:</strong> <?php echo get_the_excerpt(); ?></p>
@ -49,6 +49,9 @@
<p class="text-muted"><strong>Note:</strong> <?php _e('The full paper is available for download for subscribers only.', 'duck-behavior-journal'); ?></p> <p class="text-muted"><strong>Note:</strong> <?php _e('The full paper is available for download for subscribers only.', 'duck-behavior-journal'); ?></p>
<?php endif; <?php endif;
endif; ?> endif; ?>
<?php if (is_tcpdf_available()) : ?>
<p><a href="<?php echo esc_url(home_url('/download-pdf/' . get_the_ID())); ?>" class="btn btn-primary"><?php _e('Download PDF', 'duck-behavior-journal'); ?></a></p>
<?php endif; ?>
<hr> <hr>
<?php the_content(); ?> <?php the_content(); ?>
<?php endwhile; <?php endwhile;