Create a PDF file with PHP

前端 未结 4 2169
醉酒成梦
醉酒成梦 2020-11-28 09:15

I want to create pdf file from my web page written by php . My document must produce from mysql and produce pdf file.I want to be to save this pdf and to read.Please give me

相关标签:
4条回答
  • 2020-11-28 09:58

    Download newest version of TCPDF source code from https://github.com/tecnickcom/tc-lib-pdf , The Source code itself have a directory which the folder named as examples. you can check out the code from the examples or else you can try below code.

    <?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</h2></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><b>Hello World</b></td>';
                            $html .= '<td><b>Helo </b></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');
    
     ?>
    
    0 讨论(0)
  • 2020-11-28 10:09

    I you already have an HTML page, the fastest way for you might be to use a tool like HTML2PDF to "convert" that HTML data to PDF, instead of generating a PDF file "from scratch".

    Quoting that page :

    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.

    There are some example of HTML, and the corresponding PDF files, so you can see what's capable of.

    0 讨论(0)
  • 2020-11-28 10:11

    Using the TCPDF library:

    <?
    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\n", "", 0, 'C');
    
    $pdf->Output("filename.pdf", "I");
    ?>
    
    0 讨论(0)
  • 2020-11-28 10:17

    i used CutyCapt for do that.

    1. download and install a CutyCapt ! and xvfb-run .
    2. make pdf style in html page and then create pdf from this page by this command:

    xvfb-run cutycapt --min-width=1485 --url='html url' --out='path to pdf'

    0 讨论(0)
提交回复
热议问题