简体   繁体   English

PHP / HTML导出表为PDF?

[英]PHP/HTML Export table as pdf?

I have a HTML table that displays data from a db using PHP, I'd like to give an option to print the table out (as a button). 我有一个HTML表,该表显示使用PHP的数据库中的数据,我想提供一个选项来打印表(作为按钮)。 I thought that exporting as a pdf would be the simplest option to do this. 我认为以pdf格式导出将是执行此操作的最简单选择。 This is how I have the table laid out. 这就是我布置桌子的方式。

 <table id="tfhover" class="tftable" border="1"> <tr><th>Refund ID</th><th>Customer ID</th><th>Manager ID</th><th>Customer Name</th><th>Amount Refunded</th><th>Refund Date/Time</th><th>Details</th></tr> <?php //some php code... echo '<tr>'; echo '<td>'. $idRefund[$i] .'</td>'; echo '<td>'. $idCustomer[$i] .'</td>'; echo '<td>'. $idManager[$i] .'</td>'; echo '<td>'. $customerName[$i] .'</td>'; echo '<td>'. $amount[$i] .'</td>'; echo '<td>'. $dateTime[$i] .'</td>'; echo '<td>'. $details[$i] .'</td>'; echo '</tr>'; $i++; ?> </table> 

How would I go about printing this table using php or javascript or even exporting as a pdf if thats more simplistic. 如果那比较简单,我将如何使用php或javascript打印此表,甚至将其导出为pdf。

edit: I ended up using mPDF which is very similar to FPDF in terms of the methods available to use. 编辑:我最终使用了mPDF,就可用方法而言,它与FPDF非常相似。 The help is much appreciated, thanks. 非常感谢您的帮助,谢谢。

The PDF Generator PDF生成器

    $pdf=new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();
?

To generate a pdf file, first we need to include library file fpdf.php . 要生成pdf文件,首先我们需要包含库文件fpdf.php Then we need to create an FPDF object using the default constructor FPDF() . 然后,我们需要使用默认构造函数FPDF()创建一个FPDF对象。 This constructor can be passed three values namely page orientation (portrait or landscape), measure unit, and page size (A4, A5, etc.,). 可以向此构造函数传递三个值,即页面方向(纵向或横向),度量单位和页面大小(A4,A5等)。 By default pages are in A4 portrait and the measure unit is millimeter. 默认情况下,页面为A4纵向,度量单位为毫米。 It could have been specified explicitly with: 可以通过以下方式明确指定:

$pdf=new FPDF('P','mm','A4');

It is possible to use landscape (L), other page formats (such as Letter and Legal) and measure units (pt, cm, in). 可以使用横向(L),其他页面格式(例如Letter和Legal)和度量单位(pt,cm,in)。

Then we have added a page to our pdf document with AddPage() . 然后,我们使用AddPage()将页面添加到了pdf文档中。 The origin is at the upper-left corner and the current position is by default placed at 1 cm from the borders; 原点位于左上角,默认情况下当前位置位于距边界1厘米的位置; the margins can be changed with the function SetMargins() . 可以通过函数SetMargins()更改边距。

To print a text, we need to first select a font with SetFont() . 要打印文本,我们首先需要使用SetFont()选择字体。 Let us select Arial bold 16 让我们选择Arial粗体16

$pdf->SetFont('Arial','B',16);

We use Cell() function to output a text. 我们使用Cell()函数输出文本。 A cell is a rectangular area, possibly framed, which contains some text. 单元格是一个矩形区域,可能带有边框,其中包含一些文本。 It is output at the current position. 在当前位置输出。 We specify its dimensions, its text (centered or aligned), if borders should be drawn, and where the current position moves after it (to the right, below or to the beginning of the next line). 我们指定其尺寸,其文本(居中或对齐),是否应绘制边框以及当前位置在其后的位置(向右,下方或下一行的开始)。 To add a frame, we would do this: 要添加框架,我们可以这样做:

$pdf->Cell(40,10,'Hello World !',1);

Finally, the document is closed and sent to the browser with Output() . 最后,关闭文档,并使用Output()将其发送到浏览器。 We could have saved it in a file by passing the desired file name. 我们可以通过传递所需的文件名将其保存在文件中。

To learn Fpdf: Click Here To refer about this code : Click Here 要学习Fpdf: 单击此处以参考此代码: 单击此处

看一下KnpLabs / snappy ,它从url或html页面生成PDF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM