借助 php 生成 PDF
我有为我生成 PDF 的代码。我正在使用 FPDF 生成 PDF。有时它会输出,但有时它的行为很奇怪,它会创建格式错误的 pdf,因此无法在任何 pdf 阅读器中打开。
此外,当它给我错误的格式时,pdf的大小会急剧增加..
任何人都可以建议特殊情况下出了什么问题..?
这是代码..
$pdf_Str = "<table border='1'>
<tr bgcolor=\"#666666\">
<td>Name</td>
<td>Number</td>
<td>Adjusted Amount</td>
<td>Invoice Date</td>
<td>Transaction Date</td>
<td>Amount</td>
<td>Balance</td>
<td>Record Type</td>
<td>Session</td>
<td>Course</td>
</tr>";
foreach($data as $row){
$pdf_Str .="<tr>
<td>".$row['student_name']."</td>
<td>".$row['invoiceNumber']."</td>
<td>".$row['invoiceAdjustedAmount']."</td>
<td>".($row['invoiceDate']!=''?$row['invoiceDate']:' ')."</td>
<td>".($row['TransactionDate']!=''?$row['TransactionDate']:' ')."</td>
<td>".($row['invoiceAmount']!=''?$row['invoiceAmount']:' ')."</td>
<td>".($row['invoiceBalance']!=''?$row['invoiceAmount']:' ')."</td>
<td>".($row['recordType']!=''?$row['recordType']:' ')."</td>
<td>".$row['sessionName']."</td>
<td>".($row['catalogName']!=''?$row['catalogName']:' ')."</td>
</tr>";
}
$pdf_Str .='</table>';
$p = new PDFTable();
$p->setfont('times','',12);
$p->htmltable($pdf_Str);//
$p->output("student_info.pdf",'I');
I have code that generates PDF for me.I am using FPDF to generate PDF.sometimes it gives out put but sometimes it behaves weirdly and it creates pdf with bad format so it doesnt open in any pdf reader.
moreover when it gives me bad format, size of pdf increases dramatically..
can anyone suggest what wrong is going with special cases..?
here is the code..
$pdf_Str = "<table border='1'>
<tr bgcolor=\"#666666\">
<td>Name</td>
<td>Number</td>
<td>Adjusted Amount</td>
<td>Invoice Date</td>
<td>Transaction Date</td>
<td>Amount</td>
<td>Balance</td>
<td>Record Type</td>
<td>Session</td>
<td>Course</td>
</tr>";
foreach($data as $row){
$pdf_Str .="<tr>
<td>".$row['student_name']."</td>
<td>".$row['invoiceNumber']."</td>
<td>".$row['invoiceAdjustedAmount']."</td>
<td>".($row['invoiceDate']!=''?$row['invoiceDate']:' ')."</td>
<td>".($row['TransactionDate']!=''?$row['TransactionDate']:' ')."</td>
<td>".($row['invoiceAmount']!=''?$row['invoiceAmount']:' ')."</td>
<td>".($row['invoiceBalance']!=''?$row['invoiceAmount']:' ')."</td>
<td>".($row['recordType']!=''?$row['recordType']:' ')."</td>
<td>".$row['sessionName']."</td>
<td>".($row['catalogName']!=''?$row['catalogName']:' ')."</td>
</tr>";
}
$pdf_Str .='</table>';
$p = new PDFTable();
$p->setfont('times','',12);
$p->htmltable($pdf_Str);//
$p->output("student_info.pdf",'I');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法在 PHP 中创建表。您必须使用 fpdf 内置函数来执行此操作。
参考这个:
使用 fpdf 在 pdf 中创建包含包装数据的表格php
并检查一下它可能对您有帮助。
http://www.elated.com/articles/create -nice-looking-pdfs-php-fpdf/
You can't create table in PHP. You have to use fpdf built-in function to do that.
Refer this :
create table in pdf with wrap data using fpdf in php
and also Check this out it may help's you.
http://www.elated.com/articles/create-nice-looking-pdfs-php-fpdf/
据我记得 FPDF 不允许转换 html->pdf。您需要使用它自己的函数(Cell()、MultyCell() 等)。但是您可以使用库 mPDF
它真的很棒而且非常容易使用。它可以转换html+css(!)->pdf。它能出色地完成这项工作。我推荐这个图书馆。
我希望这会有所帮助。
As far as I remember FPDF doesn't allow to convert html->pdf. You need to use its own functions(Cell(), MultyCell(), etc). But you could use library mPDF
It is really great and very easy to use. It can convert html+css(!)->pdf. And it handles this job awesome. I recomend this library.
I hope it will be helpfull.
您可以尝试 http://code.google.com/p/dompdf/ 有很多HTML 到 PDF 库的可用,我已经使用 FPDF 库来创建具有复杂表格的发票,只要 css 不太复杂,它通常会呈现得很好。
我仍然发现,当涉及到打印就绪的 PDF 等内容时,单独使用 FPDF 库会产生更好的结果,您只需付出努力,坐在那里输出文件内联点击刷新,同时轻推周围的元素。
对于上面的内容,您应该能够创建一些像样的代码,使用
Put your header and data into an array 并通过在每行末尾将 $ln 交换为 2 进行循环,将 border 保留为 0 直到您对输出感到满意等。
You could try http://code.google.com/p/dompdf/ there are plenty of HTML to PDF library's available and I have used the one for FPDF for creating invoices with complex tables, as long as the css is not too complex it usually renders fine.
I still find that using the FPDF library by itself produces better results when it comes to things like print ready PDF's you just need to put in the effort and sit there outputting the file inline hitting refresh while nudging elements around.
For the above you should be able to create some decent code using
Put your header and data into an array and loop through swapping $ln to 2 at the end of each row, leave border as 0 until your happy with your output etc..