【问题标题】:DOMPDF error http error 500DOMPDF 错误 http 错误 500
【发布时间】:2017-08-21 00:13:47
【问题描述】:

每当客户要求提供一些记录的硬拷贝时,我想在我的数据库中为我的表打印一个 PDF 文件。不幸的是,当我打印记录或将其转换为 pdf 格式时,它一直说:

localhost 当前无法处理此请求。 HTTP 错误 500

即使我摆脱了表格并尝试在其上放置任何 html 元素。

代码如下:

<?php
//index.php
//include autoloader

require_once '../autoload.inc.php';

// reference the Dompdf namespace

use Dompdf\Dompdf;

//initialize dompdf class

$document = new Dompdf();

$html = '
    <style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>
';

//$document->loadHtml($html);
$page = file_get_contents("cat.html");

//$document->loadHtml($page);

$connect = mysqli_connect("localhost", "root", "", "pdf");

$query = "
    SELECT *
    FROM preschoolers 
";
$result = mysqli_query($connect, $query);

$output = "
    <style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Interp</th>
    </tr>
";

while($row = mysqli_fetch_array($result))
{
    $output .= '
        <tr>
            <td>'.$row["p_id"].'</td>
            <td>'.$row["last_name"].'</td>
            <td>$'.$row["interp"].'</td>
        </tr>
    ';
}

$output .= '</table>';

//echo $output;

$document->loadHtml($output);

//set page size and orientation

$document->setPaper('A4', 'landscape');

//Render the HTML as PDF

$document->render();

//Get output of generated pdf in Browser

$document->stream("Webslesson", array("Attachment"=>0));
//1  = Download
//0 = Preview


?>

我正在使用 DOMPDF。 寻求帮助。 提前致谢。

【问题讨论】:

  • 如 cmets 中所述,这将有助于查看错误是什么。 500 错误来自您的 Web 服务器,表明底层技术遇到了致命错误。检查您的 PHP 日志。

标签: php html pdf dompdf


【解决方案1】:

这就是我使用 DOMPDF 的方式。我经常用这个。我使用的版本是 0.7.0。我提供了一个完整的测试样本,所以你应该知道,如果它不起作用,那就是你的 HTML。 DOMPDF 可能对它成功用于 HTML 的内容很挑剔。试试我的示例代码,但要确保包含 autoload.inc.pdf 的行确实是 autoload.inc.pdf 的位置。

<?php

use Dompdf\Dompdf;
use Dompdf\Options;

function write_file($path, $data, $mode = 'wb')
{
    if ( ! $fp = @fopen($path, $mode))
        return FALSE;

    flock($fp, LOCK_EX);
    fwrite($fp, $data);
    flock($fp, LOCK_UN);
    fclose($fp);

    return TRUE;
}

function pdf_create( $html, $filename, $output_type = 'stream', $dompdf_cfg = [] )
{
    // Remove all previously created headers if streaming output
    if( $output_type == 'stream' )
         header_remove();

    // Load dompdf and create object
    require_once '../autoload.inc.php';
    $options = new Options();
    $options->set('isHtml5ParserEnabled', TRUE);
    $options->set('isRemoteEnabled', TRUE);
    $dompdf = new Dompdf($options);

    foreach( $dompdf_cfg as $k => $v )
        $dompdf->$k($v);

    // Loads an HTML string
    $dompdf->loadHtml( $html );

    // Create the PDF
    $dompdf->render();

    // If destination is the browser
    if( $output_type == 'stream' )
    {
        $dompdf->stream( $filename );
    }

    // Return PDF as a string (useful for email attachments)
    else if( $output_type == 'string' )
    {
        return $dompdf->output( ['compress' => 1] );
    }

    // If saving to the server
    else 
    {
        // Save the file
        write_file( $filename, $dompdf->output() );
    }
}

$html = '<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>Hello World</title>
    <style>
        h1{color:red;}
    </style>
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>';

pdf_create( $html, 'Webslesson', 'stream' );

这应该对您有用,如果它对您的 HTML 有效,然后又无效,请在此处发布您的 HTML,我们将尝试找出它有什么问题。我想您的 DOMPDF 版本也有可能与我的不同。如果您遇到问题,请告诉我。同样,这段代码已经过测试,并且一直在使用。

【讨论】:

  • 我试了还是一样的错误。也许是因为我的 dompdf 版本?我正在使用 dompdf master。
  • @JayzdeVera,您是否单独尝试了我的代码,但仍然出现同样的错误?如果是这种情况,那么您需要查看服务器日志以获取更多信息。
  • 你的意思是在我的服务器上?
  • 是的,服务器应该有一个错误日志,并告诉你发生了什么。
  • 当我尝试运行它时,它说 localhost 当前无法处理这个请求。 HTTP 错误 500
猜你喜欢
  • 2018-02-28
  • 2018-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
相关资源
最近更新 更多