2010-08-18 115 views
3

一個PDF這是我的代碼,這將從數據庫獲取數據,然後將其存儲在表.pdf文件:創建的輸出怪異字符

<?php 

// test the table functions 
error_reporting(E_ALL); 
include('includes/pdf/class.ezpdf.php'); 
    //echo 'AAA-1-'; 
    $pdf = new Cezpdf(); 
    //$pdf->selectFont('./fonts/Helvetica'); 

    //connect to db 
    session_start(); 
    require_once('connect/ticketivity_connect.php'); 
    //echo 'AAA-2-'; 


    //get customer details 
    $sql_cust = "SELECT * FROM customers where customer_id = '".$_GET['customerid']."'"; 
    $res_cust = mysql_query($sql_cust); 
    $num_cust = mysql_num_rows($res_cust); 
    $row_cust = mysql_fetch_assoc($res_cust); 

    //display customer details 
    $db_data[] = array('first' => 'Customer Name:', 'second' => $row_cust['cust_name']); 
    $db_data[] = array('first' => 'Phone Number:', 'second' => $row_cust['cust_phone']); 
    $db_data[] = array('first' => 'Address:', 'second' => $row_cust['cust_address']); 
    $db_data[] = array('first' => '>Customer Ref/ Room Number:', 'second' => $row_cust['cust_ref']); 
    //echo 'AAA-3-'; 
    //get order 
    $sql_orderinfo= "SELECT * FROM orders where customer_id = '".$_GET['customerid']."'"; 
    $res_orderinfo = mysql_query($sql_orderinfo); 
    $num_orderid = mysql_num_rows($res_orderinfo); 
    $row_orderid = mysql_fetch_assoc($res_orderinfo); 
    //echo 'AAA-4-'; 
    //display order id 
    $orderid_len = strlen($row_orderid['order_id']); 
    if($orderid_len == 1) { 
    //echo 'AAA1--'; 
    $db_data[] = array('first' => 'Order ID:', 'second' => '00000'.$row_orderid['order_id']); 
    } else if ($orderid_len == 2){ 
    //echo 'AAA2--'; 
    $db_data[] = array('first' => 'Order ID:', 'second' => '0000'.$row_orderid['order_id']); 
    } else if ($orderid_len == 3){ 
    //echo 'AAA3--'; 
    $db_data[] = array('first' => 'Order ID:', 'second' => '000'.$row_orderid['order_id']); 
    } else if ($orderid_len == 4){ 
    //echo 'AAA4--'; 
    $db_data[] = array('first' => 'Order ID:', 'second' => '00'.$row_orderid['order_id']); 
    } else if ($orderid_len == 5){ 
    //echo 'AAA5--'; 
    $db_data[] = array('first' => 'Order ID:', 'second' => '0'.$row_orderid['order_id']); 
    } else if ($orderid_len == 6){ 
    //echo 'AAA6--'; 
    $db_data[] = array('first' => 'Order ID:', 'second' => $row_orderid['order_id']); 
    } 

    //table columns 
    $col_names = array(
    'first' => 'first', 
    'second' => 'second', 
); 
    //echo 'AAA-5-'; 
    $table_title = 'Customer Details'; 
    //echo 'AAA-6-'; 
    $pdf->ezTable($db_data, $col_names, $table_title, array('width'=>550)); 
    if (isset($d) && $d){ 
    //echo 'AAA-if'; 
    $pdfcode = $pdf->output(1); 
    $pdfcode = str_replace("\n","\n<br>", $pdfcode); 
    echo '<html><body>'; 
    echo trim($pdfcode); 
    echo '</body></html>'; 
    } else { 
    //echo 'AAA-else'; 
    $pdf->ezStream(); 
    } 

?> 

輸出:

%PDF-1.3 %���� 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R >> endobj 2 0 obj << /Type /Outlines /Count 0 >> endobj 3 0 obj << /Type /Pages /Kids [6 0 R ] /Count 1 /Resources << /ProcSet 4 0 R >> /MediaBox [0.000 0.000 595.280 841.890] >> endobj 4 0 obj [/PDF /Text ] endobj 5 0 obj << /Creator (R and OS php pdf writer, http://www.ros.co.nz) /CreationDate (D:20100817) >> endobj 6 0 obj << /Type /Page /Parent 3 0 R /Contents [ 7 0 R 8 0 R ] >> endobj 7 0 obj << /Filter /FlateDecode /Length 120 >> stream x��2�[email protected]&�ҹ�,�\����`di�gfb�`ah�gai�����`h�d�)(h8����)���$f�k*�d)��pb1h����8sK�qF�@����[email protected]��m\����-� endstream endobj 8 0 obj << /Filter /FlateDecode /Length 94 >> stream x��2�[email protected]&�ܹʹ�,���L,��,, r`"�@$��Qi����Jct�03�|C4��(" �7����2D�a����T4z endstream endobj xref 0 9 0000000000 65535 f 0000000015 00000 n 0000000080 00000 n 0000000126 00000 n 0000000257 00000 n 0000000286 00000 n 0000000391 00000 n 0000000464 00000 n 0000000657 00000 n trailer << /Size 9 /Root 1 0 R /Info 5 0 R >> startxref 823 %%EOF 

做什麼我需要做/添加?

+0

順便說一句,我使用http://www.ros.co.nz/pdf/作爲參考。我下載了zip文件:pdfClassesAndFonts_009e。 – anonymous123 2010-08-18 02:28:45

+1

您需要查看PDF查看器中的輸出,看起來就是PDF文件的實際內容。 – Thanatos 2010-08-18 02:31:38

+0

如果您可以重新格式化該問題以便代碼清晰可讀,那將會很有幫助。 – 2010-08-18 03:12:28

回答

1

這個問題不是很清楚,但我假設你正在編寫一個Web應用程序,並且你正試圖將PDF文檔提供給客戶端。

首先,您需要設置一個頭,讓客戶知道它應該指望PDF數據:

header('Content-type: application/pdf'); 

這將導致大多數瀏覽器呈現在瀏覽器窗口中的PDF。如果你寧願讓瀏覽器提供PDF的下載,這樣做除了上面:

header('Content-Disposition: attachment; filename="filename.pdf"'); 

你可以設置你代替filename.pdf喜歡的任何文件名。

任何輸出被寫入之前,請撥打header()這些電話很重要。

順便說一下,我想$d是一個調試標誌,但我不認爲這是一個非常好的主意,做這些字符串操作時,你正在設置。您可能最終會破壞PDF數據。

+0

謝謝!這有效嗎? $ DB_DATA =陣列( \t \t \t \t陣列( '第一'=> '客戶名稱:', '第二'=> $ row_cust [ 'CUST_NAME']) \t \t \t \t,陣列( '第一'= >'電話號碼','second'=> $ row_cust ['cust_phone']) \t \t \t \t,array('first'=>'Address','second'=> $ row_cust ['cust_address'] ) \t \t \t \t,array('first'=>'Customer Ref/Room Number:','second'=> $ row_cust ['cust_ref']) \t \t \t \t); 其中例如($ row_cust ['cust_ref'])是來自DB的數據。 – anonymous123 2010-08-18 06:53:44

+0

我添加了標題爲你說的,但我仍然得到pdf -3 0 0錯誤 – StrawhatLuffy 2013-08-06 10:25:51

+0

你能解決這個問題嗎?即時通訊在移動鉻上得到相同的結果。謝謝 – Delavega 2016-08-30 18:10:28