Skip to content

Instantly share code, notes, and snippets.

@Web-Assembler
Last active August 7, 2023 13:28
Show Gist options
  • Save Web-Assembler/52da3fb37134cfd12bad17ae56d602f0 to your computer and use it in GitHub Desktop.
Save Web-Assembler/52da3fb37134cfd12bad17ae56d602f0 to your computer and use it in GitHub Desktop.
Plugin for creating PDFs with FPDF
<?php
/*
* Plugin Name: Web Assembler - FPDF Tutorial
* Description: A plugin created to demonstrate how to build a PDF document from WordPress posts.
* Version: 0.1
* Author: Web Assembler
* Author URI: https://www.webassembler.co.uk/
*/
/**
* Note to developers- This is an untested, incomplete plugin that was written to accompany a blog post in 2018.
* Do not use in production, test in a local environment first.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists('PDF_HTML') ) {
return;
}
/**
* 1. Create a folder for your plugin
* 2. Inside this folder add this file as the main plugin file.
* 3. Create a file called `webassembler-pdf-helper-functions.php` in the folder
* 4. Download FPDF here: http://www.fpdf.org/ and store in the folder root
* 5. In the plugin webassembler-pdf-helper-functions.php file, include the FDPF library at the top using instructions on FPDF website
* 6. Copy all of the functions here: http://www.fpdf.org/en/script/script42.php and paste them in the same file
* 7. Uncomment the line below to include the include
*/
// include( 'webassembler-pdf-helper-functions.php');
$pdf = new PDF_HTML();
if ( isset($_POST['generate_posts_pdf'])){
webassembler_output_pdf();
}
add_action( 'admin_menu', 'webassembler_create_admin_menu' );
function webassembler_create_admin_menu() {
$hook = add_submenu_page(
'tools.php',
'Web Assembler PDF Generator',
'Web Assembler PDF Generator',
'manage_options',
'wa-fdpf-tutorial',
'webassembler_fpdf_create_admin_page'
);
}
function webassembler_output_pdf() {
$posts = get_posts( 'posts_per_page=5' );
if ( ! empty( $posts ) ) {
global $pdf;
$title_line_height = 10;
$content_line_height = 8;
$pdf->AddPage();
$pdf->SetFont( 'Arial', '', 42 );
$pdf->Write(20, 'Web Assembler FPDF Tutorial');
foreach ( $posts as $post ) {
$pdf->AddPage();
$pdf->SetFont( 'Arial', '', 22 );
$pdf->Write($title_line_height, $post->post_title);
// Add a line break
$pdf->Ln(15);
// Image
$page_width = $pdf->GetPageWidth() - 20;
$max_image_width = $page_width;
$image = get_the_post_thumbnail_url( $post->ID );
if ( ! empty( $image ) ) {
$pdf->Image( $image, null, null, 100 );
}
// Post Content
$pdf->Ln(10);
$pdf->SetFont( 'Arial', '', 12 );
$pdf->WriteHTML( $post->post_content );
}
}
$pdf->Output( 'D','webassembler_fpdf_tutorial.pdf' );
exit;
}
function webassembler_fpdf_create_admin_page() {
?>
<div class="wrap">
<h1>Web Assembler - Wordpress PDF Generator</h1>
<p>Click below to generate a pdf from the content inside all the Wordpress Posts. </p>
<p>Each post will be on its own pdf page containing the post title and post content.</p>
<form method="post" id="webassembler-fdpf-form">
<button class="button button-primary" type="submit" name="generate_posts_pdf" value="generate">Generate PDF from Wordpress Posts</button>
</form>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment