0
votes

I tried to convert a html page to pdf and was decided to use mPDF, I follow what the documentation does. When running the code, it does not prompt out the PDF to ask for save. Btw I get those error code.

Here is the code from Controller.

    //this data will be passed on to the view
    $data['the_content']='mPDF and CodeIgniter are cool!';

    //load the view, pass the variable and do not show it but "save" the output into $html variable
    $html=$this->load->view('ajax/pdf_output', $data, true); 

    //this the the PDF filename that user will get to download
    $pdfFilePath = "the_pdf_output.pdf";

    //load mPDF library
    $this->load->library('m_pdf');
    //actually, you can pass mPDF parameter on this load() function
    $pdf = $this->m_pdf->load();
    //generate the PDF!
    $pdf->WriteHTML($html);
    //offer it to user via browser download! (The PDF won't be saved on your server HDD)
    $pdf->Output($pdfFilePath, "I");

Below is the result i get: %PDF-1.4 %���� 3 0 obj <> /Contents 4 0 R>> endobj 4 0 obj <> stream x��P]O�@���㓚����^�1�h�7�C��B(h���sW�Fs����vvv�B')�ձCgha�6��Mp�6� �H�U[P��{��-[�uz��#��뮉�r�@Υ�9�R���'�J�h&���e� �J�YW�f����\���/�m�Ӷ�����J.w���j��N�ގ��^�=f!��ƲO����o�92yh�m���9� �e��[��#�3���?u�R%_�¿�)�X|jt2H׆��+��S��™9%�R��:��ƒ7��m��Z����9n� endstream endobj 1 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj 7 0 obj <> endobj 8 0 obj <> stream /CIDInit /ProcSet findresource begin 12 dict begin begincmap /CIDSystemInfo <> def /CMapName /Adobe-Identity-UCS def /CMapType 2 def 1 begincodespacerange <0000> endcodespacerange 1 beginbfrange <0000> <0000> endbfrange endcmap CMapName currentdict /CMap defineresource pop end end endstream endobj 9 0 obj <> endobj 10 0 obj < >> /FontFile2 12 0 R >> endobj 11 0 obj <> stream x����V�����Qfd%{dT*�l-�Ȉ��_����s��ڥ����}��@;���t���:��Nt�S�n�3�m�s��B��h��JW���nt�[��Nw�^�{��&�l�GM7�l�{���z��^4��^��7��]���B�-��J���V[�K�}�[}�G?��f���V���n���w�� endstream endobj 12 0 obj <> stream x��| \TU��9��;�.�zqIe�]ce�t��apfQQ�q�qGKSS+5-K+Ӟ�z�6�35���gi���wν3d=��}����g�s�=������\0�B��4#+76�r����=\��}�O��:�p <7�r���G~BHH���EŖ�j��B�C_U��W /� q<��U�}��<�!��t�Qo��ۀPdwXN���<���X���<�~]��Po3oC(��_��gW��Ўz��r��8�XR!<����+�vG�B4�Q��� ����Z�<���(��^�Dhk��@��r'WQ����h4�F��i�Fw���H$�]�3�$77kBi(��e��A�=7�"凨����C���]D��.=�onf�577���l��$��y#����? ��CA(�G!(���#��:�Ѓ��uE݀�p�"Qw�z�����F}P4��bP,����8�EP<���h����h�yQJF)H�R�(4��1h,JG(e�l4��\��ƣ|4MD�@�S�T4 =���H:�Ev$���-�|�tdF3�r��w�\�����>�����~�N��0O

4

4 Answers

0
votes

it looks like the "I" parameter is causing trouble because the browser doesn't recognize your file

according to the docu you've the following Possibilities:

I: send the file inline to the browser. The plug-in is used if available. The name given by $filename is used when one selects the “Save as” option on the link generating the PDF.

D: send to the browser and force a file download with the name given by $filename.

F: save to a local file with the name given by $filename (may include a path).

S: return the document as a string. $filename is ignored.

try something like that :

$pdf->Output($pdfFilePath, "D");
die;

or on the other hand you can try to add some header to tell the browser explicitly this is a pdf document

header('Content-Type: application/pdf'); 
$pdf->Output($pdfFilePath, "I");
die;

because it could be CIs outpout class overwrites MPDF's header (but this is just a hunch)

0
votes
    $html=$this->load->view("ajax/pdf_output",$data,true);
    //load mPDF library
    $this->load->library('m_pdf');
    //generate the PDF from the given html
    $this->m_pdf->pdf->WriteHTML($html);
    //download it.
    ob_clean(); 
    $this->m_pdf->pdf->Output($pdfFilePath,'F');

 check your folder.....
0
votes

If you want show download dialogue your need to place below code

$filename = time()."_order.pdf"; //your file name 
$html = $this->load->view('unpaid_voucher2',$data,true);
 /// $data variable is your dynamic data if you have no dynmic data then you can pass empty instead of variable like.
$html = $this->load->view('unpaid_voucher2','',true);
$this->load->library('M_pdf');
$this->m_pdf->pdf->WriteHTML($html);

 //For download pass D and save on server pass F.
 $this->m_pdf->pdf->Output("./uploads/".$filename, "D"); 

Here is full configuration to integrate mpdf into codeigniter

0
votes

The string is a binary PDF representation and its presence means Content-type: application/pdf header is not sent correctly or it is overriden by your code or setup. Most likely by text/plain or text/html.

Try to figure out these:

  • Are you resetting Content-type header in PHP code somewhere after calling the mPDF Output method?
  • Is your server forcing a different Content-type somewhere in your setup?
  • Does your browser support displaying application/pdf Content-type directly?

Given that the D Output mode gives you the same result, I'd guess the Content-type header is being overriden somewhere after calling the Output method, presumably by CodeIgniter.