2017-11-14 6 views
0

PHP를 사용하여 다운로드 할 수있는 동적 PDF 파일을 만들려고했습니다. 아래 코드를 포함 시켰습니다. 문서에 빈 데이터가 표시되고 PDF 파일에는 아무 것도 표시되지 않습니다.서버에서 동적 데이터를 가져 와서 TCPDF를 사용하여 PDF로 표시

<?php 
$output = ''; 
$quote = $_GET['quote']; 
function fetchData(){ 
    include 'config.php'; 
    $quote = $_GET['quote']; 
    $output = ''; 
    $query = "SELECT * FROM quote_list, quotation WHERE quote_list.quote_id = quotation.id AND quote_list.quote_id = '$quote'"; 
    $result = mysqli_query($connect, $query); 

    if($row = mysqli_fetch_assoc($result)){ 

    $companyName = $row['name']; 
    $email = $row['email']; 

    $countQuery = "SELECT COUNT(quotation.id) AS countRow FROM quotation WHERE company_name = '$companyName'"; 
    $countRes = mysqli_query($connect, $countQuery); 

    while($countRow = mysqli_fetch_assoc($countRes)){ 
     $count = $countRow['countRow']; 
    } 

    $quoteNum = $companyName . ' - ' .$count; 

    $sqlDate = $row['date']; 
    $date = strtotime($sqlDate); 
    $date = date('j F Y', $date); 

    $output .= " 
    <div class='row'> 
     <div class='col'> 
      <img class='logo' src='img/logo.jpg'> 
     </div> 
    </div> 

    <div class='row margin'> 
     <div class='col'> 
      <strong><h3>To:" . $companyName . "</h3> 
      Email:". $email."</strong> 
     </div> 
    </div> 

    <div class='row margin'> 
     <div class='col'>Quotation Number:". $quoteNum ."</div> 
    </div> 

    <div class='row'> 
     <div class='col'>Issue Date:". $date ."</div> 
    </div> 

    <div class='row margin'> 
     <div class='col'> 
      <p>Further to your enquiry, please find attached our quotation based on your discussed requirements. If you have any other queries, amendments or your requirements change please do not hesitate to contact me.</p> 
      <p>Thank you for the opportunity to quote on this project.</p> 
     </div> 
    </div> 

    <div class='row margin'> 
     <div class='col'> 
      <h1>Your quotation:</h1> 
      <table class='table table-bordered table-striped table-condensed table-responsive'> 
       <thead> 
        <tr> 
         <th> 
          Quote Details 
         </th> 
         <th class='float-right'> 
          Amount Quoted 
         </th> 
        </tr> 
       </thead> 
       <tbody>"; 

       $query = "SELECT * FROM quote_list, quotation WHERE quote_list.quote_id = quotation.id AND quote_list.quote_id = '$quote'"; 
       $result = mysqli_query($connect, $query); 
       $total = 0; 
       $discount = 0; 

       while($row = mysqli_fetch_assoc($result)){ 
        $title = $row['title']; 
        $price = $row['price']; 
        $discount = $row['discount']; 
        $total += $price; 

        $ouput .= 
        " 
        <tr> 
         <td>". 
          $title." 
         </td> 
         <td class='float-right'> 
          £".$price. 
          " 
         </td> 
        </tr>  
        "; 
       } 

       $final = $total-$discount; 



       $output .= " 
        <tr> 
         <td></td> 
         <td class='float-right'><em><strong>Total: </strong></em>£".$total."</br><em><strong>Discount: </strong></em>£".$discount."</br><em><strong>Quotation Price: </strong></em>£".$final."</td> 
        </tr> 
       </tbody> 
       </table> 
     </div> 
    </div> 

    <div class='row margin'> 
     <div class='col'> 
      <h1>Terms and Conditions</h1> 
      <h2>Our Quotation</h2> 
      <p>The above costs are inclusive of VAT, which will be charged where applicable. NI Flyers Print & Design quotations are valid for 30 days. Design costs are based on our understanding of your initial brief. Any additional amends/change to brief will be advised prior to invoicing. The right of design creations remains exclusively with NI Flyers Print & Design including economic and moral rights. Acceptance of this quotation includes a non-exclusive licence for the use of the works outlined above. An exclusive licence may be obtained at an additional cost.</p> 

      <h2>Additional photographic or illustration-based imagery</h2> 
      <p>Sourced photography or illustration this will be an additional cost to the design process. Imagery can be sourced either from stock library at £30.00 per image, or commissioned at cost, which will always be pre-quoted. If attendance to manage or art direct photoshoots is required this will be based on our standard hourly rate of £50 per hour. Additional photoshoot props or services will be charged accordingly.</p> 

     </div> 
    </div> 
"; 
    } 
     return $output; 
    } 

if(isset($_GET['viewPDF'])){ 
    require_once('tcpdf/tcpdf.php'); 
    $obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
    $obj_pdf->SetCreator(PDF_CREATOR); 
    $obj_pdf->SetTitle("Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP"); 
    $obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING); 
    $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); 
    $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); 
    $obj_pdf->SetDefaultMonospacedFont('helvetica'); 
    $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER); 
    $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT); 
    $obj_pdf->setPrintHeader(false); 
    $obj_pdf->setPrintFooter(false); 
    $obj_pdf->SetAutoPageBreak(TRUE, 10); 
    $obj_pdf->SetFont('helvetica', '', 11); 
    $obj_pdf->AddPage(); 
    $output .= fetchData(); 
    $obj_pdf->writeHTML($output); 
    $obj_pdf->Output('Quote_' . $quote . '.pdf', 'I'); 
} 
?> 

이 문서를 만들 때 잘못한 점이 있습니까? 나는 이것에 대한 튜토리얼과 문서를 읽고 있었고 전혀로드하지 않는 것처럼 보였다. 어떤 이유로 든 항상 빈 PDF 문서입니다. PHP를 통해 PDF를 동적으로 생성 할 때 사용할 간단한 도구가 있습니까?

답변

0

fetchData() 함수가 아무 것도 반환하지 않는 것처럼 보입니다.

내가 수정하면

는 :

$obj_pdf->writeHTML($output); 

읽으려면 ..

$obj_pdf->writeHTML('hello world'); 

다음이 올바르게 PDF로 보여줍니다. 이렇게하면 TCPDF 코드가 올바른지 확인할 수 있습니다.

코드를 로컬에서 실행할 때 fetchData() 함수가 반환 한 값을 얻지 못합니다. 분명히 나는 ​​데이터베이스 설정을 가지고 있지 않지만 이것은 당신의 문제가있는 곳이라고 생각한다.

함수 전체에 걸쳐 에코를 추가하고 반환되는 것으로 생각되는 내용이 반환되는지 확인하십시오.

+0

나는 데이터를 반향하려고 시도했지만 여전히 반환하지 않습니다. 뭔가 내 일을하지 않는 내 fetchData() 함수입니다. 감사. 내가 기대하는 페이지는 이것입니다 -> http://adonnelly759.students.cs.qub.ac.uk/niflyers/viewQuote.php?quoteID=19, 단지 PDF 형식 –

+0

그래, HTML이 좋아 보인다. 이는 확실히 쿼리 문제입니다. $ connect = $ mysqli_query ($ connect, $ query);에서 $ connect가 정의 된 것처럼 보이지 않습니다. 아마 그걸보아야 할 부분 일 겁니다. – Chris

관련 문제