每当客户要求提供一些记录的硬拷贝时,我想在我的数据库中为我的表打印PDF文件。不幸的是,当我打印记录或将其转换为pdf格式时,它一直说:
localhost目前无法处理此请求。 HTTP ERROR 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。 寻求帮助。 提前谢谢。
答案 0 :(得分: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版本也可能与我的版本不一样。如果您遇到问题,请告诉我。同样,这段代码经过测试,并且一直在使用。