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

Asked

Viewed 2,025 times

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 on 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:

  • The parameters are passed to the PHP that generates the report via POST.
  • The file is being accessed exclusively via HTTPS with valid certificate.
  • The web server is enabled to log all errors and errors are being properly logged in the log, but no errors related to this PHP are being logged. (i.e., I clear the error log, run the report and no errors appear in the log... forcing an error purposely and the same is recorded). This way, it seems that there is no syntax error.
  • The Content-type used is: header('Content-type: application/pdf');
  • The problem occurs on Windws and Google Chrome dom computers (tested on multiple machines).
  • Using native Google Chrome plugin (Chrome://plugins/ -> Chrome PDF Viewer). If I disable it, it keeps downloading normal file. If active, it views everything normal, prints, but does not work the SAVE option.

All buttons of the browser PDF display plug-in (save, print, rotate, zoom, etc.) work normally. Except the save button in Google Chrome (other browsers works normal).

While trying to save the PDF open and already displayed in Google Chrome, the following error occurs:

Failure - Network error

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

Would anyone have any idea or suggestion how to resolve this error or what might be causing the same?

  • Could you post a basic example of the code? I wasn’t the one who denied it, but I believe that the downvote giver had this reason.

  • It makes no sense Output('I', '/var/tmp/relatorio.pdf');, the I serves to put the buffer in the output directly if you want to save to a file use F, if you want to download use D and if you want to get the buffer in a string use S

  • But that too is wrong $this->Output('doc.pdf', 'I');, you changed the order why? See the examples in the reply (I edited).

  • is thus in the FPDF class: Function Output($name = ', $dest = '') {

  • Allan which version you downloaded?

  • Allan which plugin is using on Chrome, is his native (PDF Viewer) or is Acrobatreader or Foxitreader?

  • His native: Chrome://plugins/ -> Chrome PDF Viewer (if I disable it, keeps downloading the file normally.) If active, it views everything normal, prints, but does not work the SAVE option.

  • Blz, I’m gonna run the tests.

Show 3 more comments

1 answer

1

I believe the problem is with the cache

  • You can do prevent caching, add this to the top:

    The I in the method Output 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 caching, but if it doesn’t work yet you can try it like this:

    <?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 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 method Output has 4 options:

  • I:

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

  • F:

    Saves the file to a folder on the server

  • D:

    Force the buffer download

  • S:

    Returns the buffer as a string

  • If I redirect, how do I control access permission?

  • @Allanandrade just add permissions to the folder data 755 (note that it is an example folder), if permission you mean access level by authentication you can create a token and put as the file name.

  • @Allanandrade updated the code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.