How to generate PDF dynamically based on values from the database in WordPress

Using the FPDF library (found at http://www.fpdf.org) you generate PDFs on the fly using any data from your WP site.

function wpse60844_generate_pdf( $post ) {
    /* Define paths for where the PDF will be permanently or temporarily saved */
    define( 'FPDF_PATH', plugin_dir_path( __DIR__ ) . '/fpdf/' );
    define( 'SAVE_PATH', plugin_dir_path( __DIR__ ) . 'fpdf/pdfs/' );
    $pdf_dir = plugin_dir_path( __DIR__ ) . 'fpdf/pdfs/';
    
    /* If the directory doesn't already exist, generate it */
    if( !file_exists ( $pdf_dir ) ) :
        wp_mkdir_p( $pdf_dir );
    endif;
    
    /* Bring in the FPDF files so this all works */
    require( FPDF_PATH .'fpdf.php' );
    require( FPDF_PATH .'writeHTML.php' );
    
    /* Start retrieving your data from the WP DB, whether it's post_meta or some other data, no difference, just get your data into variables */
    $yourdata                       = get_post_meta( $post->ID, 'meta_key', true );
    $moredata                       = get_post_meta( $post->ID, 'meta_key', true );
    
    /* Now you begin to generate the HTML that will be converted to a PDF
     * Careful as some standard HTML won't work, like `<strong>` or `<em>`, use `<b>` or `<i>` */
    $html="<b>Some Label: " . $yourdata . '</b><br><br>';
    $html   .= '<b>Some other Label: ' . $moredata . ' </b><br>';
    /* Keep going until you have all the content and layout you need */
    
    /* generating the PDF itself */
    $pdf = new hFPDF( 'P', 'mm', 'A4' );
    $pdf->AddPage();
    $pdf->SetFont( 'Arial', '', 12 );
    
    /* this is where the actual HTML you built above gets added as PDF. */
    $pdf->WriteHTML( utf8_decode( $html ) );
    $pdf->Output( SAVE_PATH . 'whatever-your-filename-is.pdf', 'F' );
    global $pdf_filename;
    $pdf_filename = SAVE_PATH . 'whatever-your-filename-is.pdf';

    /* Next you either have to write a function to mail this to your self using `wp_mail()`
    * Or send over the download path via AJAX and then download the PDF
    * To keep things clean you should also probably delet the PDF after it has been downloaded/sent
    * The rest is really up to you. */
}
add_action( 'the_action_that_triggers_pdf_send_or_download', 'wpse60844_generate_pdf' );

There’s a lot of detail work involved and the odd character here or there can break the whole process. So be careful and I would honestly try to first output a single line PDF and then line by line add in the extra data, as that’ll make it easier to help figure out where an error popped up.