Search code examples
codeigniterhtml2pdf

Spaces in Html using Html2pdf with Codeigniter


I am using html2pdf library within Codeigniter

When I put spaces in html code I cannot write to the file.

When I remove spaces in html code I can write to the file because there is no spaces in the html code

I would like to use the view file within Codeigniter but it is the same problem

$this->html2pdf->html($this->load->view('tableaubord/tableaubord_read', '', true))

(I use visual studio code as IDE)

Example:

This can be written to the file

$this->html2pdf->html('<html><head><title>Page Title</title></head><body><h1>This is a Heading</h1> <p>This is a paragraph.</p></body></html>');
$this->html2pdf->create('save')

This cannot be written to the file !

    $this->html2pdf->html('<html>
                            <head>
                              <title>Page Title</title>
                            </head>
                              <body><h1>This is a Heading</h1> 
                               <p>This is a paragraph.</p>
                             </body>
                           </html>');

    $this->html2pdf->create('save')

Solution

  • "Sanitize" the view output into a string, removing line breaks and spaces between tags.

    $html = $this->load->view('tableaubord/tableaubord_read', '', true);
    
    // removes line-breaks (optional)
    $html = str_replace(array("\n", "\r"), '', $html);
    
    // removes whitespace between tags:
    $html = preg_replace('/\>\s+\</m', '><', $html);
    
    $this->html2pdf->html($html);
    $this->html2pdf->create('save')
    

    php sandbox