Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Not quite sure if anyone here has used dompdf, but I'm in need of assistance. There's a plugin (post-pdf-export) that exports posts to PDF using dompdf. All I need to do is generate some html (<html><body><p>PUT INVOICE HERE</p></body></html>) and export that to a PDF object and attach it to an email. I have got all of the code "executing" but the PDF that is generated will not open in Adobe Reader, but the pdf generated by the post-pdf-export plugin will. Not sure where I'm going wrong, but here's the basics (and please, someone tell me what I'm missing):

1) Include the dompdf class (in functions.php):

    define('SSM_PLUGIN_PATH' , plugin_dir_path(  __FILE__  ) );
    include(SSM_PLUGIN_PATH.'/dompdf/dompdf_config.inc.php' );

2) Add admin sub-menu item and call my function:

    add_submenu_page( 
        'ssm_main_menu', // Parent Slug
        'Test PDF', // Page Title
        'Test PDF', // Menu Title
        'manage_options', // Capability
        'test-pdf', // Menu Slug
        'test_pdf_handler' // Function
    );

3) Function exports html to pdf:

function test_pdf_handler()
{
    $stuff = '<html>
                <body>
                <p>
                    PLEASE WORK!!!
                </p>
            </body></html>';
    set_time_limit(300);
    ini_set('memory_limit', '-1');

    $dompdf = new DOMPDF();
    $dompdf->load_html($stuff);
    $dompdf->set_paper( 'letter' , 'portrait' );
    $dompdf->render();
    $dompdf->stream('sample.pdf');

    exit();
}

4) Result - PDF that will not open...

HELP?

UPDATE: To add to the confusion, the post-pdf-plugin dompdf directory loads perfectly, but when I go get the dompdf package directly from git and include it in my plugin, it fails to load. If someone doesn't know what's going on, can someone recommend another solution for generating PDFs in PHP in a WordPress plugin?

UPDATE 2: After creating a blank plugin to test this (to see what I have wrong in my plugin), I did notice one difference. My plugin pushes content to the front-end using shortcodes so I used to get the error message Warning: Cannot modify header information - headers already sent by (output started at H:\.... So, to get around this, I added the following to my plugin to clear that error, but I believe it is causing the creation of the PDF to fail:

add_action('init', 'do_output_buffer');

function do_output_buffer() {
    ob_start();
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Seems as though I was attempting to perform this PDF creation too late in the lifecycle of the page. After adding this snippet to an action binding to init, it works perfectly.

share|improve this answer
    
I'm actually trying to do what you're doing–create a PDF from a dom element and email it. It would be a huge help if you shared the rest of your code. Thanks! –  cfx Nov 10 '13 at 23:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.