Search code examples
phppdfdompdf

how to remove extra white space from generated PDF by PHP


I made an auto-generated invoice using dompdf and PHP and for the PDF I auto generated it using an online PDF HTML converter but when I run the PHP code I get a lot of extra white around the original PDF as showing in this example enter image description here
I used this code to create the

  try{
        $pdf = new Pdf();
        $file_name = "INV ".$factname. '.pdf';
        $html_code = '<link rel="stylesheet" href="bootstrap.min.css">';
        $html_code .= fetch_customer_data();
        $pdf->load_html($html_code);
        $pdf->render();
        $file = $pdf->output();
        $file_name= mb_convert_encoding($file_name, 'iso-8859-1', 'utf-8');
        file_put_contents($file_name, $file);
        
    }
    catch (Exception $e) {
        
        

    }

I want to remove the extra white space and conserve only what's in the black border I tried :

@page {
            margin: 0px 0px 0px 0px !important;
            padding: 0px 0px 0px 0px !important;
        }

But did not work, does anyone have any Idea ?


Solution

  • To solve this problem, I added the following line with the height and width of the PDF that I have and removed every margin and PDF to move it up

    @page {size: 595px 842px; margin:0!important; padding:0!important}