【问题标题】:created a PDF that outputs weird characters创建了一个输出奇怪字符的 PDF
【发布时间】:2013-08-06 21:03:07
【问题描述】:

这是我的代码,它将从数据库中获取数据,然后将其存储在 .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�300P@&�ҹ�,�\����`di�gfb�`ah�gai�����`h�d�)(h8����)���$f�k*�d)��pb1h����8sK�qF�@����F@��m\����-� endstream endobj 8 0 obj << /Filter /FlateDecode /Length 94 >> stream x��2�300P@&�ܹʹ�,���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 

我需要做什么/添加什么?

【问题讨论】:

  • 顺便说一下,我使用ros.co.nz/pdf 作为参考。我下载了 zip 文件:pdfClassesAndFonts_009e。
  • 您需要在 PDF 查看器中查看输出,从外观上看,这就是 PDF 文件的实际内容。
  • 如果您可以重新格式化此问题以使代码清晰易读,将会很有帮助。
  • 这是否有效:$db_data = array(array('first'=> 'Customer Name:','second'=> $row_cust['cust_name']) ,array('first'= > '电话号码:','second'=> $row_cust['cust_phone']) ,array('first'=> '地址:','second'=> $row_cust['cust_address']) ,array(' first'=> '客户编号/房间号:','second'=>$row_cust['cust_ref']) );

标签: pdf-generation


【解决方案1】:

这个问题不是很清楚,但我假设您正在编写一个 Web 应用程序,并且您正在尝试将 PDF 文档提供给客户端。

首先,您需要设置一个标头,让客户端知道它应该期待 PDF 数据:

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

这将导致大多数浏览器在浏览器窗口中呈现 PDF。如果您希望浏览器提供 PDF 下载,请在上述之外执行此操作:

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

您可以设置任何您喜欢的文件名来代替filename.pdf

在写入任何输出之前,对header()进行这些调用很重要。

顺便说一句,我猜$d 是一个调试标志,但我认为在设置时执行您正在执行的那些字符串操作不是一个好主意。您最终可能会损坏 PDF 数据。

【讨论】:

  • 谢谢!这是有效的吗? $db_data = array(array('first'=> '客户姓名:','second'=> $row_cust['cust_name']) ,array('first'=> '电话号码:','second'=> $row_cust['cust_phone']) ,array('first'=> '地址:','second'=> $row_cust['cust_address']) ,array('first'=> '客户编号/房间号:' ,'second'=>$row_cust['cust_ref']) );其中 Ex .($row_cust['cust_ref']) 是来自数据库的数据。
  • 我按照你说的添加了标题,但我仍然收到 pdf -3 0 0 错误
  • 你能解决这个问题吗?我在移动 chrome 上得到相同的结果。谢谢
  • 如果上面的答案对您没有帮助,您要么做错了其他事情,要么您没有正确阅读答案。我无法读懂你的想法并弄清楚是哪一个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 2021-04-23
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 2018-10-29
相关资源
最近更新 更多