Error generating PDF in Cakephp through FPDF

Asked

Viewed 571 times

0

I’m rewriting a legacy system of the company I work on (using Cakephp for this) and it generates some PDF reports using FPDF. However, in Cakephp I cannot generate any PDF.

Follows the code

Action

function pdf ($id = null) {
    App::import('Vendor', 'Fpdf', array('file' => 'fpdf/fpdf.php'));
    $this->layout = 'pdf';         
    $this->set('fpdf', new FPDF('P','mm','A4'));

    $mensagem = is_null($id) ? "Nenhum ID fornecido" : "ID: $id";
    $this->set('data', $mensagem);

    $this->response->type('pdf'); 
    $this->render('pdf');
}

View

$fpdf->AddPage(); 
$fpdf->SetFont('Arial','B',16); 
$fpdf->Cell(40,10, $data); 
$fpdf->Output();

No error appears, only the Chrome PDF plugin and is written "loading" but never loads.

There is no problem on the server because, in the same test server, has a mirror of the old system working normally.

  • 1

    What is the error generated?

  • None, the pdf plugin of Chrome appears and is just loading, if I give Ctrl+s comes an empty document. Thanks for the touch

  • Can it take time to load and/or build the PDF? There is a way for you to debug to see how the execution occurs?

  • 1

    You’re calling $fpdf->Output() in your view?

  • I would like to see the View code too so I can try to reproduce the problem

  • @Mutley is not slow, it stays like this forever. I also tried to use debug but nothing came out.

  • @Andréribeiro I am

  • @Erloncharles $fpdf->AddPage(); $fpdf->SetFont('Arial','B',16); $fpdf->Cell(40,10, $data); $fpdf->Output();

  • 1

    A pdf document is returned valid but empty or an empty file?

  • @Andréribeiro comes an empty file.

  • 1

    @rafaslide try to add a error_reporting(E_ALL) at the beginning of its action just to see if the FPDF generates some error that is not being shown.

Show 6 more comments

2 answers

1

Is it possible to migrate to Htmltopdf? If so, migrate. I also had several problems with FPDF that I could only solve with migration.

In addition, open the Chrome development tools (CTRL+J) and in the Network (or Network) tab, check what is being loaded when trying to generate the pdf.

0


The error was in the Layout (pdf) that I was using, in the end it was all right with the action and the view, what was missing was a part of the code. I needed to echo the content through the variable $content_for_layout; and I had not committed myself to this.

Thank you all for your support.

Browser other questions tagged

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