【问题标题】:Pdfcrowd Error: [26] couldn't open file "data.php"Pdfcrowd 错误:[26] 无法打开文件“data.php”
【发布时间】: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 是如何工作的,但我怀疑 -&gt;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' 通常,它是使用绝对文件路径而不是相对路径更安全。
  • 通过本地主机路径后出现错误
  • 我更正自己:-&gt;convertFile 仅适用于本地文件。您必须传递有效的文件路径。尝试使用绝对文件路径(没有 HTTP,我很抱歉)。注意转换时会忽略php代码。

标签: php pdf-generation


【解决方案1】:

使用-&gt;convertFile() 方法,您必须传递一个本地 HTML 文件路径。您的错误很明显:

  1. 文件丢失。
  2. 您拼错了文件名。
  3. 您使用的是相对文件路径(例如“index.html”),但当前工作
    目录与您预期的不同:'${cwd}'
    通常,使用绝对文件路径而不是相对路径更安全。

在您的具体情况下,data.php 不在执行脚本的同一目录中。请改用绝对文件路径


请注意:

使用此方法,您将原始 php 文件发送到 PDFcrowd(您的 php 代码可见,包括最终敏感的数据)。转换过程将忽略所有 php 代码,并将仅转换页面中的纯 HTML

换句话说,如果你的test.php页面是这样的:

<html>
    <head><title>Test</title></head>
    <body>
        <div style="border:1px solid black;">Hello <?php echo 'World'; ?></div>
    </body>
</html>

在您的浏览器中,您会看到:

┌─────────────┐
│ Hello World │
└─────────────┘

但是,在-&gt;convertFile( '/Absolute/Path/to/test.php' ) 之后,您转换后的 pdf 文件将如下所示:

┌───────┐
│ Hello │
└───────┘

如果你想转换处理后的 php 文件,你可以试试这样:

$html = file_get_contents( 'http://localhost/path/to/your/test.php' );
file_put_contents( '/Absolute/Path/To/tempfile.html', $html );
$pdf = $client->convertFile( '/Absolute/Path/To/tempfile.html' );

换句话说,您必须先检索 url,然后将其保存到文件,然后再转换保存的文件。

您也可以使用-&gt;convertURI() 代替-&gt;convertFile()

$pdf = $client->convertURI( 'http://www.example.com/path/to/your/test.php' );

在这种情况下,您必须将“www.example.com”替换为有效的主机名可访问的 IP 地址(因此,NO strong> 本地主机, 192.168.0.108)。

【讨论】:

    猜你喜欢
    • 2014-07-06
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多