我正在使用tcpdf根据数据库中的数据创建pdf文档。我试图根据他们的信息从我的客户那里生成合同。 pdf有效,但只为数据库中的第一个客户创建合同,而不是每个客户的页面。
这是我的代码(简化):
<?php
//This file create a pd contract agreement based on the client's detail.
// Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');
require_once('connect.php');
$stmm= $conn->prepare("SELECT fields from table
WHERE MyConditions");
$stmm->execute();
while($resultst = $stmm-> fetch()){
//list of myvariables to use is here
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Mycompany');
$pdf->SetTitle('Client Agreement');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' ', PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
/*
NOTES:
- To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
- To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
- To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
*/
// set certificate file
$certificate = 'file://data/cert/tcpdf.crt';
// set additional information
$info = array(
'Name' => 'My info',
'Location' => 'SA',
'Reason' => 'Advertising Agreement',
'ContactInfo' => 'http://www.mydomain.co.za',
) ;
// set document signature
$pdf->setSignature($certificate, $certificate, 'tcpdfdemo', '', 2, $info);
// set font
$pdf->SetFont('helvetica', '', 7);
// add a page
$pdf->AddPage();
// print a line of text
$text = "PDF layout for each client with their detail here as per SELECT query";
$pdf->writeHTML($text, true, 0, true, 0);
// define active area for signature appearance
$pdf->setSignatureAppearance(180, 60, 15, 15);
// *** set an empty signature appearance ***
$pdf->addEmptySignatureAppearance(180, 80, 15, 15);
//Close and output PDF document
$pdf->Output('client_contracts.pdf', 'D');
//============================================================+
// END OF FILE
//============================================================+
}
任何Idead
答案 0 :(得分:2)
但只为数据库中的第一个客户创建合同,而不是为每个客户创建一个合同
那么这与你在每次迭代循环中创建一个新的TCPDF对象有关。
另外,你也在循环中调用它的输出方法 - 这两个动作都应放在外面循环中。