"network failure" when downloading PDF generated with PHP FPDF class in Google Chrome

0

I have reports with generated in PHP (version 5.6) / FPDF (version 1.7) that are normally displayed in the browser window.

The following simplified example about these reports are generated:

<?php
 // não fazer cache (Recomendação de Guilherme Nascimento)
 $dategmt = gmdate('D, d M Y H:i:s');
 header('Expires: ' . $dategmt . ' GMT');
 header('Last-Modified: ' . $dategmt . ' GMT');
 header('Cache-Control: no-store, no-cache, must-revalidate');
 header('Cache-Control: post-check=0, pre-check=0');
 header('Pragma: no-cache');


// controla se o usuário pode ter acesso ao relatório
require 'controle-acesso.php';
if(!userTemAcesso()){
   echo 'Você não tem permissões para acessar este recurso';
   exit;
}

require 'fpdf.php';

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(40, 10, 'Olá mundo!');

// saída para arquivo não acessível diretamente via URL
$this->Output('doc.pdf', 'I');

Note:

  • Parameters are passed to PHP that generates the report via POST .
  • The file is being accessed exclusively via HTTPS with a valid certificate.
  • The web server is enabled to log all errors and errors are being properly registered in the log, however no errors related to this PHP are being registered. (ie I clean the error log, run the report and no error appears in the log ... forcing an error purposely and it is logged). This way it looks like there is no syntax error.
  • The% used% is: Content-type
  • The problem occurs on Sun Windws and Google Chrome computers (tested on multiple machines).
  • Using native Chrome plugin (chrome: // plugins / - > Chrome PDF Viewer). If I disable it, it will download the file normally. If enabled, it displays everything normal, prints, but the SAVE option does not work.

All browsers PDF view plug-in buttons (save, print, rotate, zoom, etc.) work normally. Save the save button in Google Chrome (other browsers work normal).

When you try to save the open PDF already displayed in Google Chrome, the following error occurs:

  

Failed - Network error

Therefore, it is not possible to save the PDF, unless you go to print and print to PDF, that is, print the PDF in PDF, which does not make much sense.

Would anyone have any ideas or suggestions how to resolve this error or what could be causing the same?

    
asked by anonymous 19.08.2016 / 22:00

1 answer

1

I think the problem is with the cache

  • You can prevent cache , add this to the top:

      The I in the Output method already adds the header application/pdf and sends the buffer directly to the buffer

    <?php
    
    // controla se o usuário pode ter acesso ao relatório
    require 'controle-acesso.php';
    
    if (!userTemAcesso()) {
       echo 'Você não tem permissões para acessar este recurso';
       exit;
    }
    
    require 'fpdf.php';
    
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 16);
    $pdf->Cell(40, 10, 'Olá mundo!');
    $pdf->Output('I', 'meuarquivo.pdf'); //versão 1.8
    //$pdf->Output('meuarquivo.pdf', 'I'); //versão 1.7
    
  • The I already prevents the cache, but if it still does not work you can try:

    <?php
    
    $dategmt = gmdate('D, d M Y H:i:s');
    
    header('Expires: ' . $dategmt . ' GMT');
    header('Last-Modified: ' . $dategmt . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Cache-Control: post-check=0, pre-check=0');
    header('Pragma: no-cache');
    
    // controla se o usuário pode ter acesso ao relatório
    require 'controle-acesso.php';
    
    if (!userTemAcesso()) {
       echo 'Você não tem permissões para acessar este recurso';
       exit;
    }
    
    require 'fpdf.php';
    
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 16);
    $pdf->Cell(40, 10, 'Olá mundo!');
    $pdf->Output('I', 'meuarquivo.pdf'); //versão 1.8
    //$pdf->Output('meuarquivo.pdf', 'I'); //versão 1.7
    
  • You can save to the server and redirect:

    <?php
    
    // controla se o usuário pode ter acesso ao relatório
    require 'controle-acesso.php';
    
    if(!userTemAcesso()){
       echo 'Você não tem permissões para acessar este recurso';
       exit;
    }
    
    require 'fpdf.php';
    
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 16);
    $pdf->Cell(40, 10, 'Olá mundo!');
    $pdf->Output('F', '/home/user/www/data/meuarquivo.pdf'); //versão 1.8
    //$pdf->Output('/home/user/www/data/meuarquivo.pdf', 'F'); //versão 1.7
    
    //Redireciona
    header('Location: data/meuarquivo.pdf');
    

Parameters:

The Output method has 4 options:

  • I :

    Displays the buffer directly in the response (eg in the browser) as if it were a PDF using Content-Type: application/pdf

  • F :

    Save the file to a folder on the server

  • D :

    Force buffer download

  • S :

    Returns the buffer as a string

26.08.2016 / 06:38