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:

        '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>
                    PLEASE WORK!!!
    ini_set('memory_limit', '-1');

    $dompdf = new DOMPDF();
    $dompdf->set_paper( 'letter' , 'portrait' );


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


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() {

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.

| 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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