2017-03-27 47 views
0

私は、データベースをチェックし、アカウントの開始日が特定の日付になっている顧客の各行を返すスクリプトを作成しています。顧客が指定された日付に作成された場合、顧客の行が自分の関数に渡され、顧客の情報からデータベースにPDFが作成されます。dompdfで複数のPDFファイルを作成する

問題

私は顧客ごとに1つのPDFを作成すると思っている唯一の方法は、データをループしていると、PDFを作成します。そうするとき、私には1つの問題があります。

最初のユーザーのPDFのみが作成されます。 2番目のものには何も起こりません。

Unable to stream PDF: headers already sent

ブラウザと最初のPDFダウンロードで表示されます。しかし、テスト目的のために、データベースには2つの顧客が作成される必要があります。私はブラウザにデータをエコーアウトし、両方の顧客が表示されます。だから私はデータが存在し、実行時にそこにあることを知っています。

// create instance of customer's that need to be created 
    $a = new SelectTempOne; 
    // obtain response from customers in database that have met timing requirement 
    $response = $a->selectUserForOne(); 
     // loop through each row in the results and handle then pass to the pdf creation string 
    foreach($response as $i => $row) { 
    // statically typing template string to render email address of user 
    $temp1 = ' 
     <!DOCTYPE html> 
    <html> 

    <head> 
     <title>Template 1</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <link rel="stylesheet" type="text/css" media="screen" href="pdf-templates/temp1/temp1.css" /> 
    </head> 

    <body> 
     <div id="pdf_header"> 
      <table> 
       <tr> 
        <td>'.$response[$i]['email'].'</td> 
       </tr> 
      </table> 
     </div> 
    </body> 

    </html> 
    '; 

    // pass html to dompdf object 
    $dompdf->loadHtml($temp1); 

    // set option for the paper size and orientation 
    $dompdf->setPaper('A4', 'letter'); 

    // Render the HTML as PDF 
    $dompdf->render(); 

    //save the file to the server 
    $output = $dompdf->output(); 
    file_put_contents('pdf/'.$response[$i]['email'].'.pdf', $output); 


    // Output the generated PDF to Browser 
    $dompdf->stream(); 
    exit; 
    } // end of the for loop 
    ?> 

私は、顧客の電子メールを追加する場所この行は、私が作成できるように何をする必要がありますどのような

<td>'.$response[$i]['email'].'</td> 

質問

ですデータベースから返された行をループする際のPDFの動的な量?

* UPDATE *

Fatal error: Uncaught Dompdf\Exception: No block-level parent found. Not good. in /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/Positioner/Inline.php:45 Stack trace: #0 /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(872): Dompdf\Positioner\Inline->position(Object(Dompdf\FrameDecorator\Inline))

1 /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameReflower/Inline.php(51):

Dompdf\FrameDecorator\AbstractFrameDecorator->position() #2 /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(893): Dompdf\FrameReflower\Inline->reflow(NULL) #3 /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameReflower/Page.php(141): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow() #4 /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/FrameDecorator/AbstractFrameDecorator.php(893): Dompdf\FrameReflower\Page->reflow(NULL) #5 /Users/wuno/Dropbox/google/devop in /Users/wuno/Dropbox/google/devops/htdocs/tracker/login/dompdf/src/Positioner/Inline.php on line 45

答えて

1

Webブラウザが文書を要求することによって動作します。ユーザーに複数のドキュメントを提供できることは期待できません。あなたは一度$dompdf->stream();に電話することができます。それだけです。あなたのオプション:個々のリンクを含むリストページを動的に作成したり、複数のページのPDFを作成したり、PDFを一緒に圧縮したりできます。

編集:あなたのコメントに基づいて、実際にPDFを表示する必要はありません。さらに、私はもう一度見て、ループ内にexitステートメントがあります。それは複数回実行されることはありません。また、コードに大きなテキストブロックがある場合はheredoc stringsを使用することをお勧めします。

<?php 
// create instance of customer's that need to be created 
$a = new SelectTempOne; 
// obtain response from customers in database that have met timing requirement 
$response = $a->selectUserForOne(); 
// loop through each row in the results and handle then pass to the pdf creation string 
foreach($response as $i => $row) { 

    // statically typing template string to render email address of user 
    $temp1 = <<< HTML 
<!DOCTYPE html> 
<html> 

<head> 
    <title>Template 1</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <link rel="stylesheet" type="text/css" media="screen" href="pdf-templates/temp1/temp1.css" /> 
</head> 

<body> 
    <div id="pdf_header"> 
     <table> 
      <tr> 
       <td>$row[email]</td> 
      </tr> 
     </table> 
    </div> 
</body> 

</html> 
HTML; 

    // pass html to dompdf object 
    $dompdf->loadHtml($temp1); 

    // set option for the paper size and orientation 
    $dompdf->setPaper('A4', 'letter'); 

    // Render the HTML as PDF 
    $dompdf->render(); 

    //save the file to the server 
    $output = $dompdf->output(); 
    file_put_contents("pdf/$row[email].pdf", $output); 

} // end of the for loop 
?> 
+0

私はちょうど私の質問を編集していたので、私はそれが混乱することに気づいた。実際の問題は、最初のPDFを作成することだけです。私はサーバー上に複数のpdfを作成したい。このスクリプトはcronジョブで実行され、ユーザーはブラウザで表示されません。私はテスト中にそれをやっていました。そして、助けてくれてありがとう/スクリプトが実行されるとき、それはデータベースの特定の日にサインアップしたすべての顧客のためのpdfを作成すべきです。それから私は後でそれらのpdfへのリンクを管理者に示します。 – wuno

+0

[編集]を参照してください。あなたのループには「exit」があります。 – miken32

+0

ありがとうございました。一度私はこの致命的なエラーを今すぐ更新 – wuno

関連する問題