在服务器上将带有CSS的动态PHP页面的内容另存为pdf

时间:2018-08-12 06:28:04

标签: php css pdf-generation dompdf

我有一个PHP页面,该页面从数据库中检索页面本身中显示的各种数据。我想生成包含内容的PDF文件,并将该文件保存在服务器中以备后用。我的PHP页面在浏览器中呈现完美。
我尝试使用header('Content-disposition: attachment; filename=filename.pdf');,但收到错误Failed to load PDF document.
我也尝试过dompdf,但最终得到了空白的pdf文档。
由于该文件包含一些敏感数据,因此我不冒险将内容发送到第三方服务器。请帮帮我。

  

我的文件主要包含PHP和CSS。

<?php
include('php-include/connect.php');

if(isset($_GET['purpose']) && isset($_GET['membership'])) {
    $purpose = $_GET['purpose'];
    $membership = $_GET['membership'];
    if ($purpose == 'Volunteer Membership') {
        $member_type = 'temporary';
        $query = mysqli_query($con,"select * from temp_members where `membership`='$membership'");
        if (mysqli_num_rows($query) > 0) {
            while ($row = mysqli_fetch_array($query)) {
                $name = $row['name'];
                $email = $row['email'];
                $address = $row['address'];
                $city = $row['place'];
                $state = $row['state'];
                $country = $row['country'];
                $pin = $row['pin'];
                $bloodgroup = $row['bloodgroup'];
                $dob = $row['dob'];
            }
        }
    } 
}

$count = substr($membership,-10);
$myDateTime = DateTime::createFromFormat('Y-m-d', $dob);
$dob = $myDateTime->format('d/m/Y');
$today = date('d/m/Y');
switch ($member_type) {
    case 'temporary':
        $validity = strtotime("+6 months");
        $validity = date('d/m/Y', $validity);
        break;
    case 'default':
        $validity = 'Lifetime';
}
?>
<?php // HTTP Headers for PDF File Downloads

// set example variables
$filename = $membership.".pdf";
$filepath = "http://localhost/example/Members/";

// http headers for zip downloads
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
@readfile($filepath.$filename);
?>
<?php
require_once('dompdf/autoload.inc.php');
use Dompdf\Dompdf;
$dompdf = new Dompdf();
//$dompdf->set_paper('A4','landscape');
$fileurl = 'http://localhost/example/card view.php?purpose='.$purpose.'&membership='.$membership;
$dompdf->loadHtmlFile($fileurl);
$dompdf->render();
//$dompdf->stream();
$output = $dompdf->output();
$file = "http://localhost/example/Members/".$membership.".pdf";
file_put_contents($file, $output);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://www.w3.org/2005/10/profile">
<link href="images/logo.png" rel="shortcut icon" type="image/png" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.8/css/all.css">
<style type="text/css" media="all">
@font-face {
    font-family: "CHECKBK0";
    src: url(http://localhost/example/fonts/CHECKBK0.TTF) format('truetype');
    }
@font-face {
    font-family: "miner";
    src: url(http://localhost/example/fonts/MANICMINER.ttf) format('truetype');
    }
#top {
    height: 100%;
    }
#position_me {
    left: 0;
    }
#printerhide {
    left: 50px;
    top: 450px;
    position: fixed;
    text-align: center;
    }
#printarea {
    border: medium solid #8B7B67;
    width: 360px;
    height: 270px;
    border-radius: 5%;
    }
.Seal{
    height: 60px;
    width: 350px;
    position: fixed;
    left: 15px;
    top: 27px;
    background-image: url(http://localhost/example/images/logo1.png);
    background-color: #DDDDDD;
    background-size: 350px 60px;
    background-repeat: no-repeat;
    z-index: 5;
    filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://localhost/example/images/logo1.png',sizingMethod='scale');
    -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://localhost/example/images/logo1.png',sizingMethod='scale')";
    }
.Signature{
    width: 100px;
    height: 50px;
    position: fixed;
    left: 205px;
    top: 184px;
    background-image: url(http://localhost/example/images/signature.png);
    background-size: 100px 50px;
    background-repeat: no-repeat;
    z-index: 11;
    filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://localhost/example/images/signature.png',sizingMethod='scale');
    -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://localhost/example/images/signature.png',sizingMethod='scale')";
    }
.ExaminerText{
    width: fit-content;
    position: fixed;
    left: 230px;
    top: 217px;
    color: #8B7B67;
    z-index: 11;
    }
.ParagraphSmall{
    height: 200px;
    width: fit-content;
    position: fixed;
    font-size: 13px;
    text-align: left;
    z-index: 11;
    color: #8B7B67;
    max-width: 300px;
    }
.ParagraphMedium{
    width: 350px;
    position: fixed;
    left: 15px;
    top: 242px;
    font-size: 12px;
    text-align: center;
    z-index: 11;
    color: #8B7B67;
    }
.MembershipNumber {
    height: 15px;
    width: 350px;
    position: fixed;
    font-size: 14px;
    z-index: 11;
    color: #8B7B67;
    }
.Number {
    font-family: "miner";
    font-size: 12px;
    }
.MiddleLine{
    width: 350px;
    position: fixed;
    left: 15px;
    top: 232px;
    z-index: 11;
    color: #8B7B67;
    }
.StudentName{
    font-weight: bold;
    font-size: 15px;
    text-align: center;
    z-index: 11;
    color: #8B7B67;
    }
</style>
</head>

<body><div id="printarea">
<div class="StudentName" style="width: 350px; margin-left: -3px; margin-top: -2px;"><?php echo($purpose) ?></div>
<div class="MembershipNumber" style="left: 135px; top: 89px; width: 230px">Membership #: <span class="Number">EX</span><span style="font-family: 'CHECKBK0'"><?php echo($count) ?></span></div>
<div class="MembershipNumber" style="left: 15px; top: 102px">Name: <span class="StudentName"><?php echo($name) ?></span></div>
<div class="MembershipNumber" style="left: 15px; top: 117px">Address:&nbsp;<span class="ParagraphSmall" style="top: 117px"><?php echo("$address, $city, $state, $country. PIN - $pin") ?></span></div>
<div class="MembershipNumber" style="left: 15px; top: 182px">DOB:&nbsp;<span class="ParagraphSmall"><?php echo($dob) ?></span></div>
<div class="MembershipNumber" style="left: 15px; top: 202px">BloodGroup:&nbsp;<span class="ParagraphSmall"><?php echo($bloodgroup) ?></span></div>
<div class="MembershipNumber" style="left: 15px; top: 222px">Valid upto:&nbsp;<span class="ParagraphSmall"><?php echo($validity) ?></span></div>
<div class="Seal"></div>
<hr class="MiddleLine" />
<div class="ExaminerText">Secretary</div>
<div class="Signature"></div></div>
</body>
</html>

我在这里已经提到了与上述两种方法相对应的两种代码。请查看代码,并指导我获得所需的输出。
谢谢。

1 个答案:

答案 0 :(得分:0)

仅添加说明您返回的内容为PDF类型的标头是不够的,因为您发送的内容不是该格式。 我可以告诉您,我有使用fpdf(http://www.fpdf.org/)在PHP中创建PDF文档的经验,并且效果很好。 您可以使用FPDF库创建文档,然后返回文件内容,而不是当前返回的HTML。