0

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;
}
  • What *exactly* is wrong with the output you're getting? "Sometimes looks weird" isn't enough information. – Calum Halpin Mar 08 '20 at 13:59
  • I can't say precisely, because it variates from page to page. If I just make an echo of the $content everything looks fine on the browser. Could be normal? This is google.com. https://ibb.co/xYf4v1T –  Mar 08 '20 at 15:26

1 Answers1

0

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

  • Hey, thank you. Yes, I'm parseing all the links inside the string and adding the host to all href and src (i havent paste the entire code because was to much)... but still the pdf look like a "one column" page or something like that. When I echo my $content variable the page looks perfect, but when I put this on the pdf is when it gets messy. –  Mar 08 '20 at 13:44