【问题标题】:Save contents of dynamic php page with css as pdf on server将带有css的动态php页面的内容保存为服务器上的pdf
【发布时间】:2019-01-19 05:43:45
【问题描述】:

我有一个 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>

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

【问题讨论】:

    标签: php css pdf-generation dompdf


    【解决方案1】:

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

    【讨论】:

    • 如果您能帮助我解决代码或我应该对代码进行的任何更改,我将不胜感激。由于我的代码包含单独使用的两种方法(headerdompdf),因此您可以忽略与您的答案部分无关的部分。请告诉我如何在我的项目中使用fpdf。另外,我需要提到的是,特定文件需要以确切的动态名称 ($membership.pdf) 保存在服务器中,并稍后邮寄。
    • 我没用过 dompdf,看来你想用它,这样你代码的 HTML 部分中的所有内容都将进入 loadHtmlFile 命令,尝试将所有 HTML 放入一个字符串然后在该字符串上调用 loadHtml。
    猜你喜欢
    • 1970-01-01
    • 2013-04-30
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    相关资源
    最近更新 更多