2014-09-26 118 views
1

將此類代碼渲染爲pdf的最佳方式是什麼?我已經看過不同的圖書館,但我不認爲他們中的任何一個對我而言都是有用的。我詳細研究了TCPDF庫,但是在我的情況下,這太複雜了,因爲我的代碼涉及大量的css,html和php條件。因此,我在這裏想知道在哪裏可以找到這方面有用的東西。以下是我的代碼,並從我的代碼:將HTML渲染爲PDF文件

<div class="box-container" id="week<?php print $w; ?>" > 
       <div class="roster"> 
        <div style="text-align: right;" class="dontprint"> 

         <div style="display:inline;"> 
          <label><strong>Dispatch All</strong> <input type="checkbox" name="dispatch-<?php print $w; ?>" value="<?php print $w; ?>"></label> 
         </div> 
         <div style="display:inline;"> 
          <a href="#" class="site-roster" rel="week<?php print $w; ?>"> 
           <img src="<?php print base_url('images/icon-screenshot.png'); ?>" /> 
          </a> 
         </div> 
        </div> 

        <?php $shiftStr = array(); foreach ($schedule['sites'] as $site): ?> 
         <?php if ($site['weekf'] == 'Week '.$w) : ?> 

         <?php $isDispatched[$w][$site["sid"]] = true; ?> 

          <div class="box printr" id="r<?php print $w.'-'.$site["sid"]; ?>"> 
           <h4 class="white" align="center"><?php print $site["title"]; ?> - <?php print $site["address"]; ?></h4> 
           <div class="box-container links" style="border:1px solid #CCC"> 
            <table style="width:100%"> 
             <thead> 
             <tr> 
              <td style="width:140px;"></td> 
              <?php 
              $dateForRoster = ($w == 1) ? $startDate : strtotime("+7 day", $startDate); 
              ?> 
              <?php for ($i = 0; $i < 7; $i++) : ?> 
               <td><?php print date('d-M-y', strtotime("+".$i." day", $dateForRoster)); ?></td> 
              <?php endfor; ?> 
              <td colspan="2"></td> 
             </tr> 
             <tr> 
              <td class="col-left"><?php print $site['job_type'] == 1 ? 'CSO' : 'Guard';?></td> 
              <td>Monday</td> 
              <td>Tuesday</td> 
              <td>Wednesday</td> 
              <td>Thursday</td> 
              <td>Friday</td> 
              <td>Saturday</td> 
              <td>Sunday</td> 
              <td>Contact</td> 
              <td>License</td> 
             </tr> 
             <tr> 
              <td colspan="10"><div style="border-bottom:2px solid #ccc; margin:5px 0"></div></td> 
             </tr> 
             </thead> 
             <?php foreach($schedule['guards'] as $guard): ?> 
              <?php if ($guard['weekf'] == 'Week '.$w && $site['sid'] == $guard['sid']) : ?> 
               <tr><?php $tot = 0;?> 
                <td style="max-width: 140px; overflow: hidden; white-space: nowrap;" class="col-left"><?php print ($guard["guard_name"] != '') ? $guard["guard_name"] : 'Click on each shift to assign guard'; ?></td> 

                <?php for ($s = 0; $s < 7; $s++) : ?> 
                 <td> 
                  <?php 

                  if (isset ($schedule['week'][str_replace(' ', '-', $guard['weekf']).'-'.$site['sid'].'-'.$guard['gid']][$s])) { 
                   $shifts = $schedule['week'][str_replace(' ', '-', $guard['weekf']).'-'.$site['sid'].'-'.$guard['gid']][$s]; 
                   $noOfShifts = count($shifts); 

                   if ($noOfShifts == 0) { print '-'; } 
                   else { 
                    foreach($shifts as $shift) { 

                     $shiftStr[] = array($shift['shift']['shid'], $shift['shift']['dispatched']); 
                     $allShifts[] = array($shift['shift']['shid'], $shift['shift']['dispatched']); 
                     //print $tot += dateDifference1($shift['shift']['actual_sign_in'], $shift['shift']['actual_sign_out']); 
                     $class = $shift['shift']['job_type']; 
                     $difference = dateDifference(date('Y-m-d'), $shift['shift']['advised_sign_in']); 

                     if ($shift['shift']['gid'] == "") { 
                      $class = "unassigned"; 
                     } else { 
                      if ($shift['shift']['training'] == 1) $class = "training"; 
                      if ($difference == 0 && 
                       date("Y-m-d", strtotime($shift['shift']['actual_sign_in'])) == "1970-01-01") { 
                       $class = "notime"; 
                      } 

                      if (
                       ($shift['shift']['actual_sign_in'] != "" && $shift['shift']['actual_sign_out'] == "") || 
                       ($shift['shift']['actual_sign_in'] == "" && $shift['shift']['actual_sign_out'] != "") 
                       ) 
                      { 
                       $class = "onlyone"; 
                      } 

                      if (
                       ($shift['shift']['actual_sign_in'] != "" && ($shift['shift']['advised_sign_in'] != $shift['shift']['actual_sign_in'])) || 
                       ($shift['shift']['actual_sign_out'] != "" && ($shift['shift']['advised_sign_out'] != $shift['shift']['actual_sign_out'])) 
                      ) { 
                       $class = "altertime"; 
                      } 
                     } 

                     if ($shift['shift']['dispatched'] == "0") $isDispatched[$w][$site["sid"]] = false; 

                  ?> 

                     <a href="#" 
                      class = "shift-detail <?php print $class; ?>" 
                      shid = "<?php print $shift['shift']['shid'];?>" 
                      date = "<?php print date('Y-m-d', strtotime("+".$s." day", $dateForRoster)); ?>" 
                      jid = "<?php print $shift['shift']['jid']; ?>" 
                      sid = "<?php print $site['sid']; ?>" 
                      gid = "<?php print $guard['gid']; ?>"> 
                      <?php 
                       if (/*$difference > 0 &&*/ $shift['shift']['actual_sign_in'] != "" && $shift['shift']['actual_sign_out'] != "" && date("Y-m-d", strtotime($shift['shift']['actual_sign_in'])) != "1970-01-01") 
                        print date('H:i', strtotime($shift['shift']['actual_sign_in'])).' - '.date('H:i', strtotime($shift['shift']['actual_sign_out'])) ; 
                       else 
                        print date('H:i', strtotime($shift['shift']['advised_sign_in'])).' - '.date('H:i', strtotime($shift['shift']['advised_sign_out'])) ; 
                      ?></a> 

                    <?php 
                    } 
                   } 
                  } else { 
                   ?> 
                   <a href="#" 
                    class="shift-detail" 
                    shid = "0" 
                    date = "<?php print date('Y-m-d', strtotime("+".$s." day", $dateForRoster)); ?>" 
                    jid = "0" 
                    sid = "<?php print $site['sid']; ?>" 
                    gid = "<?php print $guard['gid']; ?>"> 
                    - 
                   </a> 
                  <?php 
                  } 
                  ?> 
                 </td> 
                <?php endfor; ?> 
                <td><a href="#" class="contact"><?php print $guard['mobile_number']; ?></a></td> 
                <td><a href="#" class="license"><?php print ($guard['license_number'] != 0) ? $guard['license_number'] : 'N/A'; ?></a></td> 
               </tr> <?php //print $tot;?> 
              <?php endif; ?> 
             <?php endforeach; ?> 
            </table> 

            <div style="text-align: right; margin-top:4px; padding-top: 8px; color: #50575A; font-weight:bold; border-top:1px dotted" class="dontprint"> 

             <div style="display:inline; "> 
              <label>Dispatch <?php print $site["title"]; ?> shifts <input type="checkbox" name="dispatch-<?php print $w.'-'.$site["sid"]; ?>" id="dispatch-<?php print $w.'-'.$site["sid"]; ?>" <?php print ($isDispatched[$w][$site["sid"]]) ? 'checked="checked"' : '' ;?> value='<?php print json_encode($shiftStr);?>'></label> 
             </div> 
             <div style="display:inline;"> 
              <a href="#" class="site-roster" rel="r<?php print $w.'-'.$site["sid"]; ?>"> 
               <img src="<?php print base_url('images/icon-screenshot.png'); ?>" /> 
              </a> 
             </div> 
            </div> 


           </div> 


           <!-- end of div.box-container --> 
          </div> 

         <?php unset($shiftStr); endif; ?> 
        <?php endforeach; ?> 
       </div> 
      </div> 
      <input type="hidden" name="allshifts-<?php print $w;?>" id="allshifts-<?php print $w;?>" value='<?php print json_encode($allShifts); ?>' /> 
     <?php endfor; ?> 
    <?php endif; ?> 
</div> 

輸出

enter image description here

我期待直接呈現這種輸出成PDF。或者,我在想我應該首先捕捉圖像,然後將該圖像渲染爲PDF,但這將是雙重工作。

因此,如果您有任何想法可以幫助我,請在此分享。

謝謝。

+0

去與TCPDF,這肯定會解決您的問題。 – 2014-09-26 05:58:00

+0

[將HTML + CSS轉換爲PDF格式的PDF?](http://stackoverflow.com/questions/391005/convert-html-css-to-pdf-with-php) – user1914292 2014-09-26 11:00:38

回答

1

我已經使用DOMPDF,因爲它工作得很好。保留生成的HTML代碼放在一個變量,並傳遞到DOMPDF,像這樣:

$dompdf = new DOMPDF(); 
$dompdf->load_html($my_pdf_content); 
$dompdf->set_paper('a4', 'portrait'); 
$dompdf->render(); 

在此之後,你可以保存文件:

file_put_contents('my_pdf.pdf', $dompdf->output()); 

,或者強制下載到瀏覽器:

$dompdf->stream("dompdf_out.pdf", array("Attachment" => true)); 
exit(0); 
+0

我使用DOMPDF創建了我的PDF,但我相信這個庫有速度性能問題。需要50到60秒才能生成10頁PDF,我認爲這是不可接受的。你知道有什麼方法可以幫助我擺脫這個問題嗎? – 2014-10-03 15:34:57

+0

不,我只用它來打印發票。最多,我有3頁。您也可以嘗試緩存這些PDF文件。如果只有幾條記錄要創建爲pdf或者所有記錄都是靜態的,則可以運行腳本來創建這些pdf。這種方法唯一的問題是硬盤空間。 – machineaddict 2014-10-06 07:43:52

+0

我通過搜索「php pdf generation overflow:hidden」來到這裏。我對這個CSS代碼有問題:(從主題啓動器複製):「max-width:140px; overflow:hidden; white-space:nowrap;」。我完全使用DOMPDF,它所做的是剪切文本,但保持行寬很大,就像文本未被剪切一樣!我投了棄票。 – user109764 2017-02-24 20:51:57

1

你也可以試試這個TCPDF很容易使用PHP PDF庫..你也可以檢查例子here