有没有一种方法可以使用tcpdf从html表生成pdf,并且所有这些都在php的一个div中



我已经将JSON API转换为HTML TABLE,并希望将此HTML TABLE生成为PDF。我使用TCPDF,并尝试了所有与使用TCPDF生成pdf相关的方法。所有这些都只在一个div中。我不知道我的代码哪里出了问题,而且我刚刚开始使用PHP,所以除了HTML之外,我对我的代码了解不多。

这是我的代码:

<?php                               
$askPriceBeds = '{"status":"success","postcode":"W14 9JH","postcode_type":"full","url":"https://propertydata.co.uk/draw?input=W14+9JH","bedrooms":2,"data":{"points_analysed":20,"radius":"0.09","average":657495,"70pc_range":[ 575000, 725000 ],"80pc_range":[ 550000, 875000 ],"90pc_range":[ 550000, 925000 ],"100pc_range":[ 525000, 950000 ],"raw_data":[ {"price":650000,"lat":"51.48887000","lng":"-0.20776000","bedrooms":2,"type":"flat","distance":"0.00" }, {"price":575000,"lat":"51.48884800","lng":"-0.20701200","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":615000,"lat":"51.48851000","lng":"-0.20742000","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":640000,"lat":"51.48932000","lng":"-0.20804000","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":725000,"lat":"51.48843100","lng":"-0.20775400","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":699950,"lat":"51.48928000","lng":"-0.20793700","bedrooms":2,"type":"flat","distance":"0.03" }, {"price":550000,"lat":"51.48941000","lng":"-0.20832000","bedrooms":2,"type":"flat","distance":"0.04" }, {"price":600000,"lat":"51.48813700","lng":"-0.20781600","bedrooms":2,"type":"flat","distance":"0.05" }, {"price":925000,"lat":"51.48948300","lng":"-0.20827400","bedrooms":2,"type":"flat","distance":"0.05" }, {"price":650000,"lat":"51.48824900","lng":"-0.20669000","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":700000,"lat":"51.48941200","lng":"-0.20666900","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":625000,"lat":"51.48817000","lng":"-0.20689500","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":645000,"lat":"51.48822500","lng":"-0.20872100","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":675000,"lat":"51.48877800","lng":"-0.20643200","bedrooms":2,"type":"flat","distance":"0.06" }, {"price":525000,"lat":"51.48978800","lng":"-0.20864000","bedrooms":2,"type":"flat","distance":"0.07" }, {"price":700000,"lat":"51.48785100","lng":"-0.20757600","bedrooms":2,"type":"flat","distance":"0.07" }, {"price":675000,"lat":"51.48819900","lng":"-0.20637900","bedrooms":2,"type":"flat","distance":"0.08" }, {"price":875000,"lat":"51.48818700","lng":"-0.20931800","bedrooms":2,"type":"flat","distance":"0.08" }, {"price":550000,"lat":"51.48891000","lng":"-0.20955000","bedrooms":2,"type":"flat","distance":"0.08" }, {"price":950000,"lat":"51.48808300","lng":"-0.20628500","bedrooms":2,"type":"flat","distance":"0.09" } ] },"process_time":"2.08"}';
$data = json_decode($askPriceBeds, true);
$raw_data = $data['data']['raw_data'];
?>  
<!DOCTYPE html>
<html>
<body>
<table>
<thead>
<tr>
<td>Price</td>
<td>Lat</td>
<td>Lng</td>
<td>Bedrooms</td>
<td>Type</td>
<td>Distance</td>
</tr>
</thead>
<tbody>
<?php
foreach($raw_data as $raw){
echo "<tr>
<td>".$raw['price']."</td>
<td>".$raw['lat']."</td>
<td>".$raw['lng']."</td>
<td>".$raw['bedrooms']."</td>
<td>".$raw['type']."</td>
<td>".$raw['distance']."</td>
</tr>";
}
?>
</tbody>
</table>
<form method="POST">
<input type="submit" name="generate_pdf" value="Generate PDF">
</form>
</body>
</html>

<?php
if(isset($_POST['generate_pdf'])){

require_once('tcpdf/tcpdf.php');

$tcpdf = new TCPDF(PDG_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$tcpdf->SetCreator('PDF_CREATOR');

$tcpdf->SetTitle('');

$tcpdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);

$tcpdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));

$tcpdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

$tcpdf->SetDefaultMonospacedFont('helvetica');
$tcpdf->SetFooterMargin(PDF_MARGIN_FOOTER);

$tcpdf->SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT);
$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(false);
$tcpdf->SetAutoPageBreak(true, 10);
$tcpdf->SetFont('helvetica', '', 12);
$tcpdf->AddPage('P', 'A4');

$html = '<html>
<head></head>
<body>
<table border="1">
<tr>
<th>Price</th>
<th>Lat</th>
<th>Lng</th>
<th>Bedrooms</th>
<th>Type</th>
<th>Distance</th>
</tr>
<tr>
<td>650000</td>
<td>51.48887000</td>
<td>-0.20776000</td>
<td>2</td>
<td>flat</td>
<td>0.00</td>
<tr>
<tr>
<td>575000</td>
<td>51.48884800</td>
<td>-0.20701200</td>
<td>2</td>
<td>flat</td>
<td>0.03</td>
<tr>
<tr>
<td>615000</td>
<td>51.48851000</td>
<td>-0.20742000</td>
<td>2</td>
<td>flat</td>
<td>0.03</td>
<tr>
</table>
</body>
</html>';

$tcpdf->writeHTML($html, true, 0, true, 0);
$tcpdf->lastPage();
$tcpdf->Output("datareport.pdf", 'I');
}
?>

我想知道这里的错误是否是一个PHP文件不足以生成pdf?

试试这个,看看它是否有帮助:

<?php                               
if (!isset($_POST['generate_pdf']))
{ 
//Your JSON code
?>
//Your HTML code
<?php
}
else
{
//Your PDF code
}
?>

您的代码在检查之前发送HTML,无论您是否真的希望输出PDF。这将导致实际的PDF文件被视为HTML的一部分,而不是一个独立的文档。

提示这一点的常见错误消息如下(类似(:Cannot send headers. Headers have already been sent.

相关内容

最新更新