0

I encountered a problem with FPDF in wordpress. Actually, I am developing a wordpress plugin where need to generate pdf. As a php application its working fine. But when I converted it to wordpress plugin it's returning an error

"FPDF error: Some data has already been output, can't send PDF file (output started at D:\xampp\htdocs\wordpress\wp-includes\class.wp-styles.php:225"

I tried to solve it in a few ways. But no luck.

I am using the latest version of FPDF and Wordpress. Here my code. Thanks in advance. Any solution will highly appreciated.

<?php

/*  Download pdf */

function jacgp_card() { 
    if( isset($_POST['download']) ){

        //call the FPDF library
        require('fpdf.php');

        //A4 width : 219mm
        //default margin : 10mm each side
        //writable horizontal : 219-(10*2)=189mm

        //create pdf object
        $pdf = new FPDF('P','mm','A4');
        //add new page
        $pdf->AddPage();


        // Header
        //set font to arial, bold, 14pt
        $pdf->SetFont('Arial','',12);
        //$pdf->Cell(45, 40, $pdf->Image('bg.jpg', $pdf->GetX(), $pdf->GetY(), 35.50), 1, 0, 'L', false );
        $pdf->Cell(189 ,20,' I am in FPDF',1,1,'C');//end of line

        //output the result
        $pdf->Output();
    }


    /* Shortcode start */
    ob_start();
?>

 <form id="admitDform" method="POST" action="">
  <fieldset>
    <legend>Download Card</legend>
        <div class="row">
            <div class="col-md-12">
                <input type="submit" value="Download" name="download" class="download" id="download">
            </div>
        </div>
  </fieldset>
</form> 
<?php 
    $jacgp_sr_v = ob_get_clean();
    return $jacgp_sr_v;
}

add_shortcode('jacgp_acs','jacgp_card');
  • The shortcode is rendered after other content of the page. That's what the error message is telling you. :) Send the PDF prior to the output, for example on template_redirect. – fuxia Jan 1 '18 at 6:18
  • Sorry, I didn't get you. Can share me little bit of code. Actually, I am newbie . Thank you. – S.k.joy Jan 1 '18 at 6:59
  • A shortcode is done to output HTML then you cannot output PDF because HTML headers are already send then you cannot send PDF headers. To output PDF try to use the AJAX system of WordPress (codex.wordpress.org/AJAX_in_Plugins) then you can download the PDF with a link like example.com/wp-admin/wp-ajax.php?action=PluginName_download – mmm Jan 1 '18 at 8:25
  • In other words you use the shortcode to output the download button as you have, but you need to use another method such as AJAX to output the PDF itself. (The reason for this is the download needs to sends a HTTP header identifying itself as PDF file, but can't do so since HTML output and header has already sent by the time the shortcode is run.) – majick Jan 2 '18 at 0:43
  • I've solved my problem using ajax. Thanks all for hints, specially mmm – S.k.joy Jan 2 '18 at 3:25

Your Answer

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

Browse other questions tagged or ask your own question.