duanquannan0593 2018-08-12 06:28
浏览 54

在服务器上用css作为pdf保存动态php页面的内容

I have a PHP page that retrieves various data from database that are displayed in the page itself. I want to generate PDF file with the contents and save that file in the server for later use. My PHP page renders perfectly in the browser.
I have tried to use header('Content-disposition: attachment; filename=filename.pdf');, but get error Failed to load PDF document.
I also tried dompdf but ended up with blank pdf document.
Since the file contains some sensitive data, I would not risk sending the contents to a third-party server. Please help me out.

My file contains PHP and CSS mainly.

<?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>

I have mentioned both the codes here corresponding to both the methods mentioned above. Please have a look at the code and guide me to have the desired output.
Thanks in advance.

</div>
  • 写回答

1条回答 默认 最新

  • duanfeigui6655 2018-08-12 09:10
    关注

    Just adding the header stating the content you are returning is of type PDF isn't enough since the content you are sending is not in that format. I can tell you I have experience with creating PDF documents in PHP using fpdf (http://www.fpdf.org/) and it works nicely. You can create a document using FPDF library and then return the files contents instead of the HTML you are returning currently.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀