用PHP创建PDF文件

Create a PDF file with PHP

我想从php编写的网页上创建pdf文件。 我的文档必须由mysql生成并生成pdf文件。我想保存该pdf并阅读。请给我代码示例。


使用TCPDF库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?
require_once("tcpdf/tcpdf.php");
$pdf = new TCPDF();

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("my name");
$pdf->SetTitle("my doc");
$pdf->SetSubject("x");
$pdf->SetKeywords("a, b, c");


// set default header data
$pic ="/gfx/header.png";
$pdf->SetHeaderData(realpath($pic),"25","Title");

// set header and footer fonts
$pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

    //set auto page breaks
 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

 //set image scale factor
 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

 //set some language-dependent strings
 $pdf->setLanguageArray($l);

 //initialize document
 $pdf->AliasNbPages();

 // add a page
 $pdf->AddPage();

 // ---------------------------------------------------------

 // set font
 $pdf->SetFont("helvetica","", 12);

 // set a background color
 $pdf->SetFillColor(230, 240, 255, true);


$pdf->SetFont("","b", 16);
$pdf->Write(16,"some text\
"
,"", 0, 'C');

$pdf->Output("filename.pdf","I");
?>


如果您已经有了HTML页面,最快的方法可能是使用HTML2PDF之类的工具将HTML数据"转换"为PDF,而不是"从头开始"生成PDF文件。

引用该页面:

It allows the conversion of valid HTML
4.01 in PDF format, and is distributed under LGPL.

This library has been designed to
handle mainly TABLE intertwined to
generate invoices delivery, and other
official documents. It does not yet
have all the tags.

有一些HTML的示例,以及相应的PDF文件,因此您可以看到它的功能。


我用CutyCapt做到这一点。

  • 下载并安装CutyCapt! 和xvfb-run。
  • 在html页面中创建pdf样式,然后通过以下命令从该页面创建pdf:
  • xvfb-run cutycapt --min-width = 1485 --url ='html url'--out ='pdf路径'


    从https://github.com/tecnickcom/tc-lib-pdf下载最新版本的TCPDF源代码,源代码本身具有一个目录,该目录以示例命名。 您可以从示例中查看代码,也可以尝试以下代码。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    <?php

    require_once('tcpdf/config/tcpdf_config.php');
    require_once('tcpdf/tcpdf.php');

    // create new PDF document
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

    // set default font subsetting mode
    $pdf->setFontSubsetting(true);
    // set margins
    //$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    //$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    // set default header data
    $pdf->SetHeaderData('', '', 'Hello ','Gudmrng', array(0,64,255), array(0,64,128));
    $pdf->setFooterData(array(0,64,0), array(0,64,128));

    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

     // set default monospaced font
     $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    // set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

     // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

    // Set font
    // dejavusans is a UTF-8 Unicode font, if you only need to
    // print standard ASCII chars, you can use core fonts like
    // helvetica or times to reduce file size.
     $pdf->SetFont('dejavusans', '', 10, '', true);
     //     $pdf->SetFont('helvetica', '', 8);
      // Add a page
     // This method has several options, check the source code documentation for more information.
    $pdf->AddPage();
     $tbl_header = '<h2 style="color:blue;text-align: center;font-size: 14px;">Headding</br>';

     $html = '<table width="530" cellspacing="0" cellpadding="3" border="1">
                        <tr>
                        <th style="text-align: center;font-size: 10px;font-weight: bold;width:100px;">heading 1</th>
                        <th style="text-align: center;font-size: 10px;font-weight: bold;width:122px;">heading 2</th>
                        </tr>'
    ;
                            $html .= '<tr>';
                            $html .=  '<td>Hello World</td>';
                            $html .= '<td>Helo </td>';
                            $html .=  '</tr>';
                            $html .=  '</table>';


     $pdf->writeHTML($tbl_header . $html, true, false, false, false, '');
     //$pdf->writeHTML($html, true, 0);
     //$pdf->writeHTML($html, true, 0);


      $pdf->Output('test_1.pdf','I');

     ?>