รวมเครื่องมือสร้าง PDF File ด้วย PHP อย่างง่าย


 

แนะนำ 5 เครื่องมือสำหรับการสร้างไฟล์ PDF ด้วย PHP

  1. FPDF

    ตัวอย่าง

    <?php
    require('fpdf/fpdf.php');
    
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();
    ?>
    
  2.  mPDF

    ตัวอย่าง

    <?php
    require_once __DIR__ . '/vendor/autoload.php';
    
    $mpdf = new mPDF();
    
    // Write some HTML code:
    
    $mpdf->WriteHTML('<h1>Hello World</h1><br><p>My first PDF with mPDF</p>');
    
    // Output a PDF file directly to the browser
    $mpdf->Output();
    ?>

  3.  DOMPDF

    ตัวอย่าง

    <?php
    require_once 'dompdf/autoload.inc.php';
    // reference the Dompdf namespace
    use Dompdf\Dompdf;
    
    // instantiate and use the dompdf class
    $dompdf = new Dompdf();
    $dompdf->loadHtml('<h1>DOMPDF Demo</h1><br><p>Hello World !</p>');
    
    // (Optional) Setup the paper size and orientation
    $dompdf->setPaper('A4', 'portrait');
    
    // Render the HTML as PDF
    $dompdf->render();
    
    // Output the generated PDF to Browser
    $dompdf->stream();

  4.  Snappy

    ตัวอย่าง

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use Knp\Snappy\Pdf;
    
    // For example, in windows use the wkhtmltopdf executable file
    $snappy = new Pdf('wkhtmltopdf.exe');
    
    // Download the streamed PDF
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename="file.pdf"');
    echo $snappy->getOutput('http://www.github.com');
    

  5.  TCPDF

    ตัวอย่าง

    <?php
    // Include the main TCPDF library (search for installation path).
    require_once('tcpdf_include.php');

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

    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Our Code World');
    $pdf->SetTitle('Example Write Html');

    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 006', PDF_HEADER_STRING);

    // 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 image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

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

    $html = '<h4>PDF Example</h4><br><p>Welcome to the Jungle</p>';
     
    $pdf->writeHTML($html, true, false, true, false, '');
    // add a page
    $pdf->AddPage();

    $html = '<h1>Hey</h1>';
    // output the HTML content
    $pdf->writeHTML($html, true, false, true, false, '');

    // reset pointer to the last page
    $pdf->lastPage();
    //Close and output PDF document
    $pdf->Output('example_006.pdf', 'I');

 

ขอบคุณที่มาข้อมูล https://ourcodeworld.com/articles/read/226/top-5-best-open-source-pdf-generation-libraries-for-php

ความคิดเห็น