Search code examples
phplaraveldompdf

Why my pdf document generated from html looks weird?


I have made a simple laravel app to create a pdf document from a page through an url. But my pdf doesn't get the right style from the page and sometimes looks weird. Am I doing it wrong?

This is google.com

enter image description here

This is what I'm doing with dompdf

$pdf->loadHTML($content); <--- HTML getted with curl
$pdf->setPaper('A2', 'portrait');
$output = $pdf->output();

This is how I get the html on a string.

protected function get_web_page( $url )
{
    /**
    * Send a GET requst using cURL
    * @param string $url to request
    * @param array $user_agent values to send
    * @param array $options for cURL
    * @return string
    */
    $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';

    $options = array(

        CURLOPT_CUSTOMREQUEST  =>"GET",        //set request type post or get
        CURLOPT_POST           =>false,        //set to GET
        CURLOPT_USERAGENT      => $user_agent, //set user agent
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 10,      // timeout on connect
        CURLOPT_TIMEOUT        => 10,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

Solution

  • You're probably not getting the style of the page via your curl.

    See if this helps providing everything on your "$content" variable.

    PHP: Get all CSS files of an HTML web page