0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
<?php
namespace WPTurboEbookGenerator;
use WP_Turbo;
require_once 'vendor/autoload.php'; // Composer autoload for PDF generation library
function wpturbo_generate_ebook( $post_ids ) {
// Check if the post IDs are valid
if ( ! is_array( $post_ids ) || empty( $post_ids ) ) {
return "No valid post IDs provided.";
}
// Create a new PDF document
$pdf = new TCPDF(); // Assuming you are using TCPDF library
// Set document information
$pdf->SetCreator( PDF_CREATOR );
$pdf->SetAuthor( "WP Turbo" );
$pdf->SetTitle( "Generated E-Book" );
$pdf->SetSubject( "E-Book from WordPress Posts" );
// Add a page
$pdf->AddPage();
// Add content from each post
foreach ( $post_ids as $post_id ) {
$post = get_post( $post_id );
if ( null === $post ) {
continue; // Skip invalid posts
}
$pdf->Write( 0, $post->post_title, '', 0, 'C', true, 0, false, false, 0 );
$pdf->Write( 0, $post->post_content, '', 0, '', true, 0, false, false, 0 );
$pdf->Ln(); // New line for spacing
}
// Output PDF to browser
$pdf->Output( "ebook.pdf", "D" ); // Force download
}
add_action( 'wp_ajax_wpturbo_generate_ebook', function() {
$post_ids = isset( $_POST['post_ids'] ) ? $_POST['post_ids'] : [];
wpturbo_generate_ebook( $post_ids );
wp_die();
} );
Make sure you have the TCPDF library installed using Composer or include it in your project to handle PDF generation. You can trigger the AJAX request from your WordPress admin area or front-end as needed.