PDF生成但已损坏

PDF生成但已损坏

问题描述:

I've checked many threads here and tried them out, but nothing works. It's generating MS Word files but not PDF. Can anyone tell me a quick way to generate PDF files.

Here's the code which I try :

<?php ob_start(); ?>
<style>

#srch-frm {}
.srchfld {
    font-size: 16px;
    height: 31px;
    padding: 4px;
    width: 162px;
}
.out-fld {
   padding: 11px;
}
.box_search {}
#srch-submit {
    font-size: 15px;
    height: 34px;
    width: 128px;
}
.township_report {
    display:none;
}
</style>

<div id="wrapper">


<div id="content">

<p>&nbsp;</p>

<div class="srch_report">

<style>
#report-headings {
    font-size: 20px;
    font-weight: bold;
    position: relative;
    top: -5px;}
#report-title {
    font-size: 20px;
    font-weight: bold;}
#report {
    background-color: lightgray;
    left: 52px;
    padding: 32px;
    position: relative;
    width: 808px;}
.hdr {    
    font-family: verdana;
    font-size: 14px;}
.hdr-ttl {
    padding-left: 39px;}
.hdr-date {padding-left: 195px;}
.hdr-val {padding-left: 5px;}
.fld-ttl {}
.fld-val {
    float: right;
    padding-right: 127px;}
.fld-row {
    height: 23px;}
.fld-col {    
    float: left;
    width: 50%;}
#fld-area {
    font-size: 16px;}
.fld2-val {}
.fld2-ttl {
    padding-left: 103px;}
.fld2 {
    font-weight:bold;}
.fld2-main {
    padding-top: 20px;}
.fld2-val-ini {}    
</style>

<p>&nbsp;</p>
<div id="report">

    <div id='report-headings' align="center">
        Law Offices of Patrick C. Doody
    </div>

    <div id='report-title'>
      <p>Township Deadlines </p>
    </div>

    <div class="hdr">   
      <spam class=''>Year:</spam> <spam class='hdr-val'>2006</spam>
        <spam class='hdr-ttl'>County:</spam> <spam class='hdr-val'>Cook</spam>
        <spam class='hdr-ttl'>Township:</spam> <spam class='hdr-val'>Cook</spam>
        <spam class='hdr-date'>As Of:</spam> <spam class='hdr-val'>2013-05-14</spam>
    </div>

<p>&nbsp;</p>

    <div id="fld-area">

        <div class="fld-row">
            <div class="fld-col">
                <spam class="fld-ttl">Assessor Open:</spam><spam class="fld-val">2006-03-07</spam>
            </div>  
            <div class="fld-col">
                <spam class="fld-ttl">BofR Opens:</spam><spam class="fld-val">2006-09-11</spam>
            </div>
        </div>

        <div class="fld-row">
            <div class="fld-col">
                <spam class="fld-ttl">Assessor Closes:</spam><spam class="fld-val">2006-04-05</spam>
            </div>
            <div class="fld-col">
                <spam class="fld-ttl">BofR Closes:</spam><spam class="fld-val">2006-10-18</spam>
            </div>
        </div>

        <div class="fld-row">
            <div class="fld-col">
                <spam class="fld-ttl">Assr Documents Due:</spam><spam class="fld-val">2006-04-20</spam>
            </div>
            <div class="fld-col">
                <spam class="fld-ttl">BofR Hearing:</spam><spam class="fld-val"></spam>
            </div>
        </div>

        <div class="fld-row">
            <div class="fld-col">
                <spam class="fld-ttl">Assr Certified:</spam><spam class="fld-val">2006-05-31</spam>
            </div>
        </div>

    </div>
<p>&nbsp;</p>
<p>
          <table>
        <tr>
            <td style="width:140px;font-weight:bold;">File #</td>
            <td style="width:140px;font-weight:bold;">Client</td>
            <td style="width:190px;font-weight:bold;">Property Name</td>
            <td style="width:140px;font-weight:bold;">Atty</td>
        </tr>
        <p>
          <tr><td>00-5009</td><td>DORAN</td><td>Popeye's Chicken</td><td>F&G</td></tr></table>        </p>
</div>
<p>&nbsp;</p>
<style>
.prntdiv {
    float: left;
    padding: 16px 16px 16px 386px;}
.savediv {
    float: left;
    padding: 16px;}
#print {
    font-family: times new roman;
    font-size: 15px;
    font-weight: bold;
    padding: 6px 15px;}
#save {
    font-family: times new roman;
    font-size: 15px;
    font-weight: bold;
    padding: 6px 15px;}

</style>

</div>
</p>
</div>
</div>
</div>
<?php
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="report.pdf"');
    header('Content-Transfer-Encoding: binary');
?>

PDF doesn't work like that.

You're essentially creating a web page (standard HTML/CSS) and then telling the browser "get this as if it were PDF", which isn't going to work because when the browser hands it off to its PDF viewer, it will not be a valid PDF file.

You want to convert that HTML/CSS to PDF. See: Convert HTML + CSS to PDF with PHP?