我想使用dompdf將PHP頁面轉換爲PDF,但我不確定最好的方式來做到這一點。我已經寫了我的網頁我怎麼想它在標準的HTML/PHP提交,並希望它顯示像這樣,就像一個PDF:使用dompdf將頁面轉換爲PDF
我的PHP代碼如下。我相信我需要將我的代碼放入$html
變量中以將其解析爲dompdf,但我是一位PHP新手,所以希望得到一些幫助,以瞭解實現此目的的最佳方式。顯然,頁面的樣式都是從外部CSS文件中引入的,所以我不確定這是否是一個問題?
<?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");?>
更新
基於BrianS的評論(謝謝!),我的新代碼看起來像下面,但試圖查看監視列表中時,我現在得到的只是一個普通的空白頁。我已經測試了它沒有dompdf的東西,並且頁面顯示完美,顯示了我希望它顯示的所有信息,並正確顯示。我也用CSS替換了LESS文件並刪除了所有的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");?>
另外,如果有幫助,把錯誤報告讓我以下:
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
試試這個:https://github.com/mreiferson/php-wkhtmltox – GBD 2013-04-28 11:57:35
一些一般建議:如果您只是將輸出發送到瀏覽器,那麼運行代碼並不會產生什麼影響。在這種情況下,您會得到一個錯誤,您可以使用它來嘗試找出代碼中缺少的內容。錯誤並不總是準確地報告問題的根源,但在大多數情況下,它們會爲您提供所需的信息,以糾正任何語法問題。 – BrianS 2013-04-29 16:23:38