【发布时间】:2016-04-14 09:36:44
【问题描述】:
我必须将一个导出文件转换为 pdf。为此我使用的是 PDFcrowed 但是如果我使用 convertFile 和 convertURL 就会出现一个问题,如果我有 php 文件的传递路径,它就可以工作并转换为 pdf。它会给出一个错误。 invoices.php
<?php
require 'pdfcrowd.php';
try
{
// create an API client instance
$client = new Pdfcrowd("priyankaahire", "b50ca6e682a7194f24bf2081470d074f");
$pdf = $client->convertFile('data.php');
// set HTTP response headers
header("Content-Type: application/pdf");
header("Cache-Control: max-age=0");
header("Accept-Ranges: none");
header("Content-Disposition: attachment; filename=\"google_com.pdf\"");
// send the generated PDF
echo $pdf;
}
catch(PdfcrowdException $why)
{
echo "Pdfcrowd Error: " . $why;
}
?>
data.php
<table border="1">
<tr>
<th>NO.</th>
<!-- <th>MAWBNO</th>-->
<th>HAWBNO</th>
<th>Consignee Name</th>
<th>Consignee Address</th>
<th>Sender Name</th>
<th>Sender Address</th>
</tr>
<?php
//connection to mysql
mysql_connect("localhost", "root", ""); //server , username , password
mysql_select_db("shepherddb");
//query get data
$sql = mysql_query("SELECT ship_hawbno,cust_fname,cust_street from shipment,customers
where shipment.ship_consignee_id=customers.cust_id or shipment.ship_shipper_id=customers.cust_id and shipment.ship_id=2");
$no = 1;
while($data = mysql_fetch_array($sql)){
echo '
<tr>
<td>'.$no.'</td>
<td>'.$data['ship_hawbno'].'</td>
<td>'.$data['cust_fname'].'</td>
<td>'.$data['cust_street'].'</td>
<td>'.$data['cust_fname'].'</td>
<td>'.$data['cust_street'].'</td>
</tr>
';
$no++;
}
?>
</table>
【问题讨论】:
-
你到底有什么错误?我不知道 php pdfcrowd 是如何工作的,但我怀疑
->convertFile('data.php');不是一个有效的请求。尝试使用文件的 HTTP url,而不是本地文件名。 -
我可以将 localhost 路径传递给 convertFile('localhost/shepherdlogistics_v1.0/backend/views/shipment/…)
-
Pdfcrowd Error: convertFile(): 'localhost/shepherdlogistics_v1.0/backend/views/shipment/…' not found 可能原因: 1. 文件丢失。 2. 你拼错了文件名。 3. 您使用相对文件路径(例如 'index.html'),但当前工作目录与您预期的不同:'C:\xampp\htdocs\shepherdlogistics_v1.0\backend\views\shipment' 通常,它是使用绝对文件路径而不是相对路径更安全。
-
通过本地主机路径后出现错误
-
我更正自己:
->convertFile仅适用于本地文件。您必须传递有效的文件路径。尝试使用绝对文件路径(没有 HTTP,我很抱歉)。注意转换时会忽略php代码。
标签: php pdf-generation