dat*_*nfo 3 php pdf dompdf twitter-bootstrap-3
我正在用 PHP 生成一个列表(某种学生预约卡)。我希望能够将其另存为 PDF。我已经成功地尝试了 PHP 和 DOMPDF 的一些基本布局。
当我尝试将以下内容另存为 PDF 时,它不起作用。
<?php
session_start();
// User session check
if(!$_SESSION['email'])
{
header("Location: login.php");//redirect to login page to secure the welcome page without login access.
}
include("php/getCustomerEvents.php");
// get row id = student id
$id = $_GET[id];
// check user language
$lang = $_SESSION['lang'];
switch ($lang)
{
case 'en':
$lang_file = 'lang.en.php';
break;
//case 'fr':
//$lang_file = 'lang.fr.php';
//break;
case 'nl':
$lang_file = 'lang.nl.php';
break;
default:
$lang_file = 'lang.nl.php';
}
include_once 'lang/'.$lang_file;
// Debug code
//print_r($_SESSION);
//echo "----------------------------------------------------------------------->".$_SESSION['lang']." = taal <br>";
//echo "----------------------------------------------------------------------->".$_SESSION['name']." = naam <br>";
//echo "----------------------------------------------------------------------->".$_SESSION['resource']." = id <br>";
//Echo "----------------------------------------------------------------------->".$_SESSION['admin']." = admin <br>";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/bootstrap-select.css">
<style type="text/css">
.bs-example{
margin: 20px;
}
/* Fix alignment issue of label on extra small devices in Bootstrap 3.2 */
.form-horizontal .control-label{
padding-top: 7px;
}
</style>
</head>
<body >
<!-- Main content -->
<section class="content" >
<!-- Your Page Content Here -->
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header">
<h3 class="box-title"><?php echo $lang['LBL_LIST_EVENTS']; ?></h3>
</div><!-- /.box-header -->
<div class="box-body">
<table id="instructors" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th><?php echo $lang['LBL_LESSON']; ?></th>
<th><?php echo $lang['LBL_LOCATION']; ?></th>
<th><?php echo $lang['LBL_TITLE']; ?></th>
<th><?php echo $lang['LBL_EVENT_START']; ?></th>
<th><?php echo $lang['LBL_EVENT_END']; ?></th>
</tr>
</thead>
<tbody>
<?php customerevents($id); ?><!-- function from getCustomerEvents.php -->
</tbody>
</table>
</div><!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-success" ><span class="glyphicon glyphicon-print" aria-hidden="true"></span> PDF </button>
<button type="submit" class="btn btn-success" ><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span> MAIL </button>
</div><!-- /.box-footer -->
</div><!-- /.box -->
</div><!-- /.col -->
</div><!-- /.row -->
</section><!-- /.content -->
<!-- Main Footer -->
<footer class="main-footer">
<!-- To the right -->
<div class="pull-right hidden-xs">
</div>
<!-- Default to the left -->
<strong>Copyright © 2015 <a href="http:\\www.biqsit.be">biqs it</a>.</strong> All rights reserved.
</footer>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我用来生成 PDF 的代码:
<?php
require_once("../dompdf/dompdf_config.inc.php");
//$content = "../" . $_GET["content"];
//$file_name = $_GET["file_name"];
$dompdf = new DOMPDF();
//$html = file_get_contents($content);
$html = file_get_contents('../pdf_customer_events.php');
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?或者 DOMPDF 不是正确的解决方案?
要转换不包含任何依赖项的文件,您可以获取内容并将其提供给转换库。
$html = file_get_contents('pregenerated.html');
Run Code Online (Sandbox Code Playgroud)
此方法不允许传递可能需要的变量(会话/POST...)。
通过缓冲内容,您可以避免将其发送到浏览器,然后可以将其提供给库。
ob_start();
include('content.php');
$html = ob_get_clean();
Run Code Online (Sandbox Code Playgroud)
这样你就可以像平常一样传输session/post/get,甚至可以正确处理错误。您还可以使用模板引擎进行高级输出。
通过包含 php 脚本,所有子文件(脚本、css、iframe...)都不会包含在内。因此,对于不包含图像的简单 pdf,您需要将所有内容都放在一页中。
相当不可维护......但可行。
因此,幸运的是有一些 php 工具可以解释页面并可以获取任何子文件。最常见的是wkhtmltopdf,它是一个二进制文件,其中包含 Chrome 的渲染引擎webkit来解释页面并打印 pdf。
为此,您可以像前面的示例一样向他提供内容,或者提供一个 url。您可以控制打印(边距、颜色...)。
它有许多php 包装器,使其易于在代码中使用。
但是(总有一个但是...)wkhtmltopdf不使用最新的引擎webkit,如果像在我的公司一样您喜欢光滑的 css3 和 javascript canvas 功能,这可能会很烦人。
我们所做的是使用无头浏览器,将页面呈现为浏览器并以 pdf 格式打印(这已经是 wkhtmltopdf 所做的,具有出色的优化和功能)。
最先进的无头浏览器可能是PhantomJS。基于Webkit,可以轻松实现多种格式的打印。
它有一个 PHP 包装器:http://jonnnnyw.github.io/php-phantomjs/
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = { width: 1920, height: 1080 };
page.open("http://www.google.com", function start(status) {
page.render('google_home.jpeg', {format: 'jpeg', quality: '100'});
phantom.exit();
});
Run Code Online (Sandbox Code Playgroud)
旁注:在我们公司,我们使用SlimerJS,因为它在我们构建服务时提供了更好的输出,这只是一个问题,因为我们的页面在浏览器上没有保持一致,并且转换弄乱了某些部分......
当我们开发该服务时,SlimerJS直到正在开发的0.10 版本才提供完整的 PDF 打印支持。所以我们必须用最新的版本自己编译它xulrunner。