PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

[FIXED] Saving PDF file as "data" - using PHP MPDF

 March 05, 2022     codeigniter, mpdf, php     No comments   

Issue

IM trying to save a PDF file, generated from HTML, into the user's local using MPDF. Here is the part of the code responsable for this:

    $mpdf = new \Mpdf\Mpdf();
    $html = $this->load->view('gestao/relatorios/relatorio_cargo_cidades', $data, true);
    $mpdf->WriteHTML($html);
    $mpdf->SetFooter(relatorio_footer());
    $mpdf->Output('relatorio_cargos_cidades_seletivo_' . $seletivo_id .'_'.date("Ymd_his").'.pdf', 'D');

At first I was trying to show the PDF using the "I" param from the output function, than the user could just see the PDF and choose to download it or not. But when I tried to submit a file into another website, it says that the file is not a PDF. Than I used my linux to see if the file was actually a PDF, here's what I got:

enter image description here

As you can see, the file is being saved as "data" for some reason. I've already tried to use the 'F' param, also from the output function, and than it worked, I saved as PDF. But the F param only save the file inside the code folder, so it was not very usefull for me.

Can anyone tell me how could I save the file as an actually PDF using MPDF?

EDIT

I think that the problem is being caused by CODEIGNITER, not by the MPDF. When I'm setting the last param from the load->view as TRUE, the returned HTML is in a data (string) form, and MPDF is not converting this properly.


Solution

Adding ob_clean() might solve your problem:

 ob_clean();
 $mpdf->Output('relatorio_cargos_cidades_seletivo_' . $seletivo_id .'_'.date("Ymd_his").'.pdf', 'D');


Answered By - NcXNaV
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing