2016-09-12 501 views
0

我已經閱讀並嘗試了多個教程,並且每次碰到一個塊時我都無法通過。來自MySQL數據庫的PHP到PDF

我有59場數據庫目前,我有一個表格填寫代理商在其銷售到每一天。每天從15-50個銷售額中獲取。目前我有一個HTML模板,其中填充了數據庫字段的變量,並通過一個循環顯示所有這些銷售在一個單獨的表中的每個銷售,然後我打印到每個銷售表在不同的頁面上。你可以想象這是每天的勞動密集程度。

我現在想給每個循環運行,這將是一個銷售到自己的PDF格式的記錄,並根據變量從數據庫表中的混合命名轉換PDF文件。

我不能讓PHP變量在PDF生成器中工作。我可以在表格中顯示數據,並從我的模板生成pdf,但只要我添加了php mysql數據庫字段變量,我總是收到關於他的變量的錯誤,並且PDF生成器失敗。

這裏是我做了什麼:

TCPDF:

<?php 

$pdf = new TCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false); 

// set document information 
$pdf->SetCreator(PDF_CREATOR); 
$pdf->SetAuthor('Asimegroup'); 
$pdf->SetTitle('Asime Loan App'); 
$pdf->SetSubject('Asime Loan Application Form'); 

// set default monospaced font 
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 

// set margins 
$pdf->SetMargins('5', '0', '0'); 


// set auto page breaks 
$pdf->SetAutoPageBreak(TRUE, '0'); 

// set image scale factor 
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

// set some language-dependent strings (optional) 
if (@file_exists(dirname(__FILE__).'examples\lang\eng.php')) { 
    require_once(dirname(__FILE__).'examples\lang\eng.php'); 
    $pdf->setLanguageArray($l); 
} 

// set font 
$pdf->SetFont('helvetica', '', 10); 

// add a page 
$pdf->AddPage(); 

// Just including 2 rows here for example sake. 

$html = ' 
<body> 
<br/> 
<table> 
    <tr> 
    <td height="40px"></td> 
    </tr> 
    <tr> 
    <td align="right" class="heading" width="175px">Personal</td> 
    <td align="left" class="heading" width="210px">Details:</td> 
    <td width="10px"></td> 
    <td align="right" class="heading" width="150px">Relative not</td> 
    <td align="left" class="heading" width="170px">living with you:</td> 
    <td width="10px"></td> 
    <td align="right" class="heading" width="185px">Expenses:</td> 
    <td width="100px"></td> 
    </tr> 
    <tr> 
    <td class="subheading">SA Citizen?:</td> 
    <td class="data"></td> 
    <td></td> 
    <td class="subheading">Name:</td> 
    <td class="data"></td> 
    <td></td> 
    <td class="subheading">Bond/Rent:</td> 
    <td class="data"></td> 
    </tr> 
    </table>'; 


    // output the HTML content 
    $pdf->writeHTML($html, true, false, true, false, ''); 

    // reset pointer to the last page 
    $pdf->lastPage(); 

    // --------------------------------------------------------- 

    //Close and output PDF document 
    $pdf->Output('example_061.pdf', 'F'); 

?> 

PHP數據庫循環:

<?php 
// server info 
$server = 'localhost'; 
$user = 'root'; 
$pass = ''; 
$db = 'debtdb'; 


// connect to the database 
$conn = new mysqli($server, $user, $pass, $db); 

// show errors (remove this line if on a live site) 
mysqli_report(MYSQLI_REPORT_ERROR); 

if(!isset($_POST['agentname'])) { 
$search_sql="SELECT * FROM daily"; 
$search_query=mysqli_query($conn, $search_sql); 
$search_rs=mysqli_fetch_assoc($search_query); 
} 

if(!empty($search_query)) { 

    while($search_rs = mysqli_fetch_array($search_query)) { 
?> 

<body> 

// Just including 2 rows here for example sake. 

<table> 
    <tr> 
    <td align="right" class="heading" width="175px">Personal</td> 
    <td align="left" class="heading" width="210px">Details:</td> 
    <td width="10px"></td> 
    <td align="right" class="heading" width="150px">Relative not</td> 
    <td align="left" class="heading" width="170px">living with you:</td> 
    <td width="10px"></td> 
    <td align="right" class="heading" width="185px">Expenses:</td> 
    <td width="100px"></td> 
    </tr> 
    <tr> 
    <td class="subheading">SA Citizen?:</td> 
    <td class="data"><?php echo $search_rs["per_citizen"]; ?></td> 
    <td></td> 
    <td class="subheading">Name:</td> 
    <td class="data"><?php echo $search_rs["rel_name"]; ?></td> 
    <td></td> 
    <td class="subheading">Bond/Rent:</td> 
    <td class="data">R&nbsp;<?php echo $search_rs["exp_bondrent"]; ?></td> 
    </tr> 
    </table> 
    <?php 
    } 
    } 
    ?> 

每所以這兩個例子的作品。以下是我卡住​​了,當我合併2:

<?php 
// server info 
$server = 'localhost'; 
$user = 'root'; 
$pass = ''; 
$db = 'debtdb'; 


// connect to the database 
$conn = new mysqli($server, $user, $pass, $db); 

// show errors (remove this line if on a live site) 
mysqli_report(MYSQLI_REPORT_ERROR); 

if(!isset($_POST['agentname'])) { 
$search_sql="SELECT * FROM daily"; 
$search_query=mysqli_query($conn, $search_sql); 
$search_rs=mysqli_fetch_assoc($search_query); 
} 

if(!empty($search_query)) { 

    while($search_rs = mysqli_fetch_array($search_query)) { 


$pdf = new TCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false); 

// set document information 
$pdf->SetCreator(PDF_CREATOR); 
$pdf->SetAuthor('Asimegroup'); 
$pdf->SetTitle('Asime Loan App'); 
$pdf->SetSubject('Asime Loan Application Form'); 

// set default monospaced font 
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 

// set margins 
$pdf->SetMargins('5', '0', '0'); 


// set auto page breaks 
$pdf->SetAutoPageBreak(TRUE, '0'); 

// set image scale factor 
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

// set some language-dependent strings (optional) 
if (@file_exists(dirname(__FILE__).'examples\lang\eng.php')) { 
    require_once(dirname(__FILE__).'examples\lang\eng.php'); 
    $pdf->setLanguageArray($l); 
} 

// set font 
$pdf->SetFont('helvetica', '', 10); 

// add a page 
$pdf->AddPage(); 

// Just including 2 rows here for example sake. 

$html = ' 
<table> 
    <tr> 
    <td align="right" class="heading" width="175px">Personal</td> 
    <td align="left" class="heading" width="210px">Details:</td> 
    <td width="10px"></td> 
    <td align="right" class="heading" width="150px">Relative not</td> 
    <td align="left" class="heading" width="170px">living with you:</td> 
    <td width="10px"></td> 
    <td align="right" class="heading" width="185px">Expenses:</td> 
    <td width="100px"></td> 
    </tr> 
    <tr> 
    <td class="subheading">SA Citizen?:</td> 
    <td class="data"><?php echo $search_rs["per_citizen"]; ?></td> 
    <td></td> 
    <td class="subheading">Name:</td> 
    <td class="data"><?php echo $search_rs["rel_name"]; ?></td> 
    <td></td> 
    <td class="subheading">Bond/Rent:</td> 
    <td class="data">R&nbsp;<?php echo $search_rs["exp_bondrent"]; ?></td> 
    </tr> 
    </table>'; 

    // output the HTML content 
    $pdf->writeHTML($html, true, false, true, false, ''); 

    // reset pointer to the last page 
    $pdf->lastPage(); 

    // --------------------------------------------------------- 

    //Close and output PDF document 
    $pdf->Output('<?php echo $search_rs["agentname"]; ?> - <?php echo $search_rs["dateofsale"]; ?>.pdf', 'F'); 

    } 
    } 
    ?> 

給我:

localhost的頁面無法正常工作

本地主機是目前無法處理此請求。 HTTP錯誤500

+0

請問您可以修改問題的標題嗎? – Bikku

+0

您試圖在mysql數據的循環內創建pdf。 '$ pdf = new ..'shoube在循環之前, – nogad

+0

順便說一句,你純粹的html例子也是錯誤的;它可能看起來不錯,但如果你在瀏覽器中閱讀源代碼,你會看到你正在創建多個標籤,而應該只有1. – Saskia

回答

0

你已經把這個行:

$pdf = new TCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false); 

在你的mysql while循環,這意味着你正在創建的每個SQL的結果,這是probebly不是你想要的新的PDF。瀏覽器會投訴,導致它無法處理多個pdf文件的請求。把東西需要只運行一次,如創建的PDF變量和作者的東西等,while循環以上......

+0

除了這個之外還有更多的應該在循環之外 – nogad

+0

這個計劃是創建多個pdf。我需要在我的html模板中創建每個記錄的pdf文件,這就是爲什麼我想要扼殺循環中的所有內容,以便每次循環執行時都會生成pdf。我不希望整個循環的輸出都在一個pdf中。 – hardbouw

+0

當你給他多個PDF文件時,你對瀏覽器有什麼期望?你有沒有看過瀏覽器顯示多個PDF文件?不,瀏覽器只提供一個文件。在這種情況下,請將PDF文件安全地保存到磁盤併爲瀏覽器提供一個包含多個PDF文件鏈接的HTML頁面,以便用戶可以通過單擊該鏈接來選擇PDF文件。 – Saskia

0

我經常發現最容易生成PDF時,簡直就是生成HTML(你已經做了)然後將其提供給像wkhtml2pdf這樣的工具,將其轉換爲PDF。

你必須添加的唯一事情是一些CSS讓每一個表出現一個新的頁面上;

table { 
    page-break-after: always; 
} 

現在您可以將URL傳遞給命令;

wkhtml2pdf http://example.com/reports.php reports.pdf 

或者先將它保存到臨時文件,然後將其傳遞給命令。

+0

謝謝,我現在看到這個命令做了什麼,添加它,目視我沒有看到一個變化,但是當打印帶有循環記錄的頁面時,它將它分成不同的頁面。你說我必須通過URL打印PDF格式的打印機,我的電腦上的網址是localhost/allclients.php – hardbouw

+0

我現在在TCPDF: $ html = file_get_contents('../../ allclients.php') ; 它只顯示沒有邊框的表格模板。將嘗試你建議的應用程序 – hardbouw

+0

'wkhtml2pdf http:// localhost/allclients.php' –

0

感謝您的建議和幫助到目前爲止形成每個人。我現在可以使用wkhtmltopdf將整個表格輸出爲一個PDF格式。

我現在需要修改此代碼爲我的第二個解決方案第二步工作分離成單獨的文件頁面。我發現stackexchange這一部分現在,看起來差不多吧,只是需要能夠與我的MySQL數據庫G642.44各級它:

PHP TCPDF Create Multiple PDFS With One Command

require_once('eng.php'); 
require_once('tcpdf.php'); 
$pageLayout = array(750, 800); 

$content=Array(
'<html><body>Document A</body></html>', 
'<html><body>Document B</body></html>', 
'<html><body>Document C</body></html>' 
); 

foreach($content as $i=>$html){ 
    $pdf =new TCPDF('P', 'pt', $pageLayout, true, 'UTF-8', false); 
    $pdf->SetCreator(PDF_CREATOR); 
    $pdf->AddPage(); 
    $pdf->writeHTML($html, true, false, true, false, ''); 
    $pdf->lastPage(); 
    $pdf->Output('filename_' . $i . '.pdf', 'D'); 
} 

我需要生成PDF文件使用一個獨特的名字文件的每個,不要在瀏覽器中顯示它們。

我需要爲例子做這樣的事情:

$pdf->Output('$clientname-$clientsurname-$dateosfale.pdf', 'F'); 

然後我就可以基於同樣的模式鏈接和在表中顯示它們能夠被下載。

糾正我,如果我錯了,這裏是我的PHP循環和tcpdf生成代碼的基本結構。

我正在考慮這樣做。當我用wkhtmltopdf一次生成所有內容時,我需要能夠對每個頁面執行相同的操作,因此我在循環中添加了與pdf生成相關的所有內容,以便爲每個循環創建一個pdf,但它不那麼容易因爲我注意到:

<?php 
// server info 
$server = 'localhost'; 
$user = 'root'; 
$pass = ''; 
$db = 'debtdb'; 


// connect to the database 
$conn = new mysqli($server, $user, $pass, $db); 

// show errors (remove this line if on a live site) 
mysqli_report(MYSQLI_REPORT_ERROR); 

if(!isset($_POST['agentname'])) { 
$search_sql="SELECT * FROM daily"; 
$search_query=mysqli_query($conn, $search_sql); 
$search_rs=mysqli_fetch_assoc($search_query); 
} 

if(!empty($search_query)) { 

    while($search_rs = mysqli_fetch_array($search_query)) { 

require_once('tcpdf_include.php'); 

// create new PDF document 
$pdf = new TCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false); 

// set document information 
$pdf->SetCreator(PDF_CREATOR); 
$pdf->SetAuthor('Asimegroup'); 
$pdf->SetTitle('Asime Loan App'); 
$pdf->SetSubject('Asime Loan Application Form'); 

// set default monospaced font 
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 

// set margins 
$pdf->SetMargins('5', '0', '0'); 

// set auto page breaks 
$pdf->SetAutoPageBreak(TRUE, '0'); 

// set image scale factor 
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

// set some language-dependent strings (optional) 
if (@file_exists(dirname(__FILE__).'examples\lang\eng.php')) { 
    require_once(dirname(__FILE__).'examples\lang\eng.php'); 
    $pdf->setLanguageArray($l); 
} 

// set font 
$pdf->SetFont('helvetica', '', 10); 

// add a page 
$pdf->AddPage(); 

$html = ' 
<table> 
    <tr> 
    <td align="right" class="heading" width="175px">Personal</td> 
    <td align="left" class="heading" width="210px">Details:</td> 
    <td width="10px"></td> 
    <td align="right" class="heading" width="150px">Relative not</td> 
    <td align="left" class="heading" width="170px">living with you:</td> 
    <td width="10px"></td> 
    <td align="right" class="heading" width="185px">Expenses:</td> 
    <td width="100px"></td> 
    </tr> 
    <tr> 
    <td class="subheading">SA Citizen?:</td> 
    <td class="data"><?php echo $search_rs["per_citizen"]; ?></td> 
    <td></td> 
    <td class="subheading">Name:</td> 
    <td class="data"><?php echo $search_rs["rel_name"]; ?></td> 
    <td></td> 
    <td class="subheading">Bond/Rent:</td> 
    <td class="data">R&nbsp;<?php echo $search_rs["exp_bondrent"]; ?></td> 
    </tr> 
    <tr> 
    <td class="subheading">Name:</td> 
    <td class="data"><?php echo $search_rs["per_firstname"]; ?></td> 
    <td></td> 
    <td class="subheading">Surname:</td> 
    <td class="data"><?php echo $search_rs["rel_surn"]; ?></td> 
    <td></td> 
    <td class="subheading">Rates, Water, Electricity:</td> 
    <td class="data">R&nbsp;<?php echo $search_rs["exp_rates"]; ?></td> 
    </tr> 
</table>'; 

// output the HTML content 
$pdf->writeHTML($html, true, false, true, false, ''); 

// reset pointer to the last page 
$pdf->lastPage(); 

//Close and output PDF document 
$pdf->Output(<?php echo $search_rs["per_firstname"]; ?>-<?php echo $search_rs["per_lastname"]; ?>-<?php echo $search_rs["dateofsale"]; ?>.pdf, 'F'); 

    } 
} 
?>