5

I am trying to convert a PHP page to PDF using dompdf, but I'm not sure of the best way to do it. I have wrote my page how I would like it to be presented in standard HTML/PHP, and would like it to be displayed like so, just as a PDF:

Desired output

My PHP code is below. I believe I need to put my code into the $html variable to parse that into dompdf, but I'm a bit of a PHP newbie, so would appreciate some help as to what the best way to achieve this is. Obviously, the styling of the page is all being brought in from external CSS files, so I'm not sure if that's an issue or not?

<?php
    include("checklog.php"); 
    require_once("watchlist-controller.php");
    require_once("dompdf/dompdf_config.inc.php");
    $html = 
?>
<!DOCTYPE html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="shortcut icon" href="img/fav.ico">
        <link rel="apple-touch-icon" href="img/apple-touch-icon.png">
        <title>Screening - Your ticket to your movies - <?php echo $watchlist_name; ?></title>
        <meta name="description" content="Screening is a brand new take on the traditional movie database, fusing social networking and multimedia to provide a clear, concise experience allowing you to share your favourite movies, and discover new classics.">
        <meta name="keywords" content="Movies, Films, Screening, Discover, Watch, Share, experience, database, movie database, film database, share film, share films, discover film, discover films, share movie, share movies, discover movie, discover movies">

        <!-- Bootstrap -->
        <link href="css/bootstrap.css" rel="stylesheet" media="screen">
        <link href="css/bootstrap-responsive.css" rel="stylesheet">
        <link href="css/custom-bootstrap.css" rel="stylesheet">
        <link rel="stylesheet" href="fonts.css" type="text/css" />
        <link rel="stylesheet/less" type="text/css" href="css/stylesheet.less" />
        <script src="js/less-1.3.3.min.js" type="text/javascript"></script>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
    </head>

    <body>
        <div class="container"><?php 
            require_once ("header.php");?>
            <div id="main" class="well main-content">
                <p class="page-title"><?php echo $watchlist_name; ?></p>
                <div class="row-fluid">
                    <section class="span12 watchlist-holder film-list"><?php
                        if (count($films) == 0) {?>
                            <div class="alert alert-info">This Watchlist is empty! Why not visit a movie page to add something to it?</div><?php
                        } else {?>
                            <ul class="unstyled"><?php
                                foreach($films as $key => $film_item) {
                                    include("watchlist-film-controller.php");?>
                                    <li class="well list-item clearfix">
                                        <div class="row-fluid">
                                            <a href="movie.php?id=<?php echo $rt_id; ?>" class="span1 pull-left" title="<?php echo $title; ?>"><img src="<?php echo $poster_thumb; ?>" alt="<?php echo $title; ?> poster" title="<?php echo $title; ?> poster" /></a>

                                            <div class="span11 movie-info">
                                                <p class="search-title"><a href="movie.php?id=<?php echo $film_item['film_id']; ?>" title="<?php echo $title; ?> (<?php echo $year; ?>)"><?php echo $title; ?></a> <small>(<?php echo $year; ?>)</small></p><?php

                                                if ($critics_consensus == "") {?>
                                                    <p class="watchlist-synopsis">No overview available</p><?php
                                                } else {?>
                                                    <p class="watchlist-synopsis"><?php echo $critics_consensus; ?></p><?php
                                                }?>
                                            </div>
                                        </div>
                                    </li><?php
                                }?>
                            </ul><?php
                        }?>
                    </section>
                </div>
            </div><?php
            include 'footer.html'; ?>
        </div>
    </body>
</html><?php
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");?>

Update

Based on BrianS's comment (thank you!), my new code looks like below, however I'm now getting just a plain blank page when trying to view the Watchlist. I've tested it without the dompdf stuff, and the page appears perfectly, showing all the information I want it to show, and presented correctly. I've also replaced the LESS file with CSS and removed all the JS.

<?php
    include("checklog.php");
    require_once("watchlist-controller.php");
    require_once("dompdf/dompdf_config.inc.php");
    ob_start();
?>
<!DOCTYPE html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="shortcut icon" href="img/fav.ico">
        <link rel="apple-touch-icon" href="img/apple-touch-icon.png">
        <title>Screening - Your ticket to your movies - <?php echo $watchlist_name; ?></title>
        <meta name="description" content="Screening is a brand new take on the traditional movie database, fusing social networking and multimedia to provide a clear, concise experience allowing you to share your favourite movies, and discover new classics.">
        <meta name="keywords" content="Movies, Films, Screening, Discover, Watch, Share, experience, database, movie database, film database, share film, share films, discover film, discover films, share movie, share movies, discover movie, discover movies">

        <!-- Bootstrap -->
        <link href="css/bootstrap.css" rel="stylesheet" media="screen">
        <link href="css/bootstrap-responsive.css" rel="stylesheet">
        <link href="css/custom-bootstrap.css" rel="stylesheet">
        <link rel="stylesheet" href="fonts.css" type="text/css" />
        <link href="css/stylesheet.css" rel="stylesheet">
    </head>

    <body>
        <div class="container">
            <header>
                <div class="navbar navbar-inverse">
                    <div class="navbar-inner">
                        <div class="container">
                            <a href="index.php" title="Screening"><img src="img/blue_logo.png" class="pull-left" title="Screening - Your ticket to your movies" alt="Screening - Your ticket to your movies" /></a>
                            <a class="brand" href="index.php">Screening</a>
                        </div>
                    </div>
                </div>
            </header>
            <div id="main" class="well main-content">
                <p class="page-title">"<?php echo $watchlist_name; ?>" Watchlist by <?php echo $first_name; ?> <?php echo $last_name; ?></p>
                <div class="row-fluid">
                    <section class="span12 watchlist-holder film-list">
                        <ul class="unstyled"><?php
                            foreach($films as $key => $film_item) {
                                include("watchlist-film-controller.php");?>
                                <li class="well list-item clearfix">
                                    <div class="row-fluid">
                                        <a href="movie.php?id=<?php echo $rt_id; ?>" class="span1 pull-left" title="<?php echo $title; ?>"><img src="<?php echo $poster_thumb; ?>" alt="<?php echo $title; ?> poster" title="<?php echo $title; ?> poster" /></a>

                                        <div class="span11 movie-info">
                                            <p class="search-title"><a href="movie.php?id=<?php echo $film_item['film_id']; ?>" title="<?php echo $title; ?> (<?php echo $year; ?>)"><?php echo $title; ?></a> <small>(<?php echo $year; ?>)</small></p><?php
                                            if ($critics_consensus == "") {?>
                                                <p class="watchlist-synopsis">No overview available</p><?php
                                            } else {?>
                                                <p class="watchlist-synopsis"><?php echo $critics_consensus; ?></p><?php
                                            }?>
                                        </div>
                                    </div>
                                </li><?php
                            }?>
                        </ul>
                    </section>
                </div>
                <p>This Watchlist was generated by <span class="bold">Screening</span>.</p>
            </div>
        </div>
    </body>
</html><?php
$html = ob_get_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("watchlist.pdf");?>

Also, if it helps, turning on error reporting gets me the following:

Notice: Undefined variable: submit in \\ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening\watchlist-controller.php on line 20
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in \\ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening\watchlist-controller.php on line 107
Notice: Undefined index: name in \\ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening\watchlist-controller.php on line 111
Notice: Undefined index: category in \\ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening\watchlist-controller.php on line 112
Notice: Undefined index: description in \\ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening\watchlist-controller.php on line 113
Notice: Undefined index: hash in \\ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening\watchlist-controller.php on line 114
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in \\ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening\watchlist-controller.php on line 119
Warning: require_once(//ICS-FILESHARE/WWW/newmedia.leeds.ac.uk/ug10/cs10aer/screening/dompdf/lib/php-font-lib/classes/font.cls.php): failed to open stream: No such file or directory in \\ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening\dompdf\dompdf_config.inc.php on line 335
Fatal error: require_once(): Failed opening required '//ICS-FILESHARE/WWW/newmedia.leeds.ac.uk/ug10/cs10aer/screening/dompdf/lib/php-font-lib/classes/font.cls.php' (include_path='.;C:\php\pear') in \\ICS-FILESHARE\WWW\newmedia.leeds.ac.uk\ug10\cs10aer\screening\dompdf\dompdf_config.inc.php on line 335
lxg
  • 12,375
  • 12
  • 51
  • 73
Alex Ryans
  • 1,865
  • 5
  • 23
  • 31
  • Try this: https://github.com/mreiferson/php-wkhtmltox – GBD Apr 28 '13 at 11:57
  • 1
    Some general advice: if you're just sending output to the browser then it doesn't hurt to run the code an see what happens. In this case you would have gotten an error, which you can use to try and figure out what you're missing in your code. Errors don't always accurately report on the source of the problem, but in most cases they give you the info you need to correct any syntax problems. – BrianS Apr 29 '13 at 16:23

3 Answers3

7

There are a number of ways of doing what you want. Your current logic should work, though the syntax isn't quite right. If you want to stick with your current structure, though, you might use output buffering instead.

<?php
include("checklog.php"); 
require_once("watchlist-controller.php");
require_once("dompdf/dompdf_config.inc.php");
ob_start();
?>

<html>...</html>

<?php
$html = ob_get_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>

Linked stylesheets are fine. JavaScript is not, dompdf doesn't support client-side JS. So if your LESS stylesheet is required you'll have to convert it to standard CSS.

BrianS
  • 13,284
  • 15
  • 62
  • 125
  • Hi, thanks for your help! I appreciate the assist. I've made the changes you suggest, however I'm now getting a plain blank page when trying to download the PDF. I've updated my original post w/ the new code, based on your comment. – Alex Ryans Apr 29 '13 at 18:37
  • Edit: Also added error reporting messages to updated post for further clarity. – Alex Ryans Apr 29 '13 at 19:08
  • Most of those warnings/notices don't have anything to do with dompdf. What you're seeing appears to be a missing variable causing your MySQL query to fail. Always check the status of your query. For example, `$query = mysql_query(...); if ($query) {...} else {...}`. – BrianS Apr 29 '13 at 21:23
  • FYI, you might want to stop using the MySQL extension since [it's deprecated](http://www.php.net/manual/en/intro.mysql.php). Sure it's easier to learn (if for no other reason than the voluminous amount of examples), and it will be around for a while, but it's a good idea to learn using a library that won't be going away. If you want to stick with a MySQL library MySQLi is recommended. Or try PDO, which makes it easier to work on a variety of databases by standardizing the API. – BrianS Apr 29 '13 at 21:25
  • Normally execution continues through notices/warnings, but there is a fatal error and it's related to dompdf. The last message is an error indicating that php-font-lib is missing. If you downloaded dompdf from github you'll have to [download the php-font-lib library separately](http://stackoverflow.com/a/13432787/264628). – BrianS Apr 29 '13 at 21:29
  • Thanks. I've downloaded php-font-lib and uploaded it to the corresponding empty directory and the PDF now creates successfully, however it's unable to be opened. If I stream it to the browser, rather than downloading it, I get 'Cannot load PDF', and attempting to open a downloaded copy results in an error, too. – Alex Ryans Apr 29 '13 at 22:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29146/discussion-between-brians-and-alex-ryans) – BrianS Apr 30 '13 at 00:26
4

Use the following simple code.

require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();

$html = 'Insert full HTML content';
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("codexworld",array("Attachment"=>0));

You can find the complete guide to using Dompdf for convert web page to PDF in PHP from here - Convert HTML to PDF in PHP with Dompdf

JoyGuru
  • 1,803
  • 20
  • 11
0

I would suggest using WKHTMLTOPDF which provides quite a lot options

werd
  • 648
  • 4
  • 13
  • 23