Problems showing a PDF file in the browser

0

I am showing a PDF file that I developed with the FPDF library in PHP and clicking on the button opens a new tab , but the tab remains blank, you only see a photo that I have in the background and the typical image of loading (like a circling circle) in the middle of the page and it is not shown nothing else, but after reloading the page it loads well and shows the information.

Why could this be happening?

EDIT

The button to call to create the PDF

<td>
    <?php
        // PDF
        if($borrador == 1){ //SI ES BORRADOR
    ?>
    <a href="index.php?p=pdf_borrador&id=<?php echo $row['ID_SOL']; // echo $row['id_spom']; ?>" class="btn btn-primary btn-sm" data-title="View" target="_blank"><span class="glyphicon glyphicon-save-file" aria-hidden="true"></span></a>
    <?php
        }else if ($borrador == 0){ //SI ES SOLICITUD
    ?>
    <a href="index.php?p=pdf_solicitud&id=<?php echo  $row['ID_SOL']; // echo $row['id_spom']; ?>" class="btn btn-primary btn-sm" data-title="View" target="_blank"><span class="glyphicon glyphicon-save-file" aria-hidden="true"></span></a>
    <?php
        }
    ?>
</td>

PDF headers (first lines)

<?php
 define('FPDF_FONTPATH','_fpdf/font/');
 require('_fpdf/fpdf.php');
 $pdf=new FPDF('P','mm',array(216,330));
 $pdf->SetAutoPageBreak(true,10);
 $pdf->AddPage();
 $pdf->SetFillColor(194,194,194);
 $pdf->SetLineWidth(0.1);

 $pdf->SetFont('Arial','',7);
 $pdf->Text(10,4,'titulo1');

 $pdf->Text(166,4,'titulo2');

 ob_end_clean();
 $pdf->Output('m_pdf_xxxx.php','i');
?>

I hope you clarify the subject a bit more. Obviously it is a much more extensive PDF but I can not place everything here, I tried this on my localhost and the same thing happens.

Thankful!

    
asked by x_Mario 07.04.2016 в 17:05
source

1 answer

0

Your PDF writing is wrong. It should be like this:

$pdfRecibo = $pdf->Output("",'S');

Option S returns the binary to a variable, so you do not need the route, so send it empty. Then at the end of your php do this:

echo $pdfRecibo;

Finally in javascript with jquery:

function consultarCFDI(noCFDI,campo,ejercicio_registro){
    $.ajax({
       url : "archivo.php",
       type : 'POST',
       dataType : 'text',
       data :  { noCFDI: noCFDI,campo:campo,EJERCICIO_REGISTRO:ejercicio_registro },
       success : function(binario) {
           //alert(binario);
           var nombreLogico = campo;
           var parametros = "dependent=yes,locationbar=no,scrollbars=yes,menubar=yes,resizable,screenX=50,screenY=50,width=850,height=1050";
           var htmlText = "<embed width=100% height=100% type='application/pdf' src='data:application/pdf,"+escape(binario)+"'></embed>"; 
           var detailWindow = window.open ("", nombreLogico, parametros);
           detailWindow.document.write(htmlText);
           detailWindow.document.close();
       },
       error : function(jqXHR, status, error) {
                       alert("error\njqXHR="+jqXHR+"\nstatus="+status+"\nerror="+error);
       },
       complete : function(jqXHR, status) {

       }
    });
}

and check the character encoding when going from php to javascript, that may affect you and you may have to use utf8_encode or utf8_decode

    
answered by 08.04.2016 / 21:35
source